c++.command-line - compiler error? data outside of specified array can be accessed! - error.cpp
- Jens Friese (20/20) Nov 30 2003 Hi,
- Cesar Rabak (15/42) Nov 30 2003 The "mystery" part can be solved by a good book on C and C++.
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 begin 0644 error.cpp ` end
Nov 30 2003
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. HTHKind Regards Jens-- Cesar Rabak GNU/Linux User 52247. Get counted: http://counter.li.org/
Nov 30 2003