Thread: Question about this Array

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61

    Question about this Array

    I was just playing around with some code when I ran into this.
    The output for this code was 7, 1, 2686728, 9, 7, 5, 3, 1

    (if you run this your third number may be different)

    In the code I get an address when cout is executed the third time note (anArray - 1). "I thought it would print (anArray) one element below the last one but I was wrong" Is this the address the actual address of element [3] or the address one byte below it or what?
    How can I prove what it is??

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    int main ()
    {
    int anArray[5] = { 9, 7, 5, 3, 1 };
    cout << *(anArray+1) << endl;
    cout << * (anArray +4) <<endl;
    cout << *(anArray -1) <<endl;
    cout << anArray [0]<<endl;
    cout << anArray [1] <<endl;
    cout << anArray [2] <<endl;
    cout << anArray [3] <<endl;
    cout << anArray [4] <<endl;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    *(anArray - 1) is equivalent to anArray[-1].

    So it is evaluating the memory immediately before the 9 - as an int. The fact that the preceding the preceding statement printed *(anArray + 4) (i.e. anArray[4]) does not somehow make *(anArray - 1) somehow reference anArray[3]. The address associated with anArray is not magically changed by the pointer logic in your code.

    The value being printed is just a large integer value, made up of whatever bits happen to be set in that particular location in memory (immediately before the 9). The value printed is not an address.

    Formally your code exhibits undefined behavior, as does any attempt to access an array element out of bounds.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How can I prove what it is??
    You can't. When you try to access an array out of bounds you are in undefined territory.

    In the code I get an address when cout is executed the third time note
    No, you get an undefined value.
    Is this the address the actual address of element [3] or the address one byte below it or what?
    No it is not an address, it is whatever value resides at the address of the array - 1.



    Jim

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You simply happen to access whatever is stored in memory just before your array. What is that? We don't know. The standard does not say. Therefore, it can vary with compiler, OS and architecture. It can even crash your program. In short, it's undefined, so there is no answer. Don't do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Thank You all for the input. It is understandable that anArray [-1] is not part of the array. I should have caught it myself!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-21-2010, 09:46 AM
  2. Array question
    By beon in forum C Programming
    Replies: 1
    Last Post: 11-30-2006, 03:21 AM
  3. Array question..
    By snapshooter in forum C++ Programming
    Replies: 14
    Last Post: 11-24-2004, 09:06 AM
  4. Array question
    By WolfPack in forum C Programming
    Replies: 0
    Last Post: 12-11-2002, 03:50 PM
  5. array question
    By Martijn in forum C++ Programming
    Replies: 1
    Last Post: 07-09-2002, 05:11 AM