Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle memory allocation in C?

I think I've got a good grasp on how to handle memory in C++ but doing it in C is different I'm a bit off.

In C++ I've got constructors and destructors, I've got the pretty straightforward new and delete and I know how to encapsulate it using RAII, using with smart pointers and within classes.

However in C I can't handle malloc and free the same way. I don't know how to hide them and how to automate things. All I can figure is using functions for initiating and destroying my pointers. But how should I structure my memory handling?

While writing this I've realized this is more a question about me understanding the flow of C than anything else, but one question at a time.

Edit: Thanks for the answers but I need to rephrase myself.

When I say that I use RAII and smart pointers for C++ I don't want the same for C, I know it's not the same. But how I handle memory allocation in C++ is connected to these techniques.

For example in my classes I dynamically add and destroy the memory my class uses. This way I can achieve a sort of encapsulation, I don't need to know when/how/why the class handles it's memory, it just does. This means I can "hide" the lower memory handling and just focus on a few "bigger" classes.

What I want to know is what's the best-practice in handling memory in C? There are no classes with constructors/destructors to handle this for me. Is it good to allocate memory in the beginning of a function or use a function that creates it for me? And how should I free them again?

These are broad questions and they differ from situation to situation but how do you prefer to handle it? What tips and lessons can you give?

like image 764
Jonas Avatar asked Apr 06 '09 19:04

Jonas


People also ask

How is memory allocation done in C?

C malloc() method 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.

Which algorithm is most efficient in memory allocation?

In this problem, the Best-Fit Algorithm makes the most efficient use of memory because it was the only algorithm that meet all the memory requests.

How will you free the allocated memory in C?

Answer: You can use Free() function. Answer: C free() Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own.


2 Answers

Part of the confusion is that it is inherently more difficult in C. malloc and free are similar to new and delete: malloc allocates new memory, and returns a pointer to that memory. free makes that memory available again, so long as it's memory that was allocated using malloc. Otherwise, it just makes hash of some chunk of memory. It doesn't care.

The important thing with malloc/free is to decide on and consistently maintain a disciplined use. Here are some hints:

ALWAYS check the returned pointer from malloc for NULL

if((p = (char *) malloc(BUFSIZ)) == NULL {
   /* then malloc failed do some error processing. */
}

For belt and suspenders safety, set a pointer to NULL after freeing it.

free(p);
p = NULL ;

try to malloc and free a chunk of memory within the same scope if possible:

 {  char * p ;
   if((p = malloc(BUFSIZ)) == NULL {
       /* then malloc failed do some error processing. */
   }

 /* do your work. */

   /* now you're done, free the memory */

   free(p);
   p = NULL ;  /* belt-and suspenders */
 }

When you can't, make it clear that what you're returning is malloc'ed memory, so the caller can free it.

 /* foo: do something good, returning ptr to malloc memory */
 char * foo(int bar) {
     return (char *) malloc(bar);
 }
like image 62
Charlie Martin Avatar answered Oct 14 '22 16:10

Charlie Martin


While writing this I've realized this is more a question about me understanding the flow of C than anything else, but one question at a time.

I honestly think you should read up on K&R if you haven't.

like image 24
Brian Liang Avatar answered Oct 14 '22 16:10

Brian Liang