0

I'm taking an Operating Systems class in C for my Computer Science major.

I am curious if whether or not this has to do with any wasted space when memory is allocated and if whether or not this varies on any computer operating system, and I understand that how memory is arranged can be done so in any order best for the system.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    The heap memory typically has to keep track of sizes of blocks of memory, and where the next block of memory in the used or free list is. So, if you allocate 1 bytes of heap memory, there could be 16-32 bytes allocated in total, depending on platform and implementation. By contrast, if you allocate 1 byte of stack memory, it might need 4 bytes to maintain alignment (perhaps, but probably not, 8 bytes). – Jonathan Leffler May 12 '19 at 07:10
  • You can see that the kernels use different memory management functions than userspace programs. Your question is much too broad to answer - but automatic storage objects have limited to the scope lifetime and stack (especially the kernel one is very limited in size) – 0___________ May 12 '19 at 07:11
  • You need to cite a reference to back up the premise and provide some context. For example are you talking about the overall allocation for each, or the space required to store each individual object? – Clifford May 12 '19 at 07:46
  • Your class will presumably require you to write a heap manager? Then you'll see why. – Clifford May 12 '19 at 07:55

1 Answers1

1

Heap allocations have maximal alignment - typically 8 bytes, so individual allocations have up to 7 bytes "wasted" to ensure alignment.

Then each allocation includes a block of metadata for heap management.

Further in some implementations or debug builds, additional metadata or padding may be included to detect heap errors such as attempts to free non-heap objects or block overrun.

Clifford
  • 88,407
  • 13
  • 85
  • 165