Determine size of dynamically allocated memory in C

后端 未结 15 2503
孤街浪徒
孤街浪徒 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 06:57

    This may work, a small update in your code:

    void* inc = (void*) (++p)
    size=p-inc;
    

    But this will result 1, that is, memory associated with p if it is char*. If it is int* then result will be 4.

    There is no way to find out total allocation.

提交回复
热议问题