views:

240

answers:

5

The question was asked to me in an interview and my answer was "computer memory". But where exactly..? is it the Random Access Memory or the hard drive?

+7  A: 

They were probably looking for "the heap". This is an area of memory that's separate from "the stack", which is where all your local variables, parameters, return values, etc., are stored. And yes, it's all in RAM, not on the hard drive.

jwismar
But I could go to the hard disk if the operating system needs to do [Paging](http://en.wikipedia.org/wiki/Paging)
Carlos Muñoz
@Carlos Muñoz, Yes, but then you can't access it anymore. It has to be fetched first.
strager
You've got to be running a really primitive operating system to get allocations in RAM. It gets allocated in the paging file. And eventually ends up there, if necessary.
Hans Passant
@strager: Sure you can--OS paging is generally transparent at the application level (other than the sluggish performance that results). That's what virtual memory is all about.
Drew Hall
Yes, I understand, but you can't directly access paged memory as if it were on the heap. You (i.e. the system entirely, probably including the OS) have to fetch the memory from the HDD first.
strager
@strager, the whole point of virtual memory is that from the application's point of view, it just uses it as if it were all there all the time. Sure, the OS is kept busy behind the scenes, and if physical memory is in short supply then some access take longer (a **lot** longer) than others while the OS pages it in, but the application gets to remain blissfully ignorant and just use the memory.
RBerteig
+4  A: 

Dynamically allocated memory is allocated from memory that is commonly referred to as the "Heap".

In C++ we really have two "Heaps" (thought they may (or may not) technically be the same area). Memory allocated by malloc() (and family) comes from the "heap" while memory allocated by new (and family) comes from the "free store".

Please note that both "heap" and "free store" are moniker for specific instances of a Computer Science "Heap".

The language definition does not define what is meant by "heap" or "free store" that is left for the implementation to define. Also the Operating system will play a role in what happens to memory pages but that is beyond the scope of most applications.

In Computer science the term "Stack" is used as a simplistic teaching tool. It defines an area of memory that is used to track function calls and is distinct from the "Heap". In modern operating systems this is too simplistic a definition. As such the runtime "Stack" is now usually implemented as part of the "Heap" (This has the added advantaged that you do not need to attempt to track if heap/stack clash at run-time. Also it is used as security precaution as it becomes harder to smash the stack and get a deterministic result).

Martin York
+3  A: 
  1. The C++ standard does not mention the word 'heap' in relation to operator new or dynamic memory allocation. So 'heap' probably in common parlance is used to denote a memory which is distinct from the storage area for automatic variables

  2. C++ standards states in a footnote - "The intent is that the memory model of C + + is compatible with that of ISO/IEC 9899 Programming Language C."

  3. When I refer the C standard, it also does not mention anything about 'heap'.

So in summary, it is best to assume that the actual storage of dynamic memory allocation is unspecified (behavior, for a well-formed program construct and correct data, that depends on the implementation.)

One last point.

4.It is important to remember that 'placement new' does not allocate any memory. Instead it uses a preallocated memory (passed to it as an argument) to construct the object.

Chubsdad
+1  A: 

To summarize briefly- A one word answer might be 'heap' . But, what is referred to as stack or heap turns out to be context dependent. For instance,

See the Intel Programming Guide, Section 6.2 for Stack. 'SS' is the stack segment, which provides a continuous address space. When you invoke a function, it''s return address, parameters passed, and local variables are allocated on this segment- referred to as 'Stack'. If you have a pointer variable (say char * p), then this variable is allocated on the stack, but the memory is allocated when malloc(), or 'new' are invoked on C,C++ is allocated on the Data Segment (DS) .Also, see the stanford tutorials.So, instead of just incrementing the Top of the Stack in SS to go to the next memory address,now the processor has to do address translation- check if the address is within the process address range,split the the Virtual Address to Segment+Offset,to get the value at a particular address.

Hence, over here the discussion of 'Stack' and 'Heap' is closely related to the processor architecture- when we deal with languages like C,C++ .

For Java, everything is done on the heap- which is a common storage area for all Java processes- The JVM spec (see sections 3.5 and 3.6)says that each JVM thread has it's own private stack-which stores stack frames-which consists of parameters passed, local variables etc. Most importantly, these Stack frames can be allocated from the Heap- and need not be sequentially addressed. In addition to this java also provides for Native method stacks-like C.

But, I am guessing in an interview setting,it might be best to say 'Heap'- and only think about elaborating if asked to.

+1  A: 

On a modern PC, workstation, or server machine, the answer to "Is it the Random Access Memory or the hard drive?" is: neither. It's allocated in virtual memory. When it's first allocated, the virtual address given to you likely has no physical existence whatsoever. On the first access, it will be physically instantiated in RAM, but it may be later moved to storage on a disk or other swap device if RAM is needed for other uses. Upon a subsequent attempt to access it, it will again be moved back into RAM.

This is just the very very basic explanation of virtual memory. If you really want to understand, go read some articles on Wikipedia or Google for it.

R..