Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Crash when using multidimensional arrays
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
capochavy




PostPosted: Tue Dec 09, 2008 8:59 pm   Post subject: Crash when using multidimensional arrays

I am using Dev-C++ version 4.9.9.2 at school for an assignment, and whenever i try running a program that uses a multidimensional array, the program crashes, and an error box appears saying that there was an error with the program and it will need to be restarted, and that an error log was created. I have yet to find where that error log was created. What is wrong with my program?

This is my code:

code:

#include <iostream>
#include <conio.h>
using namespace std;
int main () {
    int location [2][11];
    for (int y = 0; y <= 12; y++){
        for (int x = 0; x <= 2; x++){
            location [x][y] = 0;
        }
    }
    getch ();
}
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Tue Dec 09, 2008 9:11 pm   Post subject: RE:Crash when using multidimensional arrays

You're going beyond the bounds of the array in the for loop. Arrays start at index zero, so:
code:
int location[5];

Will have elements at index 0, 1, 2, 3, 4.

Based on this info, the fix to your code should be obvious.
capochavy




PostPosted: Tue Dec 09, 2008 9:26 pm   Post subject: Re: Crash when using multidimensional arrays

My bad, i posted the wrong code. I checked this time to make sure it does not go out of range.

code:

#include <iostream>
using namespace std;
int main () {
    int location [2][11];
    for (int y = 0; y < 12; y++){
        for (int x = 0; x < 3; x++){
            location [x][y] = 0;
        }
    }
}
[Gandalf]




PostPosted: Tue Dec 09, 2008 9:35 pm   Post subject: RE:Crash when using multidimensional arrays

Clue: You didn't fix the problem, you actually broke the part that already works. Look carefully at what I posted above, what's the relationship between the size of the array (ie. 5) and the upper index of that array (ie. 4)?
wtd




PostPosted: Tue Dec 09, 2008 9:36 pm   Post subject: RE:Crash when using multidimensional arrays

You're still going out of bounds.
capochavy




PostPosted: Tue Dec 09, 2008 9:42 pm   Post subject: RE:Crash when using multidimensional arrays

So there isn't a location [2][11]? But when using 1D arrays there is (int location [2] works when calling location [2])...
gianni




PostPosted: Tue Dec 09, 2008 10:03 pm   Post subject: RE:Crash when using multidimensional arrays

Also, it won't make a huge difference, but I think your loops are backwards. Your for loops should be reversed.
wtd




PostPosted: Tue Dec 09, 2008 10:11 pm   Post subject: RE:Crash when using multidimensional arrays

When you declare:

code:
int foo[2];


You have an array with space for two integers.

code:
foo[0]


And:

code:
foo[1]
Sponsor
Sponsor
Sponsor
sponsor
capochavy




PostPosted: Tue Dec 09, 2008 10:13 pm   Post subject: RE:Crash when using multidimensional arrays

But my past programs have worked when using
code:

int hello [3];
hello [3] = 1;

Why is that?
[Gandalf]




PostPosted: Tue Dec 09, 2008 10:23 pm   Post subject: RE:Crash when using multidimensional arrays

I'm not sure which compilers allow or disallow that, or which just give a warning, but that's just the nature of C++. That's also the exact reason it's not a good language to begin programming with.

If I'm remembering correctly, the exact reason is that hello[3] just means *(hello + sizeof(int) * 3) or some such, which is a valid address in memory to dereference. Perhaps I'm completely off. Razz
capochavy




PostPosted: Tue Dec 09, 2008 10:26 pm   Post subject: RE:Crash when using multidimensional arrays

I haven't had any problems coding so far except for that... Anyways, thanks for all the help.
btiffin




PostPosted: Tue Dec 09, 2008 11:38 pm   Post subject: RE:Crash when using multidimensional arrays

Off topicy jokey smurf post

A classic off by one. We should be asked when we are two years old if we plan on getting into computer programming. Those kids that raise there hands will be taught how to count starting at zero. Wink

And then, just for fun, when the words ordinal, cardinal and nominal are taught, the computer group will use the ordinal first equal cardinal zero and nominal 0. Ordinal zeroth will be a null brain exception and the student will then abend for speaking it. Smile

Cheers
[Gandalf]




PostPosted: Wed Dec 10, 2008 1:53 am   Post subject: Re: RE:Crash when using multidimensional arrays

btiffin @ 2008-12-09, 11:38 pm wrote:
Zeroth will be a null brain exception

Laughing No reason to be insulting, btiffin!

Also, my previous example should be *(hello + 3), no need for the sizeof in this case.
btiffin




PostPosted: Wed Dec 10, 2008 10:49 am   Post subject: Re: RE:Crash when using multidimensional arrays

"Gandalf" @ Wed Dec 10, 2008 1:53 am wrote:
btiffin @ 2008-12-09, 11:38 pm wrote:
Zeroth will be a null brain exception

Laughing No reason to be insulting, btiffin!

Also, my previous example should be *(hello + 3), no need for the sizeof in this case.


I didn't even think about that angle. Sorry Zeroth. And now I have to abend as my null brain spins on the paradox. Wink
Cheers
Edit; fixed quoting
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: