I have been working on the following code for a while and there is a problem when I tried to allocate/deallocate memory for a struct and its elements. Any insights into the problem would be greatly appreciated thanks. Looking at the error message, I believe the problem lies in the fact that I tried to free an element that I had not properly allocated memory for but it is not obvious to me while looking at the code. I also tried code where I did not individually allocate memory for each element of the struct but that did not work as well.
typedef struct {
char *cnet;
char *email;
char *fname;
char *lname;
char *tel;
} vcard;
vcard *vcard_new(char *cnet, char *email, char *fname, char *lname, char *tel)
{
vcard* new = (vcard*)malloc(sizeof(vcard));
printf("%lu\n", sizeof(new->tel) );
new->cnet = malloc(sizeof(new->cnet));
new->email = malloc(sizeof(new->email));
new->fname = malloc(sizeof(new->fname));
new->lname = malloc(sizeof(new->lname));
new->tel = malloc(sizeof(new->tel));
new->cnet = cnet;
new->email = email;
new->fname = fname;
new->lname = lname;
new->tel = tel;
return new;
}
/* vcard_free : free vcard and the strings it points to
*/
void vcard_free(vcard *c)
{
free(c->cnet);
free(c->email);
free(c->fname);
free(c->lname);
free(c->tel);
free(c);
return;
}
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.
Sometimes there just isn't enough memory, meaning that malloc isn't guaranteed to return a pointer to usable memory. If it isn't able to allocate memory, it will return a null pointer, NULL .
Memory allocation errors are due to your Operating System not having the resources to allocate any more memory for the simulation.
Your entire memory allocation is erroneous. Here are some pointers.
You allocate memory for only one char *
, which is not what is intended.
Then, you overwrite the allocated memory by assigning the parameters to the same variables which held the pointer to allocated memories.
malloc()
and family.
free()
, causes undefined behavior
printf()
sizeof
yields a result of type size_t
, you must use %zu
to print the result.Solutions:
Allocate enough memory to store the expected content, like in predefined sizes
#define CNETSIZ 32
#define EMAILSIZ 64
. . . . .
new->cnet = malloc(CNETSIZ);
new->email = malloc(EMAILSIZ);
or, based on the length of the input string, like
new->cnet = malloc(strlen(cnet)+1); //+1 for the space to null-terminator
Inside the vcard_new()
function, use strcpy()
to copy the content from the function parameters, like
strcpy(new->cnet, cnet);
strcpy(new->email, email);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With