3

I am trying to learn the concept of pointers in depth. In the below code , I create an array an create a pointer to each of the element.

int bucky[5];
int *bp0 = &bucky[0];
int *bp1 = &bucky[1];
int *bp2 = &bucky[2];

cout<<"bp0 is at memory address:"<<bp0<<endl;
cout<<"bp1 is at memory address:"<<bp1<<endl;
cout<<"bp2 is at memory address:"<<bp2<<endl;

These are the memory allocations given to the array elements.

bp0 is at memory address:0x0018ff3c
bp1 is at memory address:0x0018ff40
bp2 is at memory address:0x0018ff44

With my limited knowledge in c++ , I am aware that memory is allocated contiguously to an array. But looking at the output closely, bp0 looks out of place.

According to me bp0 should be at 0x0018ff36. Or is it that the locations 0x0018ff3c , 0x0018ff40 , 0x0018ff44 are continuous in the CPU?

So is it possible that two contiguous memory allocations are not assigned in a progression?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • 4
    That’s not how hexadecimal works. – Konrad Rudolph Feb 20 '15 at 10:26
  • Yes it is contiguous. If size of pointer is 4 bytes, next pointer will be placed after 4 bytes, isn't it? – Mohit Jain Feb 20 '15 at 10:27
  • @MohitJain so how does 3c come ? shouldnt it be 46? – m0bi5 Feb 20 '15 at 10:28
  • You should not care about memory addresses. `bp0` is the address of `bucky[0]`. `bp0+1` is the address of `bucky[1]` and so on. That's all you need to care. Let the rest to the compiler (they are implementation details) and focus on the logic of your code. – axiac Feb 20 '15 at 10:29
  • @axiac just trying to learn something new . – m0bi5 Feb 20 '15 at 10:30
  • 1
    Why do you think `bp0` should be `0x0018ff36`? – axiac Feb 20 '15 at 10:31
  • 1
    @axiac: because he thinks in decimal, not in hex. – akira Feb 20 '15 at 10:31
  • @akira I read the question again and I think you are right. – axiac Feb 20 '15 at 10:32
  • @axiac see memory is allocated continuously to an array. Now, as every element is an integer , each element occupies 4 bytes of memory. So bp1 is at 0x0018ff40 and bp2 is at 0x0018ff44 which is perfect but how come bp0 is at 0x0018ff3c – m0bi5 Feb 20 '15 at 10:33
  • possible duplicate of [Does stack grow upward or downward?](http://stackoverflow.com/questions/1677415/does-stack-grow-upward-or-downward) – sashoalm Feb 20 '15 at 10:37
  • 2
    @MohitBhasi the `0x` in front of these numbers denotes that they are written using the hexadecimal notation (this is the usual notation of memory addresses). Search for a tutorial about hexadecimal notation and you'll understand. – axiac Feb 20 '15 at 10:38
  • Lol is this actually the duplicate of the suggestion question? I dont see the similarity – m0bi5 Feb 20 '15 at 10:40
  • This is clearly not a dupe of that question. – Puppy Feb 20 '15 at 13:45

2 Answers2

7
           +---+---+---+---+---+---+---+---+---+---+---+---+
           |      bp0      |      bp1      |      bp2      |
           +---+---+---+---+---+---+---+---+---+---+---+---+
  0x0018ff3c   d   e   f  40   1   2   3   4   5   6   7   8

Assuming size of int is 4 bytes and as bp0 points at 0x0018ff3c.

bp1 = bp0 + 4 = 0x0018ff40
bp2 = bp1 + 4 = 0x0018ff44

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
4

Each int uses 4 bytes. The first position of the array is 0x0018ff3c so the next int will be at 0x0018ff3c + 4

Notice the addresses are in heaxadecimal, so:

0x0018ff3c + 0x00000004 = 0x0018ff40

Note: these are all hexadecimal numbers. The order looks like this: 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41...

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Leo
  • 1,174
  • 11
  • 25
  • Woahhh , had no idea abt it ! can u suggest a place to learn how memory is allocated – m0bi5 Feb 20 '15 at 10:34
  • @MohitBhasi: in your 101 cs course, usually. It's not that different from base10, you just have a larger alphabet 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f – akira Feb 20 '15 at 10:36
  • If you are interested in how a computer works at a very basic level, you should start from the begining so your mind doesn't get clogged: http://en.wikipedia.org/wiki/Von_Neumann_architecture This architecture is really old, but the principles are (mostly) the same. – Leo Feb 20 '15 at 10:38