Iterate a Dynamically Allocated Array
I need to dynamically allocate an array of type int with size 12MB. I did it as follows:
unsigned int *memory; memory = (int *) malloc( 3072 * sizeof(int));
How would I iterate through the array without having to use 3072? Is there a way to get the length of the array? Or should I just do for (int i = 0; i < 3072; i++)?
The is no portable or convenient way to find out how large the allocate chunk is. After malloc is done all that is visible is a pointer to an indeterminate amount of memory (*). You must do the bookkeeping yourself.
(*) Actually malloc and friends do know how large the chunk is but there is no standard way for a client to access this information.