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
Related
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 3 years ago.
Memory addresses specify how many bytes of data they point to in memory, so it seems that the size of any variable is determined by looking at the memory address and seeing how much the variable occupies in memory. So how is the size of an array determined??? - because an array is a pointer to only the first item in the array by default:
int main() {
int a[] = {1,2,3,4,5};
// both show same memory address
printf("%p\n", a);
printf("%p\n", &a[0]);
// somehow the entire size of a is calculated
printf("%lu\n", sizeof(a)); // 20 (all elements)
return 0;
}
When you write
int a[] = {1,2,3,4,5};
The compiler already knows "a" will only have 5 ints in it.
When you call
sizeof(a)
You compiler (not your program) will calculate the size of a. This basically sets the number "20" in your program. Everytime your program runs, it'll output the number 20, it won't use sizeof. This is not evaluated at runtime, this is evaluated at compile time, since in your case sizeof is a compile time operator.
(To be noted sizeof can be evaluated at runtime when you have variable length arrays)
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 6 years ago.
Firstly before I explain, here is the code in C:
#include <stdio.h>
void printSize(char* str);
int main()
{
char a[100] = "StackOverflow";
printSize(a);
return 0;
}
void printSize(char* str)
{
int n = sizeof(str) / sizeof(char);
printf("%d" ,n);
}
As you can already see, I am trying to get my function to print the value "100" as 100 is the size that I have allocated to my char array a. However I am getting 8 as my answer. Now I know why I am getting 8 here because it's printing the size of the pointer. So my question is, if I can't use this method to print out the value of "100" then how can I find out the memory allocated to a from inside the function?
Thanks
To get the length of a string you use the strlen function. To get the size of the array you need to pass it as an argument.
When you get the size of a pointer, you get the size of the actual pointer and not what it points to. In your case you seem to be on a 64-bit platform where pointers are 64 bits (8 bytes), then dividing that by sizeof(char) (which is defined to always be 1) you get the value 8.
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:
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.
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.