www.digitalmars.com         C & C++   DMDScript  

c++.command-line - compiler error? data outside of specified array can be accessed! - error.cpp

reply Jens Friese <Jens_member pathlink.com> writes:
Hi,

to my profound astonishment, this code works just fine: (same as attachment)

#include <iostream.h>

double data[41][41];

int main(int argc, char* argv[]){

data[40][1000]=3.4;    // <<<<< HOW CAN THIS BE???????

cout << data[40][1000];

}

The console output is: 3.4 !!!!!

What am I doing wrong? Shoulnd't data[40][40] be the outmost field of data?

I wonder if you can solve this mystery... 

Kind Regards

Jens
Nov 30 2003
parent Cesar Rabak <csrabak ig.com.br> writes:
Jens Friese escreveu:
 Hi,
 
 to my profound astonishment, this code works just fine: (same as attachment)
 
 #include <iostream.h>
 
 double data[41][41];
 
 int main(int argc, char* argv[]){
 
 data[40][1000]=3.4;    // <<<<< HOW CAN THIS BE???????
 
 cout << data[40][1000];
 
 }
 
 The console output is: 3.4 !!!!!
 
 What am I doing wrong? Shoulnd't data[40][40] be the outmost field of data?
 
 I wonder if you can solve this mystery... 

The "mystery" part can be solved by a good book on C and C++. The summary is that these languages are not 'memory managed' and the program you wrote fall under the 'undefined behaviour' the standards for them talk about. One of these undefined consequences is the one you're seeing: _aparently_ it works but it carries a *bug* (if you've not coded it would be hidden, or worse, the program would exhibit erratic behaviour). In order to check for these errors you'll need to use addon libraries like Electricfence, YAMD, etc. HTH
 
 Kind Regards
 
 Jens
 
 

-- Cesar Rabak GNU/Linux User 52247. Get counted: http://counter.li.org/
Nov 30 2003