This question already has answers here:
Newbie questions about malloc and sizeof
(8 answers)
Closed 7 years ago.
I am a newbie to C,
My Problem is the following code,
int Max[10],*New_Max;
int length=5;
New_Max=(int)malloc(sizeof(int)*length));
printf("sizeof(Max)=%d,sizeof(New_Max)=%d",sizeof(Max),sizeof(New_Max));`
Output:
sizeof(Max)=40,sizeof(New_Max)=4
I Expect sizeof(New_Max) to be 20, but it prints as 4.
Can anyone explain the reason for it.
The following line allocates memory to what New_Max is pointing.
New_Max=(int)malloc(sizeof(int)*length));
And, the following line grabs the info about what a pointer size is :
sizeof(New_Max)
So, the size of what pointer is pointing and its own size are two different things. That's why you are getting size of New_Max to be 4.
You are making wrong assumption, that sizeof behaves in the same way for both types of memory allocation. There is fundamental difference between array and pointer and how it relates to sizeof. If you provide it with array, like here:
int Max[10];
size_t size = sizeof(Max);
then you will get overall size, that is number of elements multiplied by size of each element, that is:
10 * sizeof(Max[0])
On the other hand, for pointers, it results into size of pointer itself, not taking any elements into account, thus what you get is just sizeof(int *). It does not matter if it points to NULL or some array object, the size would be the same.
Note that, the proper format specifier for sizeof is %zu, not the %d.
Related
This question already has answers here:
Using sizeof() on malloc'd memory [duplicate]
(5 answers)
Closed 6 years ago.
For the following C code, I expect the last printf to print "10,10" when I input only one character. Instead it prints "10,8". Why is input only 8 bytes when I malloc 10 bytes?
char* input;
unsigned long inputLen = 10;
input = (char*) malloc(10 * sizeof(char));
printf("Input: ");
getline(&input,&inputLen,stdin);
printf("%lu,%d",inputLen,sizeof(input));
sizeof(input) returns the size of the pointer input, which is 8 bytes.
Via the C FAQ:
Q: Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?
A: sizeof tells you the size of the pointer. There is no portable way to find out the size of a malloc'ed block. (Remember, too, that sizeof operates at compile time, and see also question 7.27.)
If you want to keep track of the capacity of input, you need to use a separate variable, or declare input on the stack as an array, and divide the size of the array by the size of an individual element of the array. It's almost always easier just to preserve the capacity as a separate variable.
Why is input only 8 bytes when I malloc 10 bytes?
Because input is a pointer and pointers are 8 bytes on your platform. The sizeof function just tells you the size of the type, determined at compile time.
It's because input is a char*. You should consider using strlen provided you null terminate it.
This question already has answers here:
Using sizeof() on malloc'd memory [duplicate]
(5 answers)
C: differences between char pointer and array [duplicate]
(14 answers)
Closed 8 years ago.
I have created an array using malloc with the following line of code:
int* array = (int*)malloc(12*sizeof(int));
I attempt to get the size with:
int size = sizeof(array)/sizeof(int);
This returns 2 however, not 12 (I'm aware that sizeof(array[0] is more general.)
Confusingly (for me), when I create an array using
int array[12]
the above line of code returns the correct size: 12.
Further, I'm able to fill the int* array with 12 integers without a segmentation fault, which I don't understand if the size turns out to be 2.
Could someone please tell me what I'm doing wrong /
how to get the size of an array initialized using malloc in c?
Sorry if this is basic I looked around and couldn't find an answer.
Thanks for your time.
int *arr = malloc(sizeof(int));
No need to cast malloc().
arr is a pointer so in the first case you are using the
sizeof(arr)
which gives you the size of pointer not the size of the array.
In the second case you have a array
int arr[12];
Here you get the size of your array arr which is
sizeof(arr)/sizeof(int)
This question already has answers here:
C - SizeOf Pointers
(4 answers)
Determine size of dynamically allocated memory in C
(15 answers)
Closed 9 years ago.
What is the problem with the following memory allocation?
char *buffer;
buffer = (char*)malloc(sizeof(char)*40);
printf("buffer size: %ld\n", sizeof(buffer));
This prints 8 instead of 40 (which I expected). Something I missed in understanding malloc or this is normal?
sizeof(buffer)
returns the size of buffer, which is a pointer to char. Size of pointers to char on your machine is 8.
The sizeof operator's result is entirely based on the type of the expression it operates on, not the identity or value. It can have no idea what size you passed to malloc unless you use a type that carries the size with it.
buffer is a pointer to char. When it will pass as argument to the sizeof operator, then sizeof(buffer) gives the size of pointer unlike the array names where it gives the size of the entire array in bytes. Always remember that arrays are not pointers.
This question already has answers here:
size of a pointer allocated by malloc [duplicate]
(4 answers)
Closed 9 years ago.
I'm trying to create a pointer to a 6 element int in a function to return it later, so for that purpose I'm using malloc, but it seems to be acting not as I expected. Here's the code:
int j = 0;
for (;j < 5; j++) {
int * intBig = malloc(j * sizeof(int));
printf("sizeof intBig - %ld\n", sizeof(intBig));
}
Prints the same number 8 bytes as the sizeof(intBig) at each iteration. Whereas I would expect a series of 4, 8, 12, 16. What am I missing in this instance?
This is because you're printing the size of an int *. Such a pointer always has the same size. sizeof is a compiler construct. It cannot know things that only occur at runtime, such as dynamic memory allocation. Would it be something like
int intBig[100];
then you would get the size of the array back (in bytes), because the compiler knows how large it is. But the result of the sizeof operator is always a compile-time constant¹, so there is no way what you have there could yield anything else.
Besides, you have a memory leak there because you're not free-ing your memory again.
¹ Variable Length Arrays (VLA) are an exception, but they were not used here.
You cannot use sizeof to figure out the size of a memory block returned from malloc().
Except for variable length arrays in C99 and later, sizeof works only on statically known sizes.
Because every time you are printing the size of a pointer which is the size of an address which is 8 bytes.
sizeof tells you the size of the pointer intBig, not what it points to.
There's no standard way to discover the size of the memory block it points to, so you have to remember that separately.
If you have access to C++, just use std::vector for your dynamic array needs... it knows its size and doesn't forget to deallocate.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
length of array in function argument
How do I get the length of a dynamically allocated array in C?
I tried:
sizeof(ptr)
sizeof(ptr + 100)
and they didn't work.
You can't. You have to pass the length as a parameter to your function. The size of a pointer is the size of a variable containing an address, this is the reason of 4 ( 32 bit address space ) you found.
Since malloc just gives back a block of memory you could add extra information in the block telling how many elements are in the array, that is one way around if you cannot add an argument that tells the size of the array
e.g.
char* ptr = malloc( sizeof(double) * 10 + sizeof(char) );
*ptr++ = 10;
return (double*)ptr;
assuming you can read before the array in PHP, a language which I am not familiar with.
Here you see the dangers of C: a ptr just points to memory and has no way of knowing what supposed size is. You can just increment and increment and the OS might complain eventually, or you crash your program, or corrupt other ones. You should always specify the size, and check bounds yourself!
This is similar to Using pointer for crossing over all elements in INTEGER array
See my answer here