Reputation: 18702
Let's say you have-
struct Person {
char *name;
int age;
int height;
int weight;
};
If you do-
struct Person *who = malloc(sizeof(struct Person));
How would C know how much memory to allocate for name variable as this can hold a large number of data/string? I am new to C and getting confused with memory allocation.
Upvotes: 8
Views: 5291
Reputation: 76057
It allocates memory for the just the pointer to a char. You need to do a separate allocation for the contents.
There are other options although:
If you are OK with having a fixed sized maximum length, you can do:
struct Person {
char name[PERSON_NAME_MAX_LENGTH+1];
int age;
int height;
int weight;
};
And allocate it as in your example.
Or you can declare a variable sized struct, but I wouldn't recommend this as it is tricky and you cannot have more than one variable size array per struct:
struct Person {
int age;
int height;
int weight;
char name[]; /*this must go at the end*/
};
and then allocate it like:
struct Person *who = malloc(sizeof(struct Person) + sizeof(char)*(name_length+1));
Upvotes: 2
Reputation: 102
Don't assume that the memory storing the pointer for name is the same as the memory storing the data for name. Assuming a 4 byte word size, you have the following:
char * (4 bytes)
int (4 bytes)
int (4 bytes)
int (4 bytes)
================
total: 16 bytes
which is: sizeof(char*) + sizeof(int) + sizeof(int) + sizeof(int)
. C knows the size because you've told it the size of the elements in the struct definition.
I think what you are confused about is the following:
The contents at the char *
will be a memory location (e.g. 0x00ffbe532
) which is where the actual string will be stored. Don't assume that the struct contents are contiguous (because of the pointer). In fact, you can be pretty sure that they won't be.
So, to reiterate, for an example struct Person
(this is just an example, the locations won't be the same in a real program.)
location : [contents]
0x0000 : [0x00ffbe532]
0x0004 : [10]
0x0008 : [3]
0x000C : [25]
0x00ffbe532 : [I am a string\0]
Upvotes: 4
Reputation: 43498
The name
member is just a pointer. The size of a pointer varies with the underlying architecture, but is usually 4 or 8 bytes nowadays.
The data that name
can point to (if assigned later) should be laid out in an area that does not coincide with the struct
at all.
At the language level, the struct
doesn't know anything about the memory that the name
member is pointing to; you have to manage that manually.
Upvotes: 2
Reputation: 4770
Pointer members occupy one word in my experience (the address on witch the data actually resides) so the size will probably be 16 bytes (assuming one word is 4 bytes)
As the man said above you need to separately allocate memory for *name witch will free memory someplace else for the size you desire
Upvotes: 0
Reputation: 22956
For pointers the struct is allocated enough memory just for the pointer. You have to create the memory for the char* and assign the value to the struct. Assuming you had char* name
somewhere:
struct Person *who = malloc(sizeof(struct Person));
who->name = malloc((strlen(name)+1) * sizeof(char));
strcpy(who->name, name)
Upvotes: 1
Reputation: 206508
It won't know, You will have to allocate memory for it separately.
struct Person *who = malloc(sizeof(struct Person));
Allocates enough memory to store an object of the type Person
.
Inside an Person
object the member name
just occupies a space equivalent to size of an pointer to char
.
The above malloc
just allocates that much space, to be able to do anything meaningful with the member pointer you will have to allocate memory to it separately.
#define MAX_NAME 124
who->name = malloc(sizeof(char) * MAX_NAME);
Now the member name
points to an dynamic memory of size 124
byte on the heap and it can be used further.
Also, after your usage is done you will need to remember to free
it explicitly or you will end up with a memory leak.
free(who->name);
free(who);
Upvotes: 4
Reputation: 33817
It will allocate 4 bytes for the name
pointer, but no space for the "real" string. If you try to write there you will seg fault; you need to malloc
(and free
) it separately.
Using string
(in C++) can save you some headache
Upvotes: 1
Reputation: 3295
You have a pointer to a name character array in your struct, ie. it will consume that much bytes that are needed to represent a memory address.
If you want to actually use the field, you must allocate additional memory for it.
Upvotes: 0
Reputation: 6809
It doesn't. You have to do that too.
struct Person *who = malloc(sizeof(struct Person));
who->name = malloc(sizeof(char) * 16); /* for a 15+1 character array */
Upvotes: 0
Reputation: 2913
I think you should also need to allocate memory for the name attribute
Upvotes: -2