1

I' am learning c++ and executing this code

#include<stdio>
using namespace std;

int main(){

    char* buffer = new char[5];
    printf("%p", &buffer);                // 000000000061fe10
    cout<<endl;
    printf("%p", buffer);                  // 0000000000796b700
}

gives me 2 memory locations that are totally separated i mean around 1.5 million bytes away so why is this the heap size is supposed to be 5 bytes right ? char size * 5. Heap grows down (from 0xFFF... to 0x0000..) under heap there is the stack so difference is supposed to be much more less so what is memory layout in this case. and what is that that I don't understand exactly in my explanation above

trincot
  • 317,000
  • 35
  • 244
  • 286
KMG
  • 1,433
  • 1
  • 8
  • 19
  • Those are 120,895,728 bytes apart, not 1.5 million. – Eric Postpischil Jun 26 '20 at 22:35
  • 1
    *"Heap grows down (from 0xFFF... to 0x0000..) under heap there is the stack"* Huh? – HolyBlackCat Jun 26 '20 at 22:35
  • 3
    Note that this separation is not necessarily physical since basically all major OSes have virtual memory. – cdhowie Jun 26 '20 at 22:36
  • Note that with virtual memory and address translation, using two regions of memory that far apart is essentially free. See https://stackoverflow.com/questions/14347206/what-are-the-differences-between-virtual-memory-and-physical-memory – alter_igel Jun 26 '20 at 22:37
  • The stack growing up and heap growing down are simplified concepts that we teach to students (it may have been what very old computers used as well). This simplification works well for explaining the concept but in reality it depends a lot more on the OS. I mean you can implement the stack inside the heap if you want to (A stack is simply a linked list of stack frames there is no need for them to be physically contiguous (though that is a very simple implementation)). – Martin York Jun 27 '20 at 01:32

1 Answers1

8

The layout of memory depends on the operating system, the program loader, which is usually supplied with the operating system, rules regarding executable files, and requests given to the linker. You have not specified which operating system you are using, so a definite answer is not possible.

However, memory addresses are arbitrary. If a craftsman lays out their tools, their instructions, their parts, and their workspace, they may arrange them in any way they want. There is no requirement that the stack be near the heap. Likely, the heap has been given a high address so that there is plenty of room in the virtual memory space for it to grow downward, or room for things below it to grow upward. Since virtual memory is created by arbitrary maps from virtual addresses to physical addresses, there are few restrictions on how it is laid out—a program can use a few addresses here, a few addresses in another place, and a few addresses in yet another place, and it can leave a lot of unused space between them, and that unused space will not consume any memory because it is not mapped to physical memory. So virtual memory is laid out as the people laying it out find convenient.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I don't think i follow here so is these outputs are virtual memory or physical memory and does this is happen only with heap i mean does heap variable only that can be scattered or stack variables also can be scattered like in this example – KMG Jun 26 '20 at 22:43
  • As a user mode program you will never see physical addresses. Your app has thousands of terabytes of virtual address space to put stuff in so heap and stacks can theoretically be anywhere in that. That said, a stack is contiguous so once you've seen a variable on it, other variables on the same stack will be close by, relatively speaking. – Mike Vine Jun 26 '20 at 22:49
  • Virtual memory is an abstraction around physical memory and possibly multiple different kinds of physical memory. Part of your hard drive could be mapped to virtual memory. If we could figure out how to do it, virtual memory could map to data encoded in the spins of a billion hamster wheels – user4581301 Jun 26 '20 at 22:51
  • @MikeVine thank's alot but there is no such role for different stacks ,they are like the heap in this area – KMG Jun 26 '20 at 22:52
  • 1
    @ahmedamr for most things you can think of memory addresses as labels. They are numbers but if they would be labeled with names eg foo and bar it wouldn't make much difference. – 463035818_is_not_an_ai Jun 26 '20 at 23:05
  • @MikeVine There is no requirement for a stack to be contiguous (though that is a simple implementation of one). The only requirement is that poping the stack allows you move back to the last stack frame. In the late 90's MS experimented with random stack location to prevent deliberate stack smashing attacks on the OS to gain privileged accesses (If you can not predict where the stack frame is, then it's hard to corrupt it in a useful way). – Martin York Jun 27 '20 at 01:38