Windows Phone 7 development

Posts tagged ‘Malloc’

Does malloc allocate a block of memory on the heap or should it be called Virtual Adress Space?

To answer this question, we need to know what kind of Operating System and architecture we are dealing with,  the Standard and articles refers to “storage” or “space”. These are the most general terms and the only valid ones, unless we make a bunch of assumptions.

For example:

malloc allocates a block of Virtual Address Space on the heap

This is not correct for many embedded systems. Many of them do not use Virtual Memory, because there is no need (no multitasking etc.) or for performance reasons. What is more, it is possible that some exotic device does not have the idea of heap – doubt that malloc would be used, but fairly that is one of the reasons why the Standard refers to “storage” – it is implementation-specific.

On the other hand, the example is correct for Window and Linux in our PCs. Let’s analyze it to answer the question.

First off, we need to define what is Virtual Address Space.

Virtual Address Space (VAS) is a memory mapping mechanism that helps with managing multiple processes.

  • isolate processes – each of them has his own address space
  • allow to allocate memory that 32-bit architecture is limited to.
  • provides one concise model of memory

Back to question, ”Does malloc allocate a block of memory on the heap or should it be called Virtual Adress Space ?”

Both statements are correct. I would rather say VAP instead of memory – it is more explicit. There is a common myth that malloc = RAM memory. Back in old day, DOS memory allocation was very simple. Whenever we ask for memory it was always RAM, but in modern Oses it may vary.

%d bloggers like this: