Size of dynamic array in C doesn't change

前端 未结 4 1298
我在风中等你
我在风中等你 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:12

    sizeof(inp) gives you the size of pointer (8 bytes, 64-bits), not the storage under that pointer.

    0 讨论(0)
  • 2021-01-29 03:16

    sizeof(inp) in your case is sizeof(pointer) and hence sizeof(char-pointer) on your system is 8 bytes which is a constant. While allocating memory using malloc() you would have specified the size so why do you need to get the size of the allocated space again?

    And I see res is being initialized and not inp

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-29 03:26

    ****EDIT**** : The below post was written before the edit of the question.

    You're missing stdlib.h, to the most, for function malloc() to work properly. After that,

    Point 1:

    char *res = (char*)malloc(15*sizeof(char*));
    

    should be

    char *res = malloc(15);   //will also issue warning to resolve missing stdlib.h
    

    Point no note: you should be allocating memory for chars, not char *s. Then , you should write sizeof(char), not sizeof(char *). Also, sizeof(char) is always 1 in C. So, can omit that part.

    Please read: do not cast the return value of malloc() and family in C.

    Point 2:

    strings are not supposed to be assigned to already malloc()ed pointers. use strcpy() instead.

     inp = "hello world";
    

    should be

    strcpy(inp, "hello world");
    

    Otherwise, you'll overwrite the previously allocated memory, returned by malloc(). The assignment will overwrite the memory location held by inp, causing memory leak.

    Point 3.

    sizeof() operator returns a value of size_t. To print that, you need %zu format specifier.

    Related, from C11 standard document, chapter §7.21.6.1, paragraph 7,

    z

    Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument.


    Then, to answer the query about the output, in all the cases, you're printing sizeof(inp) which is essentially sizeof(char *) and that value is fixed for a particular platform (8, in your case).

    Just FYI, sizeof() is an operator, it's not a function. It returns the size of the datatype, not the amount of space pointed by the variable.

    0 讨论(0)
提交回复
热议问题