Given the following code:
If I had no idea how much space was allocated, how would I get the size of the space that the pointer p is pointing to (which would be 80 in this case)?Code:int *p = malloc(sizeof(int) * 20);
Given the following code:
If I had no idea how much space was allocated, how would I get the size of the space that the pointer p is pointing to (which would be 80 in this case)?Code:int *p = malloc(sizeof(int) * 20);
Use real variables for size instead of magic numbers.
It's platform specific (see MSDN for windows, I don't know how to do it on linux). I suggest you keep track if it yourself though.
For example:
Code:size_t pSize = 0; int * p = NULL; pSize = 20 * sizeof(int); p = malloc(pSize);
How is that possible? You know how much space was allocated because you know what you passed to malloc.If I had no idea how much space was allocated