Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.

memory address range

vtuneuser
Beginner
221 Views
is there any hint that indicates that certain memory addresses belong to stack segment or data segment or code segment? for example, I printedthe addresses of a local variable of a function and a field dereferencing from this variable as follows,
long func(t *ptr_t)
{
n *ptr_n;
:
printf ("adress of ptr_n: 0x%p ", &ptr_n);
printf("address of ptr_n->cos: 0x%p ", &(ptr_n->cos));
:
}
I got 0x0012FE1C for &ptr_n and 0x00A600A0 for &(ptr_n->cos), and "cos" was one field of type "n" and 8 bytes from the beginning of type "n". I dont know why I am getting the first memory address was so far from the second memoty address? Can we tell what address range belong to the stack, data or code segment?
Thanks.
0 Kudos
3 Replies
rmauldin
Beginner
221 Views
0x0000-0x9FFFFSystem boardOK
0xFEC00000-0xFEC000FFSystem boardOK
0xFEE00000-0xFEE00FFFSystem boardOK
0xFFC00000-0xFFFFFFFFMotherboard resourcesOK
0xEE800000-0xEE800FFFStandard OpenHCD USB Host ControllerOK
0xEE000000-0xEE000FFFStandard OpenHCD USB Host ControllerOK
0xE9800000-0xECFFFFFFPCI standard PCI-to-PCI bridgeOK
0xE9800000-0xECFFFFFFLinksys Wireless-G PCI AdapterOK
0xF7F00000-0xF7FFFFFFPCI standard PCI-to-PCI bridgeOK
0xEC800000-0xEC800FFFNEC PCI to USB Open Host ControllerOK
0xEC000000-0xEC000FFFNEC PCI to USB Open Host ControllerOK
0xEB800000-0xEB8000FFStandard Enhanced PCI to USB Host ControllerOK
0xEB000000-0xEB0007FFOHCI Compliant IEEE 1394 Host ControllerOK
0xEA800000-0xEA803FFFOHCI Compliant IEEE 1394 Host ControllerOK
0xEA000000-0xEA0000FFDAVICOM 9102/A PCI Fast Ethernet Adapter OK
0xE8000000-0xE97FFFFFPCI standard PCI-to-PCI bridgeOK
0xE8000000-0xE97FFFFFNVIDIA GeForce3 (Microsoft Corporation)OK
0xEF700000-0xF7EFFFFFPCI standard PCI-to-PCI bridgeOK
0xF0000000-0xF3FFFFFFNVIDIA GeForce3 (Microsoft Corporation)OK
0xEF800000-0xEF87FFFFNVIDIA GeForce3 (Microsoft Corporation)OK
0xA0000-0xBFFFFPCI busOK
0xA0000-0xBFFFFPCI standard PCI-to-PCI bridgeOK
0xA0000-0xBFFFFNVIDIA GeForce3 (Microsoft Corporation)OK
0xC8000-0xDFFFFPCI busOK
0xF0000-0xFFFFFSystem boardOK
0x100000-0x1FFFFFFFSystem boardOK
0x20000000-0xFFFFFFFFPCI busOK
Here is a sample from my computer (Windows XP... to look at yourconfiguration on a windows 9x + machine go to Accessories, click on System Tools, and then System Information. Click on Hardware Resources and then click on Memory
Possible Answers to your questions...
0x0012FE1C for &ptr_n and 0x00A600A0

These addresses would fall in this range here on my x86 machine.
0xF0000-0xFFFFFSystem boardOK
0x100000-0x1FFFFFFFSystem boardOK
(00A600A0 0012FE1C) => 930284 Hex / 8 (Bytes)

= 126050 Hex -> Dec = 1,204,304 Bytes
So your two data addresses are roughly 1.2 MB apart. This is normal in a protected mode program which can access within 16MB of memory for your data and code. The rest is allocated on the Stack and depending on when your data is allocated you may notice wider differences in these locations. Real mode programs only allow for 1 MB of memory allocation for your programs on load. However I am pretty sure your C source is running in protected mode.

I used to have a great diagram to show how the different segments looked in memory. Basically the Data and Stack segments are separate entities that grow towards each other. Global variables have their own little place as well. The rest belongs to the OS and device drivers. ( As far as exact locations beets me ask your OS provider)

Message Edited by rmauldin on 10-18-2005 01:32 PM

rmauldin
Beginner
221 Views

>>I got 0x0012FE1C for &ptr_n and 0x00A600A0 for &(ptr_n->cos), and "cos" was one field of type "n" and 8 bytes from the beginning of type "n". I dont know why I am getting the first memory address was so far from the second memoty address? Can we tell what address range belong to the stack, data or code segment? <<

A little rethinking here... It always hits me when I'm busy doing something else, and I realize I didn't quite answer the question...

&ptr_n --> is the address of the pointer (only) ... not the location of the data that the pointer is looking at. There is no need to reference a pointer to show the location of the data.

&(ptr_n->cos) is correct giving you the correct address for cos.

Here is a example of the program I modified here.
----------------------------------------------------------------------------------------
This is the example you gave where we are looking at
The address of ptr_n with the & operator...
address of ptr_n: 0x0042B7C0
address of ptr_n->cos: 0x003239C0

This is my version with no referencing of the pointer.
We just need to look at where it is pointing
address of ptr_n: 0x003239C0
address of ptr_n->cos: 0x003239C0
address of ptr_n->cos2: 0x003239C4
address of ptr_n->cos3: 0x003239C8
Notice how ptr_n is pointing to the first address
within the structure... the same place as 'cos'!

Press any key to continue
----------------------------------------------------------------------------------------
Here is the source code... and I am going to attach it as well..
Ryan Mauldin

// MemoryExample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

struct Mem
{
int cos;
int cos2;
int cos3;
}*ptr_n;

int
_tmain(int argc, _TCHAR* argv[])
{
ptr_n = new Mem;
ptr_n->cos = 0;
ptr_n->cos2 = 0;
ptr_n->cos3 = 0;

// your version
printf ("This is the example you gave where we are looking at ");
printf ("The address of ptr_n with the & operator... ");
printf ("address of ptr_n: 0x%p ", &ptr_n); // referencing
printf("address of ptr_n->cos: 0x%p ", &(ptr_n->cos));

// my version
printf(" This is my version with no referencing of the pointer. ");
printf("We just need to look at where it is pointing ");
printf("address of ptr_n: 0x%p ", ptr_n); // no ne ed to reference
printf("address of ptr_n->cos: 0x%p ", &(ptr_n->cos));
printf("address of ptr_n->cos: 0x%p ", &(ptr_n->cos2));
printf("address of ptr_n->cos: 0x%p ", &(ptr_n->cos3));
printf("Notice how ptr_n is pointing to the first address ");
printf("within the structure... the same place as 'cos'! ");

return 0;
}

jim_dempsey
Beginner
221 Views

The original discussion as I recall was figguring out where stack addresses are versis other variable addresses are. In the MemoryExample.cpp you have

struct Mem
{
int cos;
int cos2;
int cos3;
}*ptr_n;

int _tmain(int argc, _TCHAR* argv[])
{
ptr_n = new Mem;

...
Which declares a structure Mem and defines a global scoped pointer to the structure of the name ptr_n.

Therefore in the printf(s)

&ptr_n is the location in global scoped memory that holds the pointer to the Mem structure that was allocated with new.

ptr_n is the contents of the location in global scoped memory that holds the pointer to the Mem structure that was allocated with new. i.e the address of the block that was allocated.

&(ptr_n->cos) is the address of the 1st variable in the allocated Mem structure.

Therefore

printf("Address of global pointer 0x%p ", &ptr_n);
printf("Address of allocated Mem structure 0x%p ", ptr_n);
printf("Address of cos in allocated Mem structure 0x%p ", &(ptr_n->cos));
struct Mem* Stack_ptr_n = ptr_n; // copy pointer
printf("Address of Stack_ptr_n 0x%p ", &Stack_ptr_n);

Also, Most processors push downwards on the stack.

unsigned int esp_at_start;

void main()
{
unsigned int temp;// something on stack at start
esp_at_start = &temp; // this won't work on multi-threadded programs
...
}

bool IsOnStack(unsigned int pointerAddress)
{
unsigned int current_esp = &current_esp;
return(pointerAddress>current_esp && pointer_address}

Jim Dempsey

Reply