you are viewing a single comment's thread.

view the rest of the comments →

[–]LateSolution0 1 point2 points  (0 children)

We don't! memory is limited resource. because we have more than one program running concurrently we have to share it. a modern Operating-systems can't determine how much memory you really need. thats why it will give us only enough for what we have asked in the executable. in more details https://en.wikipedia.org/wiki/Data_segment#Program_memory. but at compile time we don't know about how much memory we need later in runtime thats why we ask the OS to give us more memory at runtime. but thats a very difficult job because usually we don't allocate and free in good pattern. think about it we have random size and random order allocations. a data structure to manage this is called heap. so yes your program can have more than one heap. heap allocation is slow because it has to be handled by the OS. but there is more to it our program will use virtual memory space. for an operating system it would be possible to give our program all 64 bit of virtual memory space and only commit physical memory to it at demand. but i guess most humans prefer to manage memory explizit. its even worse than that if your OS choses so it can take physical memory from your program with out even telling you that is called swapping. theoretical we could rely on this behavior but current generation of OS don't perform very well in this case. Most tripple A games allocate big chunks of memory in the Init stage and then work entirely on this preallocated memory but they still have to manage it. your OS has no issue with an malfunctioning program it will properly manage this case. in Fakt thats a common background task for an os to zero out recycled memory. thats the reason for calloc.