Thread: Accessing uninitialised element in array does not throw an error :O ??

  1. #1
    Registered User
    Join Date
    Aug 2011

    Accessing uninitialised element in array does not throw an error :O ??


    I have just started learning C++, coming with good knowledge in Java and some other languages (assembly,CSP,haskell, etc.). I am trying to make a console application that reads user input for creating a matrix (using my own Matrix class) and methods for calculations of the array (multiplication, addition etc.). So far so good. However there is something that bothers me. I have created this method for getting the values of the array as a string.

    void Matrix_::fillMatrix(string values) {
    	int counter_x = 0;
    	int counter_y = 0;
    	std::string temp = "";
    	for(int i = 0; i < values.size(); i++) {
    		if(values[i] != ' ') {
    			temp += values[i];
    		} else {
    			stringstream(temp) >> matrix[counter_x][counter_y];
    			temp = "";
    	stringstream(temp) >> matrix[counter_x][counter_y];

    It is still unfinished though, because there is something that bothers me.

    stringstream(temp) >> matrix[counter_x][counter_y];
    . Matrix is my predefined 3x3 array. If i change the above code in something like :
    stringstream(temp) >> matrix[counter_x][12];
    it should give an error, since i am accessing an undefined element. However this is not the case. There is no error!! How is this possible? :O I have also tried this
    cout << matrix[0][5];
    and instead of getting an error, it prints a negative value (-1167...). Anyone can help me with that?

    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    C++ assumes you are not an idiot. (That is to say, it does not do range checking of array indices as part of the language.) If you happen to land on memory that isn't important, then it will just overwrite whatever is there. If you happen to land on memory that is important, then you'll get a run-time error.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Basically, when it comes to behavior when you do something wrong, C++ is like assembly, not Java.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  2. error: array type has incomplete element type
    By gerger in forum C Programming
    Replies: 8
    Last Post: 10-05-2010, 07:40 AM
  3. Accessing a struct element in a different class...
    By Sparrowhawk in forum C++ Programming
    Replies: 0
    Last Post: 03-09-2009, 04:24 PM
  4. Accessing a data element in a function
    By Zooker in forum C Programming
    Replies: 5
    Last Post: 02-05-2009, 04:39 AM
  5. accessing a scruct element by relative address?
    By Birdy27 in forum C Programming
    Replies: 3
    Last Post: 02-11-2006, 11:30 AM

Tags for this Thread