0

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.

  • 2
    I am voting to close as a duplicate for the question you linked to: [Determine size of dynamically allocated memory in C](https://stackoverflow.com/questions/1281686/determine-size-of-dynamically-allocated-memory-in-c) – David Grayson Dec 29 '21 at 17:40
  • From `int arr[10];` the `sizeof(arr);` **cannot** `// gives output 10` because `int` must be at least 16 bits. In the second, you are getting the size of a pointer. There is no standard way to obtain the allocation size, but you were the one who allocated it, so you already know. – Weather Vane Dec 29 '21 at 17:42
  • If you are trying to use one of those platform-specific functions to get the allocation size and having trouble, it would be fine to ask a specific question about the problem you are having, but you should show your code and describe in detail the specific problem you are having. But it would be way too broad to just ask for generic "help" using 3 different functions on 3 different OSes. – David Grayson Dec 29 '21 at 17:43
  • @DavidGrayson actually I have posted that in my question. But I don't know whether that is my answer. Plus I don't even know how to use them. Please help – Surya Majumder Dec 29 '21 at 17:43
  • 1
    Read the linked documentation for those functions, then make an attempt. – dbush Dec 29 '21 at 17:44
  • 1
    @WeatherVane sorry and thanks for pointing that out. I have edited the question now. Thanks – Surya Majumder Dec 29 '21 at 17:46
  • Ok thanks, @dbush . I will try – Surya Majumder Dec 29 '21 at 17:47
  • @WeatherVane: `sizeof arr` can be 10. An `int` must be at least 16 bits, but it can be a single byte. – Eric Postpischil Dec 29 '21 at 19:00
  • @EricPostpischil it can be but when is it? The OP does acknowledge it was a mistake. – Weather Vane Dec 29 '21 at 19:03
  • @EricPostpischil some examples were posted in [Is CHAR_BIT ever > 8?](https://stackoverflow.com/questions/32091992/is-char-bit-ever-8) – Weather Vane Dec 29 '21 at 19:20

0 Answers0