Size of dynamic array in C doesn't change

匆匆过客 提交于 2021-02-05 09:50:32

问题


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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *inp;
    printf("%lu ",sizeof(inp));
    char *res = (char*)malloc(15*sizeof(char*));
    printf("%lu ",sizeof(res));
    res = "hello world";
    printf("%lu\n",sizeof(res));
    return 0;
}

And surprisingly it outputs 8 8 8. Can anyone explain why is it like that? Why is it 8 by default? And how malloc() effects size of inp?


回答1:


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.




回答2:


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




回答3:


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




回答4:


****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.



来源:https://stackoverflow.com/questions/29391494/size-of-dynamic-array-in-c-doesnt-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!