Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory allocation eror in C

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;
}
like image 287
physicsbot123 Avatar asked May 10 '19 06:05

physicsbot123


People also ask

What is memory allocation in C language?

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.

What happens if you don't allocate memory in C?

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 .

What is an allocation error?

Memory allocation errors are due to your Operating System not having the resources to allocate any more memory for the simulation.


1 Answers

Your entire memory allocation is erroneous. Here are some pointers.

  1. You allocate memory for only one char *, which is not what is intended.

    • Lesser memory allocation, possibility of boundary overrun.
  2. Then, you overwrite the allocated memory by assigning the parameters to the same variables which held the pointer to allocated memories.

    • You end up causing memory leak.
  3. You try to free the pointers which are not returned by malloc() and family.
    • Invalid attempt to free(), causes undefined behavior
  4. You have used wrong format specifier in printf()
    • sizeof yields a result of type size_t, you must use %zu to print the result.

Solutions:

  1. 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
    
  2. Inside the vcard_new() function, use strcpy() to copy the content from the function parameters, like

    strcpy(new->cnet, cnet);
    strcpy(new->email, email);
    
like image 187
Sourav Ghosh Avatar answered Oct 05 '22 23:10

Sourav Ghosh