Solved

Understanding Hex memory addresses

Posted on 2003-10-29
5
486 Views
Last Modified: 2010-04-15
Im am trying to understand how C used memory locations and have written a bit of code that has some variables in it, and then I have called the printf() function to print out the memory locations.

A couple of questions at this stage.

1. My output comes out as follows
Address of o =:0xbfffbe1c
Address of iDo =:0xbfffbdf0
Address of like2 =:0xbfffbdc0
...
..
..

I understand these are hex values referring to where these variables are stored in memory, I was wondering how to understand how to understand these and work out how C managed memory.

2. If I uncomment the code that refers to the variables in MyStruct_ then I get errors whilst compiling? Why is this and how can I print off the addressess of the variables within the struct?

Thanks

Here is my code



#include <stdio.h>

typedef struct MyStruct_
{
      int myFirstVariable;
      double someNumbers[4];
      char myBestLetter;
} MyStruct;

int main()
{
  int o=0;
  double iDo[3]={4.2, 1.2, 1.5};
  MyStruct like2;
  double *b= iDo;
  char besideThe;
  char *c= NULL;



 /* printf("Address of myFirstVariable =:%p \n",&myFirstVariable);
         printf("Address of someNumbers =:%p \n",&someNumbers);
  printf("Address of someNumbers =:%p \n",&someNumbers);
  printf("Address of myBestLetter =:%p \n",&myBestLetter);*/


  printf("Address of o =:%p \n",&o);
  printf("Address of iDo =:%p \n",&iDo);
  printf("Address of like2 =:%p \n",&like2);
  printf("Address of b =:%p \n",&b);
  printf("Address of *b =:%p \n",&*b);
  printf("Address of besideThe =:%p \n",&besideThe);
  printf("Address of *c =:%p \n",&c);

  printf("My struct is %d bytes long\n", sizeof(like2));

}
0
Comment
Question by:welsh_boy
5 Comments
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 50 total points
Comment Utility
Regarding your address question

Use %u for printing in decimal

e.g. printf ( "%u", &a ) ; would print the address of a in decimal


REgarding your second question

You should refer to your structure components using the structure variable

Thus it shoudl be


printf("Address of myFirstVariable =:%p \n",&(like2.myFirstVariable));
        printf("Address of someNumbers =:%p \n",&(like2.someNumbers));
  printf("Address of someNumbers =:%p \n",&(like2.someNumbers));
  printf("Address of myBestLetter =:%p \n",&(like2.myBestLetter));

Hope this helps

0
 

Author Comment

by:welsh_boy
Comment Utility
thanks dude

changed my code now as below.
And am getting the following output

Variable                Address              Decimal         Size      Value
-----------------------------------
myFirstVariable         0xbfffc6d0      3221210832      4
someNumbers           0xbfffc6d4      3221210836      16
myBestLetter             0xbfffc6e4      3221210852      1
o                             0xbfffc71c      3221210908      4
iDo                          0xbfffc6f0      3221210864      24
like2                        0xbfffc6d0      3221210832      24
b                            0xbfffc6cc      3221210828      4       -1.986039
*b                          0xbfffc6f0      3221210864      8       0xcccccccd
besideThe               0xbfffc6cb      3221210827      1
*c                          0xbfffc6c4      3221210820      4
My struct is 24 bytes long


A couple more q's
 Why is Mystruct 24bytes long since it consists of myFirstVariable (4bytes) , double someNumbers[2] (16 bytes) and myBestLetter(1byte) , shouldint it be 21 bytes long?

2. With the memory addresses what is the correct order that the variables are entered? doesnt C put the variable that first comes up in the code in the memory first?

3. *b is declared but I still have a memory entry for b and it is 4 bytes, and has a value even though i havent decared it! I dont understand this
 ii) and going on the same concept shouldt there be a c declared? i tryed a printf for this (the code is commented out below) but got a segmentation error.


Thanks again dude if you can help.

#include <stdio.h>

typedef struct MyStruct_
{
      int myFirstVariable;
      double someNumbers[2];

      char myBestLetter;
} MyStruct;

int main()
{
  int o=0;
  double iDo[3]={4.2, 1.2, 1.5};
  MyStruct like2;
  double *b= iDo;
  char besideThe;
  char *c= NULL;



      for (o=0; o<10; o++)
      {
            iDo[o] = 0xdeadbeef;
}

  printf("Variable\t\tAddress \t  Decimal\tSize\tValue\n-----------------------------------\n");

  printf("myFirstVariable \t%p \t%u \t%d\n",&like2.myFirstVariable,&like2.myFirstVariable,sizeof(like2.myFirstVariable));
  printf("someNumbers \t\t%p \t%u \t%d\n",&like2.someNumbers,&like2.someNumbers,sizeof(like2.someNumbers));
  printf("myBestLetter \t\t%p \t%u \t%d\n",&like2.myBestLetter,&like2.myBestLetter,sizeof(like2.myBestLetter));
  printf("o \t\t\t%p \t%u \t%d\n",&o,&o,sizeof(o));
  printf("iDo \t\t\t%p \t%u \t%d\n",&iDo,&iDo,sizeof(iDo));
  printf("like2 \t\t\t%p \t%u \t%d\n",&like2,&like2,sizeof(like2));
  printf("b \t\t\t%p \t%u \t%d \t%lf\n",&b,&b,sizeof(b),b);
  printf("*b \t\t\t%p \t%u \t%d\t%p\n",&*b,&*b,sizeof(*b),*b);
  printf("besideThe \t\t%p \t%u \t%d\n",&besideThe,&besideThe,sizeof(besideThe));
  printf("c \t\t\t%p \t%u \t%d\t%c\n",&c,&c,sizeof(c),c);
  //printf("*c \t\t\t%p \t%u \t%d\t%p\n",&*c,&*c,sizeof(*c),*c);
  printf("My struct is %d bytes long\n", sizeof(like2));

  return(0);

}
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 50 total points
Comment Utility
structs are packed according to the compiler's whims.
often these days the whim is to align things on 4-byte boundaries.

You can change this packing wth #pragma pack(x) where x can be 8,4,2, or 1

The ordering for structs is in increasing order of addresses.

But for variables local to a function, they're usually decreasing as they're allocated on the stack.

For global static variables they're usually increasing, but YMMV.

0
 
LVL 1

Assisted Solution

by:guitardude101
guitardude101 earned 100 total points
Comment Utility
A couple more q's
 Why is Mystruct 24bytes long since it consists of myFirstVariable (4bytes) , double someNumbers[2] (16 bytes) and myBestLetter(1byte) , shouldint it be 21 bytes long?


>>> The compilier will align structures in memory for faster access. The cpu can access data faster if it is allign on a long data boundry in memory. It is also much slower to access an odd memory address. Your compilier may also have an option not to do memory alignment.

2. With the memory addresses what is the correct order that the variables are entered? doesnt C put the variable that first comes up in the code in the memory first?

 >>>>  The compilier will put local variables on the stack in order.... but that is undefined. It may also store the variable in a register rather than in memory. Global variables may be stored differently depending on the cpu.


3. *b is declared but I still have a memory entry for b and it is 4 bytes, and has a value even though i havent decared it! I dont understand this
   Same reason as above. The compilier will align variables on long memory boundries for faster access. The contents of the memory is undefined until you assign a value.

 ii) and going on the same concept shouldt there be a c declared? i tryed a printf for this (the code is commented out below) but got a segmentation error.

>>>   This is causes an error because c is a null pointer.  The mean *c tries to access memory location 0. You can't do that. You cal look at the address of c but not what it points to.


I hope you understand.

0
 
LVL 3

Accepted Solution

by:
guynumber5764 earned 150 total points
Comment Utility
OK, the ultra short pointer primer (simplified):

A pointer is just another data type like an int or a double.  It just happens to be the correct size to hold a memory address (4 bytes in this case...the same size as an int but thats just a coincidence).  Note that the actual type associated with the pointer (ie: int in pointer to int)  is unimportant to the assembler.  It is only used early in the compile process to help make sure that the programmer knows what he is doing.

Declaration

As your code demonstrates very nicely (nice work btw) each declared variable is in fact a reference (address) to a preallocated block of  memory of the appropriate size.  In the case of an int, it is a reference to a place big enough to hold an int (4 bytes or so).  In the case of a  pointer, it is a reference to a location big enough to hold an address (also 4 bytes in this case).

Assignment

The "=" operator is called the "assignment".  It sets the bytes in the location given by the expression in the left of the operator to the specific bit pattern given by the expression in the right.

Examples:

declaration:
int i;    // this allocates 4 bytes of memory at some address (0xbfffc71c)  and, in the symbol table, adds the symbol "i" associated with the location (0xbfffc71c).  Note that those 4 bytes already contain some random value.

assignment:
i=1;    // this sets the 4 bytes at (0xbfffc71c) to 00 01 00 00 which corresponds to integer 1 on an x86.

declaration:
int * p; // this allocates 4 bytes of memory at some address (0xbfffc720) and, in the symbol table adds the symbol "p" associated with the location (0xbfffc720).  

assignment:
p = &i; // this sets the 4 bytes starting at "p" (0xbfffc720) to BF FF C7 1C which corresponds to expression "&i" (the address of i or (0xbfffc71c))


gotta go...hope it helps
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now