size of a pointer allocated by malloc [duplicate] - c

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.

Related

Global dynamic Array of Structs - in C [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
newbie questions about malloc and sizeof
I am trying to read strings into a program. When I noticed that the strings were sometimes being corrupted, I tried the following code:
void *mallocated = malloc(100);
printf("sizeof(mallocated) = %d\n", sizeof(mallocated));
According to my program, the size of mallocated was 8, even though I allocated 100 bytes for it. Because of this, whenever I try to store a string longer than 8 bytes, everything after the 8th byte will sometimes disappear. Why is this happening, and how can I prevent it?
Because the size of the "string" pointer is 8 bytes. Here are some examples of using sizeof() with their appropriate "size". The term size_of() is sometimes deceiving for people not used to using it. In your case, the size of the pointer is 8 bytes.. below is a representation on a typical 32-bit system.
sizeof (char) = 1
sizeof (double) = 8
sizeof (float) = 4
sizeof (int) = 4
sizeof (long) = 4
sizeof (long long) = 8
sizeof (short) = 2
sizeof (void *) = 4
sizeof (clock_t) = 4
sizeof (pid_t) = 4
sizeof (size_t) = 4
sizeof (ssize_t) = 4
sizeof (time_t) = 4
Source
You are leaving out how you are determining your string is disappearing (char array). It is probably being passed to a function, which you need to pass the explicit length as a variable or track it somewhere. Using sizeof() won't tell you this.
See my previous question about this and you'll see even my lack of initial understanding.
In C89, sizeof operator only finds the size of a variable in bytes at compile time (in this case a void pointer of 8 bytes). It works the way you'd expect it to work on plain arrays, because their size is known at compile time.
char arr[100]; // sizeof arr == 100
char *p = arr; // sizeof p == 4 (or 8 on 64-bit architectures)
char *p = malloc(100); // sizeof p == 4 (or 8). Still!
To know the size of heap-allocated memory, you need to keep track of it manually, sizeof won't help you.
sizeof returns the size of the pointer (void *) that you gave it, not the size of the memory you allocated. You would have to store the size of the memory in a separate variable if you want to use it later.
You cannot. As pointed out, you can get the size of the void * mallocated, but that does not tell you much.
You cannot get the size of the malloed *mallocated. That is to say, there is no function call to return 100 (in your example) - unless you write your own memory management routines.
Simplest is just to remember it somewhere ... maybe ....
stuct {
void *data;
unsigned int size
} myStructureWhichRemebersItsSize;
myStructureWhichRemebersItsSize *someData;
someData.data = malloc(100); // and check for NULL
someData.size = 100;
void* is the type of a location in memory if you don't know what it contains. It should be avoided.
char* is the type of a value which points to some location in memory which holds a char. Identifying a location in memory takes eight bytes.
sizeof tells you how many bytes a particular type takes. Not how many were allocated with malloc but just how much memory compiler knows the type should take. Applying sizeof to values is often considered bad style, and as someone else mentioned here, sometimes invokes smart behavior in C99.
char[100] the type of a value which holds 100 chars. char[100] a; is a string of 100 chars on the stack.
char(*)[100] is the type of a pointer to a value that holds 100 chars. char(*b)[100]; makes b point to 100 chars, possibly on the heap. What you probably want is
char (*s)[100] = malloc( sizeof( char[100] ) );
printf( "%u byte pointer to %u bytes of size %u elements\n",
sizeof s, sizeof *s, sizeof **s );

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

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.

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.

Using sizeof() on malloc'd memory [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
newbie questions about malloc and sizeof
I am trying to read strings into a program. When I noticed that the strings were sometimes being corrupted, I tried the following code:
void *mallocated = malloc(100);
printf("sizeof(mallocated) = %d\n", sizeof(mallocated));
According to my program, the size of mallocated was 8, even though I allocated 100 bytes for it. Because of this, whenever I try to store a string longer than 8 bytes, everything after the 8th byte will sometimes disappear. Why is this happening, and how can I prevent it?
Because the size of the "string" pointer is 8 bytes. Here are some examples of using sizeof() with their appropriate "size". The term size_of() is sometimes deceiving for people not used to using it. In your case, the size of the pointer is 8 bytes.. below is a representation on a typical 32-bit system.
sizeof (char) = 1
sizeof (double) = 8
sizeof (float) = 4
sizeof (int) = 4
sizeof (long) = 4
sizeof (long long) = 8
sizeof (short) = 2
sizeof (void *) = 4
sizeof (clock_t) = 4
sizeof (pid_t) = 4
sizeof (size_t) = 4
sizeof (ssize_t) = 4
sizeof (time_t) = 4
Source
You are leaving out how you are determining your string is disappearing (char array). It is probably being passed to a function, which you need to pass the explicit length as a variable or track it somewhere. Using sizeof() won't tell you this.
See my previous question about this and you'll see even my lack of initial understanding.
In C89, sizeof operator only finds the size of a variable in bytes at compile time (in this case a void pointer of 8 bytes). It works the way you'd expect it to work on plain arrays, because their size is known at compile time.
char arr[100]; // sizeof arr == 100
char *p = arr; // sizeof p == 4 (or 8 on 64-bit architectures)
char *p = malloc(100); // sizeof p == 4 (or 8). Still!
To know the size of heap-allocated memory, you need to keep track of it manually, sizeof won't help you.
sizeof returns the size of the pointer (void *) that you gave it, not the size of the memory you allocated. You would have to store the size of the memory in a separate variable if you want to use it later.
You cannot. As pointed out, you can get the size of the void * mallocated, but that does not tell you much.
You cannot get the size of the malloed *mallocated. That is to say, there is no function call to return 100 (in your example) - unless you write your own memory management routines.
Simplest is just to remember it somewhere ... maybe ....
stuct {
void *data;
unsigned int size
} myStructureWhichRemebersItsSize;
myStructureWhichRemebersItsSize *someData;
someData.data = malloc(100); // and check for NULL
someData.size = 100;
void* is the type of a location in memory if you don't know what it contains. It should be avoided.
char* is the type of a value which points to some location in memory which holds a char. Identifying a location in memory takes eight bytes.
sizeof tells you how many bytes a particular type takes. Not how many were allocated with malloc but just how much memory compiler knows the type should take. Applying sizeof to values is often considered bad style, and as someone else mentioned here, sometimes invokes smart behavior in C99.
char[100] the type of a value which holds 100 chars. char[100] a; is a string of 100 chars on the stack.
char(*)[100] is the type of a pointer to a value that holds 100 chars. char(*b)[100]; makes b point to 100 chars, possibly on the heap. What you probably want is
char (*s)[100] = malloc( sizeof( char[100] ) );
printf( "%u byte pointer to %u bytes of size %u elements\n",
sizeof s, sizeof *s, sizeof **s );

Resources