Size of dynamic array in C doesn't change

前端 未结 4 1372
我在风中等你
我在风中等你 2021-01-29 02:39

I was getting realloc(): invalid next size for a program. So I just coded this to understand what\'s happening.

#include 
#include &l         


        
4条回答
  •  感动是毒
    2021-01-29 03:17

    You are using sizeof.returns size in bytes of the object representation of type. Remember sizeof is an operator.

    Now sizeof is returning here 8 a constant type of size_t

    Why isn't it changing? because in all tha cases you are using the same type that is char *.

    Here 8 bytes is the size of the character pointer that is 64 bit.

    You can probably have a look at this-

    printf("size of array of 10 int: %d\n" sizeof(int[10]));

    This will give the output:40. 40 bytes.

    And malloc will never affect the size of a char*. It still needs 64 bits to store an address of a dynamically allocated space from heap.

    Is it possible to determine size of dynamically allocated memory in c?

    There is no standard way to do this. You have to take this overhead on your own. You may find some extensions to the compiler that may accomplish this.

提交回复
热议问题