Pointers - More Information?

Hi guys,

First let me say that I understand the basic concept of pointers, however I would like to know a little bit more about how the memory is allocated. I whipped up the following program for fun:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main() 
{
  // Statically declare two doubles
  double myStaticDouble1, myStaticDouble2;
  myStaticDouble1 = 6.0;
  myStaticDouble2 = 7.0;

  cout << "Static double 1 points to: " << &myStaticDouble1 << endl;
  cout << "Static double 2 points to: " << &myStaticDouble2 << endl;

  // Dynamically allocate memory for two doubles
  double * myDynamicDouble1 = new double();
  double * myDynamicDouble2 = new double();
  *myDynamicDouble1 = 8.0;
  *myDynamicDouble2 = 9.0;

  cout << "Dynamic double 1 points to: " << myDynamicDouble1 << endl;
  cout << "Dynamic double 2 points to: " << myDynamicDouble2 << endl;

  // Cleanup
  delete myDynamicDouble1;
  delete myDynamicDouble2;

  return 0;
}


When I run it on a little netbook, it gives the following output:


Static double 1 points to: 0029F974
Static double 2 points to: 0029F964
Dynamic double 1 points to: 002B1FB0
Dynamic double 2 points to: 00633490


My questions (answers for which I can't seem to find easily on the web):
1. The static doubles are allocated to the stack, and the dynamic doubles allocated to the heap, right? WHEN exactly is the static memory allocated? When the program first starts up?
2. What determines the size of the addressable space in memory? Is it to do with how much RAM the machine has? How do the physical components of a computer determine this range?
3. Is there a tool available with which one can examine stack/heap contents at any given time? For example, could I get a tool that would allow me to view the bits in memory location "002B1FB0"?

I realise that my question is only really theoretical, but I'm curious nonetheless. Perhaps there's a book I should be reading.

Please only reply if you're sure about the answers...

I'm a bit suspicious of your program output that you have shown.

I would have expected both of the dynamic variables to be in the same memory area -
but one is saying 002B1fB0 and the other 00633490. The first one looks like it is in
your program data area (as it is very close to the 0029F9xx addresses of the local variables).

I could be wrong - it just doesn't look right that's all.

Hmmmm...you're right. In fact, whenever I run it in debug mode using Visual Studio, it gives the output you're talking about (where the dynamic variables get memory located very close to one another). However, when I run the *.exe from the command prompt, it seems to always separate the dynamic variables. Anyone got an explanation for that?
Topic archived. No new replies allowed.