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 );
Related
What is the difference between the following two statements?
ptr = malloc (400);
and
ptr = malloc (100 * sizeof(int))
How does it work? Is there any difference between them?
types of ptr 'int'
It depends on your architecture. On a 64-bit machine, the int size should be 8 bytes and 4 bytes on a 32-bit. Though it is not a rule, your 64-bit compiler might register having 4 bytes instead.
So the difference is that the allocated memory might vary depending on the architecture you are targeting and what the compiler decides.
This is also true for the size of your pointers.
No there should not be any. Malloc allocates contiguous memory chunk in bytes at runtime, so as long as integer takes 4 bytes, then no problem.
You can also refer this, for more clarity.
Also since you did not mention the type of pointer you are using .. the 2 ways makes no difference. Suppose you wanted an integer type then here is an example:
int *ptr;
ptr = malloc(100 * sizeof *ptr);
The first form allocates 400 bytes.
The second form allocates enough space for 100 int objects.
These are not necessarily the same thing, as the size of an int can be 2, 4, or more bytes wide depending on the platform.
If you need to set aside space for N objects of a given type, use the following idiom:
T *ptr = malloc( N * sizeof *ptr ); // for any object type T
sizeof *ptr is equivalent to sizeof (T).
This is my code.
#include<stdio.h>
typedef struct {
int a,b;
} integers;
void main() {
integers *ptr = (integers *)malloc(10*sizeof(integers));
printf("%d",sizeof(*ptr)); // prints 8
}
From what I understand about Malloc, the above code should actually reserve 10x8=80 bytes of memory for ptr to point to.
Why then does using sizeof(*ptr) give only 8? How do I find the total size being allocated for ptr?
Because you're using sizeof(*ptr) you're actually asking for the size of the first element in the allocated buffer, thus sizeof will return the size of the first element in ptr (i.e. 2x4 bytes integers on 32bits system) rather than the allocated size.
Also, please note that even if you'd use sizeof(ptr) you'd get the size of the ptr pointer which on 32bits system would be 4 bytes.
Well, I know that this question is pretty outdated but finding no suitable answer, I decided to write one.
When specifying sizeof(*ptr), you're actually trying to reach out for the size of data type you've stored in the variable that the pointer is pointing to( here its the first element of the array). Here, that's quite evidently 8.
Even when you'll try to print sizeof(ptr), you'll be again printing the size of the pointer address which by default is 8 bytes in GCC compilers as the data type is long unsigned int.
Why then does using sizeof(*ptr) give only 8? How do I find the total size being allocated for ptr?
The type of the expression *ptr is integers - thus,
sizeof *ptr == sizeof (integers) == sizeof (int) + sizeof (int)
You cannot determine the size of the allocated buffer by looking at the pointer (it doesn't store any metadata about the buffer size). You will have to keep track of that information separately.
Edit
Note that you can do something like the following:
integers (*foo)[10] = malloc( sizeof *foo );
if ( foo )
printf( "sizeof *foo = %zu\n", sizeof *foo );
and that will give you the result you're expecting. In this case, foo is a pointer to an array of integers, not to a single instance of integers, so sizeof *foo will give you the size of the allocated array. The downside is that you have to expliticly dereference foo before applying the subscript:
(*foo)[i].a = some_value(); // or foo[0][i].a
(*foo)[i].b = some_other_value(); // or foo[0][i].b
This is normally done when you want to allocate an NxM array and make sure all the rows are contiguous:
integers (*foo)[10] = malloc( 10 * sizeof *foo );
will allocate a 10x10 array of integers such that the rows are all adjacent in memory.
Also, a pointer to a 10-element array of integers is not compatible with a pointer to an 11-element array of integers, making it more difficult to write functions that can work with pointers to arrays of different sizes. IOW, if you have a function declared as
void bar( integers (*foo)[10] ) { ... }
it can only work with Nx10 arrays of integers. There are ways around this that involve varying levels of pain, but that's a topic for another day.
I have this in my code:
int x = 4;
char* array = malloc(x*sizeof(char));
size_t arraysize = sizeof (array);
printf("arraysize: %zu\n", arraysize);
This code prints out,
arraysize: 8
Why is it 8 and not 4? (Since 4*sizeof(char) = 4 * 1)
array is a pointer in your code. sizeof(array) therefore returns the size of that pointer in C bytes (reminder: C's bytes can have more than 8 bits in them).
So, 8 is your pointer size.
Also, the correct type specifier for size_t in printf()'s format strings is %zu.
array is a pointer, hence it will be size of a pointer(sizeof(void *) or sizeof(char *)), and not sizeof(char) as you might expect. It seems that you are using 64bit computer.
sizeof doesn't have a return value because it isn't a function, it's a C language construct -- consider the fact that you can write sizeof array without the parentheses. As a C language construct, its value is based entirely on compile-time information. It has no idea how big your array is, only how big the array pointer variable is. See http://en.wikipedia.org/wiki/Sizeof for complete coverage of the subject.
sizeof(array) returns the size of a pointer.
You are calculating the size of the full array (4*sizeof(char))
But you spect to know the number of items on the array (4).
You can do
size_t arraysize = sizeof (array)/sizeof(char);
It will return 8/2 = 4
What form is correct in allocating string in C?
char *sample;
sample = malloc ( length * sizeof(char) );
or
sample = malloc ( length * sizeof(char*) );
Why does char* take 4 bytes when char takes 1 byte?
Assuming the goal is to store a string of length characters, the correct allocation is:
sample = malloc(length + 1);
Notes:
Don't use sizeof (char), since it's always 1 it doesn't add any value.
Remember the terminator, I assumed (based on name) that length is the length in visible characters of the string, i.e. the return of strlen() will be length.
I know you didn't, but it's worth pointing out that there should be no cast of the return value from malloc(), either.
The reason char * is larger is that it's a pointer type, and pointers are almost always larger than a single character. On many systems (such as yours, it seems) they are 32 bit, while characters are just 8 bits. The larger size is needed since the pointer needs to be able to represent any address in the machine's memory. On 64-bit computers, pointers are often 64 bits, i.e. 8 characters.
Why does char* take 4 bytes when char takes 1 byte?
Because you are on a 32-bit systems, meaning that pointers take four bytes; char* is a pointer.
char always takes exactly one byte, so you do not need to multiply by sizeof(char):
sample = malloc (length);
I am assuming that length is already padded for null termination.
sample = malloc ( length * sizeof(char) );
First is the correct one if you want to allocate memory for length number of characters.
char* is of type pointer which happens to be 4 bytes on your platform. So sizeof(char*) returns 4.
But sizeof(char) is always 1 and smae is guaranteed by the C standard.
In the given cases you are doing two different things:
In the first case : sample = malloc ( length * sizeof(char) );
You are allocating length multiplied by the size of type char which is 1 byte
While in the second case : sample = malloc ( length * sizeof(char*) );
You are allocating length multiplied by the size of pointer to char which is 4 byte
on your machine.
Consider that while case 1 remains immutable, on the second case the size is variable.
sample = malloc(length);
is the right one
char* is a pointer, a pointer uses 4 bytes (say on a 32-bit platform)
char is a char, a char uses 1 byte
In your case, you want to alloc an array of length characters. You will store in sample a pointer to an array of length times the size of what you point to. The sizeof(char*) is the size of a pointer to char. Not the size of a char.
A good practice is
sample = malloc(length * sizeof(*sample));
Using that, you will reserve length time the size of what you want to point to. This gives you the ability to change the data type anytime, simply declaring sample to be another kind of data.
int *sample;
sample = malloc(length * sizeof(*sample)); // length * 4
char *sample;
sample = malloc(length * sizeof(*sample)); // length * 1
Provided the length already accounts for the nul terminator, I would write either:
sample = malloc(length);
or:
sample = malloc(length * sizeof(*sample));
sizeof(char*) is the size of the pointer, and it is completely irrelevant to the the size that the allocated buffer needs to be. So definitely don't use that.
My first snippet is IMO good enough for string-manipulation code. C programmers know that memory and string lengths in C are both measured in multiples of sizeof(char). There's no real need to put a conversion factor in there that everybody knows is always 1.
My second snippet is the One True Way to write allocations in general. So if you want all your allocations to look consistent, then string allocations should use it too. I can think of two possible reasons to make all your allocations look consistent (both fairly weak IMO, but not actually wrong):
some people will find it easier to read them that way, only one visual pattern to recognise.
you might want to use the code in future as the basis for code that handles wide strings, and a consistent form would remind you to get the allocation right when the length is no longer measured in bytes but in wide chars. Using sizeof(*sample) as the consistent form means you don't need to change that line of code at all, assuming that you update the type of sample at the same time as the units in which length is measured.
Other options include:
sample = calloc(length, 1);
sample = calloc(length, sizeof(char));
sample = calloc(length, sizeof(*sample));
They're probably fairly pointless here, but as well as the trifling secondary effect of zeroing the memory, calloc has an interesting difference from malloc that it explicitly separates the number and size of objects that you're planning to use, whereas malloc just wants the total size.
For any type T, the usual form is
T *p = malloc(N * sizeof *p);
or
T *p;
...
p = malloc(N * sizeof *p);
where N is the number of elements of type T you wish to allocate. The expression *p has type T, so sizeof *p is equivalent to sizeof (T).
Note that sizeof is an operator like & or *, not a library function; parentheses are only necessary if the operand is a type name like int or char *.
Please visit this Linkhttps://www.codesdope.com/c-dynamic-memory/for understand how it allocat the memory dynamically at run time. It might be helpful to understand the concept of malloc and how it allocate the amount of memory to the variable.
In your example;
char *sample;
sample = malloc ( length * sizeof(char) );
here, you are declare a pointer to character for sample without declaring how much memory it required. In the next line, length * sizeof(char) bytes memory is assigned for the address of sample and (char*) is to typecast the pointer returned by the malloc to character.
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 );