-2

Note: This is a hypothetical situation I am creating to understand the functioning of program and malloc in C/C++.

Let us say that I allocated 10k integers and they all got allocated contiguously in the memory. Now, I free every alternate element in these 10k integers. Now, I want to allocate a long integer which requires 8 bytes. Since none of the above 5k locations have 8 bytes contiguously, does the program allocate this variable a new memory location? If in future I am going to use only > 4 byte locations then am I just wasting this entire memory? Or does the compiler take necessary steps so that these remaining 5k locations will be contiguous later?

EDIT: To those who marked this as a duplicate. Okay, let us say that it uses paging and it is taking care of fragmentation by remapping the pages to contiguous blocks in physical memory. Even then, it would mean that in virtual memory all those address locations are no longer usable right? So, those memory locations are not usable? Yes I understand that this is a problem of fragmentation etc. My question is the following:

Is there any way the run-time program might know that this issue arises and somehow try and manage its resources in a good way? Since C is a totally compiled language, I don't think this is possible. Does JAVA or C# run-time environment do things like this? Reorganizing their objects such that they take up a contiguous block of memory in their heap space? And if that happens, do they have to change values of every referencing variable? Since their position on the heap (and effectively their address) is changing?

Aditya T
  • 30
  • 5
  • How did you allocate these integers? – juanchopanza Sep 05 '14 at 06:08
  • 3
    *they all got allocated contiguously in the memory.* How do you know that? – Yu Hao Sep 05 '14 at 06:10
  • You may find [**this question**](http://stackoverflow.com/questions/3770457/what-is-memory-fragmentation) worth reading. – WhozCraig Sep 05 '14 at 06:11
  • An easy answer is to tell you that you are doing it wrong. If you allocate many instances of the same size, then use a memory pool, not malloc/new. – kay Sep 05 '14 at 06:17
  • Why do you care if they are contiguous? – Cody Gray Sep 05 '14 at 06:18
  • 1
    To answer your new question, yes. This is what's known as a garbage collector. There are many different types of garbage collection algorithms, but compaction is very commonly implemented. You can write code in C to do this as well. But you cannot use raw pointers, because like you said, their address would change. You have to use indirect references. The garbage collector would maintain a mapping table to map those references into pointers to actual memory locations. – Cody Gray Sep 08 '14 at 05:37

1 Answers1

4

The general answer to your question is that you don't know that. The C specification leaves it entirely open to the libc implementation how malloc will work. It could, if it wanted to, allocate each of those 10k integers at completely random locations in memory, in which case there is certainly many gaps left to fill with longs without allocating new pages.

There are, in fact, even real-world examples of malloc implementations acting somewhat like that. You may find it interesting to read about, for example, worst fit allocators.

Another thing that you may want to keep in mind is that many current malloc implementations have a minimum amount of memory they will allocate in one call. On x86-64 glibc, this amount is 24 bytes, so whether you call it to allocate 4 or 8 bytes, 24 bytes will still be allocated. Freeing such a 4-byte malloc, therefore, still enables an 8-byte malloc to fill the same space.

Dolda2000
  • 23,371
  • 2
  • 45
  • 83