I am new to programming. I am trying to do a program where I have dynamically allocated memory to an array. I know that:
int arr[10];
and
sizeof(arr); // gives output 10*4 = 40
But if I write :
int *arr = (int *)malloc(10*sizeof(int));
then
sizeof(arr); // doesn't give output 10*4
is there any way to get this size that is being allocated dynamically? Please help:
I have looked up and in a stack overflow post (Determine size of dynamically allocated memory in C), I got this (by @ars and editing: @kevinAlbs):
There is no standard way to find this information. However, some implementations provide functions like msize to do this. For example:
_msize on Windows
malloc_size on MacOS
malloc_usable_size on systems with glibc
Keep in mind though, that malloc will allocate a minimum of the size requested, so you should check if msize variant for your implementation actually returns the size of the object or the memory actually allocated on the heap.
If this post is the answer to my question, then can anyone please help me with how to use them. I just want to get that 40 as my answer.