Reputation: 705
Hello i'm not sure if I'm understand the following piece of code. I would glad if someone could read my explanations and correct me if I'm wrong.
So first of all I'm declaring a struct with three arrays of char and an integer.
struct Employee
{
char last[16];
char first[11];
char title[16];
int salary;
};
After that I declare a function which takes three pointers to char and an integer value. This function uses malloc() and sizeof() to create a struct on the heap. Now this creations of the object on the heap is not really clear to me. When I use struct Employee* p = malloc(sizeof(struct Employee))
, what happens there exactly?
What happens when I use the function struct Employee* createEmployee (char* last, char* first, char* title, int salary)
several times with different input. I know that I will get back a pointer p but isn't that the same pointer to the same struct on the heap. So do I rewrite the information on the heap, when I use the function several times? Or does it always create a new object in a different memory space?
struct Employee* createEmployee(char*, char*, char*, int);
struct Employee* createEmployee(char* last, char* first, char* title, int salary)
{
struct Employee* p = malloc(sizeof(struct Employee));
if (p != NULL)
{
strcpy(p->last, last);
strcpy(p->first, first);
strcpy(p->title, title);
p->salary = salary;
}
return p;
}
I would be glad if someone could explain it to me. Thank you very much.
Upvotes: 1
Views: 260
Reputation: 999
malloc
assigns you a block of memory equal to its first argument, in this case the size of the Employee.
Every time you call createEmployee
, you call malloc
a separate time, and every time you call malloc
, it gives you a fresh piece of memory.
This is what allows you to have different employees: if they all used the same memory, you would only be able to create one.
This is why calling free
, and freeing that memory is important: the operating system has no other way of knowing whether you're using the memory or not.
If you want to edit an existing employee
, maintain a pointer reference to it, and add a strcpy(p->title, newTitle);
to change its title to newTitle.
Also, something that has been mentioned, strcpy
is dangerous, as it will continue to write its strings regardless of whether it has exceeded the 11 characters allotted for it.
Upvotes: 1
Reputation: 72241
The malloc
function allocates some new bytes on the heap and returns the pointer.
So the createEmployee
function allocates new memory every time it's called, then fills it with some data (in an unsafe way - consider using strncpy
instead) and returns the pointer to that memory. It will return a different pointer every time it's called.
Each instance you create with this function will exist as long as you don't call free
on its pointer.
Upvotes: 2
Reputation: 43472
Every time you call malloc()
, you're telling it to give you a new chunk of memory, at least as long as you've asked for, not currently in use anywhere else. So the following gives you three different pointers:
void *p1 = malloc(100);
void *p2 = malloc(100);
void *p3 = malloc(100);
It's like hitting a button on a vending machine. Each time, you get a different candy bar that conforms to your requests ("Caramilk" for instance.)
Upvotes: 0
Reputation: 54325
Your first question is a question about malloc. You might get better results searching for "How does malloc work?" The answer is different for different operating systems and C libraries.
The createEmployee function creates an all-new struct Employee every time it is called.
I also see that createEmployee is written in a very dangerous way. No checking is done to ensure that the strings fit into their destinations before calling strcpy. This is how buffer overflows are created.
Upvotes: 1