This question already has answers here:
Getting the size of a malloc only with the returned pointer
(5 answers)
Closed 8 years ago.
I'm struggling with realloc...
strucType mkBggr (structType x, char ch) {
x = realloc(x, 100);
printf("%d", sizeof(x));
}
I'm thinking this should print out the value 100, but it prints 8.
It obviously has something to do with pointers, but I've no idea what. I've added *s and &s in front of the x's, but I don't seem to get it. Any help is appreciated!
Realloc returns a pointer, so x is of pointer type. sizeof(x) is returning the size of a pointer, which is 8 bytes in this environment.
It prints the value 8 because you're asking for the size of the pointer (x). You cannot print the size of a block of allocated memory - you'll have to track that yourself.
This is why I don't recommend using realloc; allmost everyone I see asking about it is using it wrong.
Related
This question already has answers here:
malloc in C: same sizeof before and after?
(5 answers)
Closed 3 years ago.
I allocate an array a 4 blocks (each is of size sizeof(int)). Then I try to print the length of the array with the following code:
int main() {
int *a = malloc(4 * sizeof(int));
printf("%d", sizeof(a)/sizeof(a[0]));
return 0;
}
Once I run this code, it prints 2, although I expected 4. Why is it?
sizeof is not a magical "how much space did I malloc into this pointer" function. The idiom you're using only works with real arrays, not pointers being dereferenced as arrays. Here, it means sizeof(int*)/sizeof(int), which is clearly not useful.
You are thinking that a is an array of 4 elements.
But it is not. It is a pointer.
I'm deducing that your system is a 64-bit system with 8-byte pointers.
So sizeof(a) is 8. And sizeof(a[0]) is sizeof(int) is 4 bytes.
So the expression becomes 8/4 == 2.
If you had declared a as int a[4];
Then you would indeed get 4 for a result.
This question already has answers here:
size of array in c
(6 answers)
Closed 5 years ago.
I am trying to write a c program that can print the size of a pointer variable that points to a memory location allocated by malloc().
C version I'm using is C99.
Below is my code :
#include<stdio.h>
int main()
{
int *temp;
temp=malloc(sizeof(int)*3);
printf("%d %d\n",sizeof(int),sizeof(temp));
return 0;
}
Output I'm getting is :
sh-4.2$ ./f1
4 8
according to the code, printf should print same values for both expressions. i.e 12. sizeof(int)=4.
Since i have allocated 12 bytes of memory for temp, sizeof(temp) should return 12. But, instead it returns the value 8.
I'm wondering what could be the reason? If anyone knows ,please answer.
sizeof does not return size of dynamically allocated memory, it just prints size of variable, 8 is size of temp pointer variable not the size of memory pointed by address in temp
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:
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.
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)