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

This is a discussion on Accessing uninitialised element in array does not throw an error :O ?? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have just started learning C++, coming with good knowledge in Java and some other languages (assembly,CSP,haskell, etc.). I ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

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

    Hi,

    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.

    Code:
    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 = "";
    			counter_y++;
    		}
    	}
    	stringstream(temp) >> matrix[counter_x][counter_y];
    
    	this->display();
    }
    ------------------

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

    Code:
    stringstream(temp) >> matrix[counter_x][counter_y];
    . Matrix is my predefined 3x3 array. If i change the above code in something like :
    Code:
    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
    Code:
    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
    Posts
    14,185
    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
    Posts
    8,893
    Basically, when it comes to behavior when you do something wrong, C++ is like assembly, not Java.
    All the buzzt!
    CornedBee

    "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, 03:39 AM
  5. accessing a scruct element by relative address?
    By Birdy27 in forum C Programming
    Replies: 3
    Last Post: 02-11-2006, 10:30 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21