Why is malloc giving me 8 bytes when I request 20? [duplicate] - c

This question already has answers here:
Newbie questions about malloc and sizeof
(8 answers)
Closed 7 years ago.
I've just been playing around in C for the first time, and I'm at a loss as to why malloc is not giving me the amount of memory that I'd expect it to. The following code:
printf("Allocating %ld bytes of memory\n", 5*sizeof(int));
int *array = (int *) malloc(5*sizeof(int));
printf("%ld bytes of memory allocated\n", sizeof(array));
results in:
Allocating 20 bytes of memory
8 bytes of memory allocated
I've checked that I'm indeed calling malloc to give me 20 bytes, but don't understand why after calling malloc, the pointer only has 8 bytes.

array is not an array but an int *. So it's size will always be the size of the pointer.
The sizeof operator does not tell you how much memory was dynamically allocated at a pointer.
If on the other hand you had this:
int array2[5];
Then sizeof(array2) would be 20, assuming an int is 4 bytes.

The sizeof operator tells you the size of its operand. array has type int* (pointer to int) which occupies eight bytes on your platform. The sizeof operator cannot find out how long the array array points to actually is. What is returns is not indicative about how much memory has been allocated.
The malloc() function either fails (in which case it returns NULL) or succeeds in which case it returns a pointer to a memory region at least as large as you need it.

Related

Why is sizeof() used in the argument to malloc()? [duplicate]

This question already has answers here:
Is the sizeof operator needed for malloc?
(5 answers)
Closed 8 months ago.
Like this is what the book showed me
int *list = malloc(3 * sizeof(int))
But what's wrong with this?
int *list = malloc(3)
My understanding says that malloc accepts "size" as a parameter and my goal is to only let pointer list to accept 3 values in it, but then why would I include sizeof(int) when using malloc?
Let's look at this line of code:
int *list = malloc(3 * sizeof(int))
It creates a pointer to an int, and allocated three times the size of an int worth of memory for it. So we have enough room to store three ints in that block of memory.
I'll assume one int takes up four bytes of memory. So to store three of them, we need 12 bytes of memory (four bytes per int times three ints). malloc allocates space in bytes, so that will give you 12 bytes of memory (sizeof(int) will return four as there are four bytes per int*).
Now let's look at the other version:
int *list = malloc(3)
You allocate three bytes of memory. Sadly, one int is four bytes... so you have enough space for 3/4 of an int (again, assuming one int is four bytes). If you wanted to store three ints, you need to allocate memory equal to three times however big an int is, hence 3 * sizeof(int).
*Technically, there are platforms where an int isn't four bytes. So it is better to write sizeof(int) instead of 4. Don't worry about that for now, though.
The parameter to malloc() is the number of bytes you want to allocate.
int* list = malloc(3) will only allocate 3 bytes
Each integer on a 32-bit platform is 32 bits (4 bytes). This can be obtained with sizeof(int)
An array of 3 integers will take 3*sizeof(int) which is 12 bytes on a 32-bit platform.
So
int* list = malloc(3*sizeof(int))
will allocate the space for 3 integers, which is 12 bytes on a 32-bit platform

Not getting the actual size of allocated memory to a pointer [duplicate]

This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
Closed 7 years ago.
I am trying to allocate space for 100 integers. But end up with 2 and have not a clue why! I tried both malloc() and calloc but understandingly there is no difference.
Code:
#define MAX 100
int *arr = (int *)calloc(MAX, sizeof(int)); //same with malloc(MAX * sizeof(int))
printf("MAIN before memset: %d | %d\n",
(int)(sizeof(arr)/sizeof(arr[0])), (int)(sizeof(arr)));
if(!arr) {
fprintf(stderr, "A malloc error occured.\n");
exit(1);
}
memset (arr, 0, MAX);
printf("MAIN: %d\n", (int)(sizeof(arr)/sizeof(arr[0])));
OUTPUT:
MAIN before memset: 2 | 8
MAIN: 2
What am I coming short of? I simply want to allocate an array/pointer to 100 integers.
In your code, arr is a pointer, and using sizeof on a pointer gives you the size of the pointer, not the amount of memory allocated to that pointer.
In your case, sizeof(arr) gives you the size of a int * and sizeof(arr[0]) gives you the size of an int, based on your platform and compiler.
That said, you cannot directly get the size of allocated memory from the pointer itself, you need to keep track of it yourself.

Does sizeof return the size of a malloc memory allocation? [duplicate]

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.

C malloc allocated only 8 bytes for int * [duplicate]

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.

size of a pointer allocated by malloc [duplicate]

This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
Closed 9 years ago.
char* pointer;
pointer = malloc (20000);
printf("%d", sizeof(pointer));
//output: 8
I was expecting 20000 for the output since I reserved 20000 bytes with malloc.
But, it returned 8. Why is this happening?
you must be using 64 bit system/OS, thats why it printed 8 for printf("%d", sizeof(pointer));
when you declare char *p; it will reserve space equalto sizeof(char *) in you memory.
now if the system is 64-bit it will reserve 8 bytes or if it is 32-bit then it will reserve 4 bytes.
now
char* pointer;
pointer = malloc (20000);
when you define pointer = malloc(20000) it will reserve a block of 20000 bytes in memory where pointer points to the first byte of that block it doesnt allocates 20000 bytes to pointer.
sizeof returns the size of the type you passed to it.
The type is char * and it just points to a memory location of size 20000.
sizeof is a compile-time operator. It only knows the size of the pointer (here, 8 bytes) and not the size of whatever it points to. sizeof doesn't exist at runtime.
sizeof gives you the size of the pointer variable, not of the area it points to.

Resources