Just a couple of questions

This is a discussion on Just a couple of questions within the C++ Programming forums, part of the General Programming Boards category; Why can I seem to use outside the Arrays limit [10][10]? For instance if I use the following code, it ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    17

    Just a couple of questions

    Why can I seem to use outside the Arrays limit [10][10]? For instance if I use the following code, it assigns and outputs the arrays element.

    Code:
    code[1][50] = "outside array";
    cout << code[1][50];
    Why does an extra string get added on to sepstr in the seperate_string function?

    Are their any other problems with my code?

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <istream>
    
    #define WIDTH 10
    #define HEIGHT 10
    
    using namespace std;
    
    string code[HEIGHT][WIDTH];
    
    void somefunc(void);
    void add_double_quotes(string &str);
    void clear_array(void);
    void clear_ibuffer(void);
    void seperate_string(string &str);
    
    int main(void)
    {
    	somefunc();
    	//cout << code[0][0] << code[1][0] << code[1][1] << endl;
    	//cout << code[3][0] << code[3][1] << code[3][2] << code[3][3] << code[3][4] << endl;
    	//clear_array();
        
    return 0;
    }
    
    void somefunc(void)
    {
    int x;
    string uinput;
    
        cout << "1. group \t2. rank \n3. rank1 \t4. names \n0. quit\n" << endl;
        do{
    	    cin >> x;
    	    clear_ibuffer();
            switch(x)
    	    {
    	    case 1:
    		    code[0][0] = "group ";
    		    break;
    	    case 2:
    		    code[1][0] = "rank";
    		    break;
    	    case 3:
    		    code[1][1] = "rank1";
    		    break;
    	    case 4:
    		    getline(cin, uinput);                
    	    default:
    		    x = 0;
    		    break;
    	    }
        }while(x != 0);
    
        seperate_string(uinput);
    }
    
    void add_double_quotes(string &str)
    {
        str = '\"' + str + '\"';
    }
    
    void clear_array(void)
    {
    	for (int n=0;n<HEIGHT;n++)
          for (int m=0;m<WIDTH;m++)
          {
    	      code[n][m] = "\0";
          }
    }
    
    void clear_ibuffer(void)
    {
        cin.clear();
        cin.ignore(100, '\n');
    }
    
    void seperate_string(string &str)
    {
    int x = 0;
    
        istringstream iss(str);
    
        do{
            string sepstr;
            iss >> sepstr;
            ++x;
    	add_double_quotes(sepstr);
    	//cout << sepstr << endl;
    	code[2][x] = sepstr;
        }while (iss);
    
            for (int a=0;a<x;a++)
            {
    	        cout << code[2][a] << endl;
            }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by redsfan
    Why can I seem to use outside the Arrays limit [10][10]?
    Undefined behaviour, so it might appear to work, but that does not make it correct. That said, code[1][50] is an element within the boundary of the 2D array, i.e., code[6][0], but relying on this index/pointer arithmetic would be error prone.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is just undefined behavior. A C(++) program does not check array indices at runtime. You do things to memory that you are not supposed to touch and you either get away with it (the program appears to work) or you don't (the program crashes immediately or starts to do weird things somewhere down the line because you messed up its state in memory).

    In this particular case you are not storing the string at indices (1, 50). The lay-out of a two-dimensional array is the same as that of a single-dimensional array (one contiguous block of memory). The index of the item in a corresponding single-dimensional array is calculated like this: first_dim * second_dim_size + second_dim. You are accessing item # 1 * 10 + 50 = 60 starting from the start of the array, which in the two dimensional array is at code[6][0]. That is, in this case you are accesing an item in the same array (although not one that you meant to) and there is no reason to not let you get away with this.
    Last edited by anon; 04-13-2011 at 10:02 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Undefined behaviour, so it might appear to work, but that does not make it correct. That said, code[1][50] is an element within the boundary of the 2D array, i.e., code[6][0], but relying on this index/pointer arithmetic would be error prone.
    Thanks, that explains some strange behavior I was getting, when inputting more than 10 strings for case 4:

    what is the best way to stop a user adding more strings than I want, if statement?

    Code:
    if (string_count > 10)
    {
        cout << "Too many strings entered";
    }

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    17
    Quote Originally Posted by anon View Post
    This is just undefined behavior. A C(++) program does not check array indices at runtime. You do things to memory that you are not supposed to touch and you either get away with it (the program appears to work) or you don't (the program crashes immediately or starts to do weird things somewhere down the line because you messed up its state in memory).

    In this particular case you are not storing the string at indices (1, 50). The lay-out of a two-dimensional array is the same as that of a single-dimensional array (one contiguous block of memory). The index of the item in a corresponding single-dimensional array is calculated like this: first_dim * second_dim_size + second_dim. You are accessing item # 1 * 10 + 50 = 60 starting from the start of the array, which in the two dimensional array is at code[6][0]. That is, in this case you are accesing an item in the same array (although not one that you meant to) and there is no reason to not let you get away with this.
    yep, that makes sense, cheers

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by redsfan
    what is the best way to stop a user adding more strings than I want, if statement?
    That is one way, but you have to be careful in what the condition should be and where to use it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  2. couple questions...
    By Rune Hunter in forum C# Programming
    Replies: 4
    Last Post: 10-03-2004, 10:57 AM
  3. A couple of OOP questions
    By codec in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2004, 07:18 PM
  4. Couple of Questions...
    By oobootsy1 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2004, 05:05 PM
  5. New to C++/ Couple of questions...
    By danielthomas3 in forum C++ Programming
    Replies: 13
    Last Post: 04-14-2002, 03:46 PM

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