Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does free always (portably) frees & reserve memory for the process or returns to the OS

Tags:

c

malloc

I have read that free() "generally" does not return memory to the OS. Can we portably make use of this feature of free(). For example,is this portable?

 /* Assume I know i would need memory equivalent to 10000 integers at max
    during the lifetime of the process */

 unsigned int* p = malloc(sizeof(unsigned int) * 10000);

 if ( p == NULL)
  return 1;

 free(p);

 /* Different points in the program */

 unsigned int* q = malloc(sizeof(unsigned int) * 5);

 /* No need to check for the return value of malloc */

I am writing a demo where I would know in advance how many call contexts to support.

Is it feasible to allocate "n" number of "call contexts" structures in advance and then free them immediately. Would that guarantee that my future malloc calls would not fail?

Does this give me any advantage with regards to efficiency? I am thinking one less "if" check plus would memory management work better if a large chunk was initially acquired and is now available with free. Would this cause less fragmentation?

like image 211
Aditya Sehgal Avatar asked Jun 21 '10 18:06

Aditya Sehgal


People also ask

What happens if you don't use free ()?

If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).

How does free () work in deallocating memory?

The free() function is used to deallocate memory while it is allocated using malloc(), calloc() and realloc(). The syntax of the free is simple. We simply use free with the pointer. Then it can clean up the memory.

When should I use free () in C?

“free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.

Do you have to free calloc?

After you've allocated memory, you must free it. No exceptions. Or get a memory leak otherwise. Using free() is always a good idea to increase heap re-usability.


2 Answers

You would be better off keeping the initial block of memory allocated then using a pool to make it available to clients in your application. It isn't good form to rely on esoteric behaviors to maintain the stability of your code. If anything changes, you could be dealing with bad pointers and having program crashes.

like image 108
Amardeep AC9MF Avatar answered Oct 26 '22 12:10

Amardeep AC9MF


You are asking for a portable and low level way to control what happens on the OS side of the memory interface.

On any OS (because c is one of the most widely ported languages out there).

Think about that and keep in mind that OSs differ in their construction and goals and have widely varying sets of needs and properties.

There is a reason the usual c APIs only define how things should look and behave from the c side of the interface, and not how things should be on the OS side.

like image 33
dmckee --- ex-moderator kitten Avatar answered Oct 26 '22 12:10

dmckee --- ex-moderator kitten