Thread: Question about Arrays

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    Question about Arrays

    How come when I run this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int examplearray[95];
    	examplearray[95] = 50;
    	cout << examplearray[95] << "\n";
    	system("pause");
    	return 0;
    }
    After the program closes, it gives this message:
    Run-Time Check Failure #2 - Stack around the variable 'examplearray' was corrupted.

    Is it because I have to give a value to every element of the array?

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    indexing starts with 0 so you need to assure you are indexing 0-94 if its size is 95. Alas you index out of bounds, thus it fails irl.

    RTFM C Guide--1.2 Indentifiers

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    int examplearray[95];
    examplearray[95] = 50;
    cout << examplearray[95] << "\n";
    No, it is because you are trying to access an element that doesnt exist.
    Array indices work from 0 to (n - 1).
    Your's would be 0 to 94.

    Also, It would help if you read a book or tutorial before you jump to posting.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    OK sorry, I knew that, I my brain just wasn't working at that time.
    I will try to figure out my problems on my own before jumping to posting for now on, sorry guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM