Question on pointer usage

This is a discussion on Question on pointer usage within the C++ Programming forums, part of the General Programming Boards category; I know that when a pointer is incremented, it will point to the next element of its type. e.g: Code: ...

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    Question on pointer usage

    I know that when a pointer is incremented, it will point to the next element of its type. e.g:
    Code:
    int a[5]={1,2,3,4,5},*p;
    p=a;
    cout<<*(p+4)<<endl; //will output 5
    This is because the addresses of elements of the array are all adjacent.
    My question is applying the same principle, why doesn't the following work:
    Code:
    int a,b,c,d,e,f,g,h,i,*p;
    p=&a;
    for(i=0;i<8;i++) *(p+i)=i*i;
    /*The loop should put into int a to h the squares of i as i increments*/
    Because the variables are all created at the same line, with nothing in between, does it mean their addresses are also adjacent?
    I AM WINNER!!!1!111oneoneomne

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >does it mean their addresses are also adjacent?<

    Not necessarily. The only way you can guarantee the kind of behaviour you expect is by using an array (or importing an asm module). Try checking the addresses of the variables in each situation.
    Joe

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by JoeSixpack
    >does it mean their addresses are also adjacent?<

    Not necessarily. The only way you can guarantee the kind of behaviour you expect is by using an array (or importing an asm module). Try checking the addresses of the variables in each situation.
    Actually, using commas in a declaration does guarantee that they are adjacent (least that's what chubsy taught us).

    However, I'm not quite sure about the order. I checked msvc++ and the order is actually opposite (so if you start with p = &h it will work). Not sure if the order is standard though.
    Last edited by Polymorphic OOP; 01-18-2003 at 07:55 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >However, I'm not quite sure about the order. I check msvc++ and the order is actually opposite (so if you start with p = &h it will work)<

    Maybe with commas; but try and get compilers to line up global/static objects up the way you'd think (****ing pain in arse). I'd imagine the backwardness was due to the stack; in which case it would be correct and all would be well.
    Joe

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    EDIT: Ahh, i see why. I didn't realize the stack expanded BACKWARDS in memory rather than forwards. Seems kind of odd. Anyone know of the reason for this?
    Last edited by Polymorphic OOP; 01-18-2003 at 08:02 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    *hint* : "the stack grows downward."
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Thanx. it works as expected now.
    I AM WINNER!!!1!111oneoneomne

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Salem
    Knowing the address of a, tells you NOTHING about the address of b, so I do hope you're just doing this for fun.
    I was taught that comma's in declarations guarantee that they are adjacent in memory (as opposed to separate lines of declarations). So b would either be sizeof(int) above or sizeof(int) below in memory. Is this correct, or improper teaching?

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    In my experience, declaring variables in any way will put them in the expected order. Ie:

    Code:
    int a,b,c,d,e,f,g;
    int h;
    int i;
    int j;
    int k;
    They will be in order in memory. I've used Windows 98, Windows XP, Turbo C 2.01, and Microsoft Visual C++, and I've never seen any different behavior.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  2. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  3. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21