Determine size of dynamically allocated memory in C

后端 未结 15 2311
孤街浪徒
孤街浪徒 2020-11-22 06:15

Is there a way in C to find out the size of dynamically allocated memory?

For example, after

char* p = malloc (100);

Is there

15条回答
  •  遇见更好的自我
    2020-11-22 07:07

    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.

提交回复
热议问题