Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How is memory allocated using malloc() ? Who allocates the memory OS or the compiler? Once the memory is freed using free() can it be used by other processes ?
In an OS there are 4 memory regions Heap,Stack,Text and Data. When you use malloc the OS provides the memory from the heap region. Compiler isn t responsible for allocating this memory. When you use free the memory block is returned back to the heap.
Usually, the heap memory is directly supplied by a runtime sub-allocator underneath whatever the OS provides. The sub-allocator is process-specific and does not require a kernel call. If the heap needs more, it has to resort to a syscall to get another chunk from the OS.
It's implementation-specific whether the sub-allocator ever releases chunks back to the OS.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
My understanding of mmap is that when used on a file it essentially reserves space for that file in memory so that it's able to access it as fast as possible as soon as you need it. but what happens when you mmap a device like dev/mem where it IS the memory, does it then use some other memory to map that memory or is it smart enough to realize that it is mapping ram and doesn't need to store it in memory? What about if you map a RAM disk where it is still memory but it's not grouped in with the regular memory?
/dev/mem is the physical memory. It won't double your address space, it will add the amount of physical memory your machine has to your address space, but your actual memory usage will not go up.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
After searching around for a bit on how deallocation (via free) after allocation (via something like malloc) works, I'm left puzzled.
Reading around, sources say that when memory is allocated, it actually has one word more allocated which stores the size of the allocation. This is what free uses to deallocate the memory. But with this information arises another question which I can not find the answer to anywhere: If the size of memory allocated is stored somewhere, why is there not a function or method in C that can give us the size of memory allocated via something like malloc?
The C standard only specifies the functions needed to allocate and free memory. This means that how the heap is managed is a implementation detail of the standard library on your system. How Linux does it is likely very different from Microsoft's implementation.
That being said, sometimes these implementations expose additional functions that expose the internal state of the heap. For example, Linux has a function called malloc_usable_size which is passed a pointer to allocated memory and returns the number of bytes that may be written at that address.
Still, it's best not to depend on system specific functions like that. Your program should have some way of keeping track of how much space a malloc'ed block of memory points to.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is maybe a more general question. I have a pointer in my code to store some data. The size of the pointer is determined only while the program is executed and needs to be allocated dynamically.
So I'm using
calloc()
to allocate memory and set it to zero. After the run of the program I'm using
free()
to free it.
Is this usage of memory in a good manner? Or is there something more "nice" one could do?
For dynamic memory allocation, the steps you had mentioned is correct, as #Stargateur pointed use malloc(), if you don't need to initialize the memory allocated.
Also take care to free the allocated memory, on all possible exit condition of the program.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm currently wroking on a "memory project" at school, more precisely about dynamic memory allocation. My problem is about the Heap management.
I don't really understand the difference between memory pages and memory blocks. Please correct me if i'm wrong : The heap contains some unmapped map. When we try to allocate some memory, the asked size becomes a mapped region of the heap that we can use.
This new region seems to contain some "memory pages" of 4096 Bytes but i don't understand where the memory "blocks" are...
Memory pages are a term used in virtual memory management. It is the smallest unit addressable by the MMU (Memory Management Unit), which converts virtual addresses (to logical addresses, on x86, and) to physical addresses. For more information on pages and virtual memory management, read how x86 paging works.
Memory blocks are not that tightly tied to a certain topic. They can refer to virtually everything and are used when referring to memory colloquially (in case anybody talks OSes colloquially).
As far as I can tell, they refer to the blocks allocated by a user on the free store a.k.a. heap here. They are the memory area allocated by users of an API providing functionality to access a heap (like the C standard library with malloc, free, etc.).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I wonder what happens to the memory that is used in functions. I am writing a multi-threaded program and I wonder what happens if I just call a functions and its memory after it returns.
"Automatic" storage -- variables you declare directly rather than explicitly allocating from the heap -- is obtained from the stack, and essentially goes away when the function exits.
Anything you explicitly malloc() MUST eventually be explicitly free()d, once and only once. It's your responsibility to structure your code so that happens correctly. If you don't intend to use it after the function exits, you should free it before the function exits. If it's part of a larger data structure, or being returned to the caller, you need to design your program to be aware of this and clean up after itself when that block of memory is no longer needed.
If you allocate, you must free -- or must document clearly whose responsibility it is to free the memory when they're done with it.
(Note that this is very different from Java and other "garbage-collected" languages, where memory is automatically recovered when nobody is actively using it.)