| C Tutorial | Dynamic arrays | |||
|
Advanced C Arrays Plus Syntax Pointer Style and strcpy() Pointer Type Effects Arrays and Pointers Array Names Are Const Heap Memory Memory Management Dynamic Arrays Advantages of being in the heap Disadvantages of being in the heap Dynamic Strings
|
Since arrays are just contiguous areas of bytes, you can allocate your own arrays in the heap using malloc(). The following code allocates two arrays of 1000 ints-- one in the stack the usual "local" way, and one in the heap using malloc(). Other than the different allocations, the two are syntactically similar in use.
{ int a[1000];
int *b; b = (int*) malloc( sizeof(int) * 1000); assert(b != NULL); // check that the allocation succeeded
a[123] = 13; // Just use good ol' [] to access elements b[123] = 13; // in both arrays.
free(b); }
Although both arrays can be accessed with [ ], the rules for their maintenance are very different.... Want To Know more with Video ??? Contact for more learning: webmaster@freehost7com |
|