Vector with values outside length

This is a discussion on Vector with values outside length within the C++ Programming forums, part of the General Programming Boards category; Hi! To get some practice in C++ (and work towards the problem I am going to use C++ for), I ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Location
    Antwerp, Belgium
    Posts
    12

    Vector with values outside length

    Hi!

    To get some practice in C++ (and work towards the problem I am going to use C++ for), I wrote a program that would translate strings, that contain digits, to integers. To do so I:
    1. Scale down the value of the characters, so that thay correspond to digits 0 - 9 of type int (from string to vector)
    2. add up the items in the vector so that they become one single integer.

    All seems to be working well, but for a bunch of values that are miraculously generated outside the domain of the vector. The fragment of the code, where it seems to go wrong:

    Code:
    vector<int> Dscale (const string& Txt) { // translates digits in a text string to digits in a vector
    
        vector<int> Dig(0);
        int n = -1;
    
        while (Txt[++n]) {
            Dig.push_back((Txt[n])-48);
            cout << "Dscale: " << Txt[n] - 48 << endl;
        }
        cout << "Txt size " << Txt.size() << " and Dig size " << Dig.size() << endl;
        Dig.resize(Txt.size());
        cout << "Txt size " << Txt.size() << " and Dig size " << Dig.size() << endl;
    
        n = -1;
        while (Dig[++n]) {
            cout << "Dscale vector item " << n << " has value " << Dig[n] << endl;
        }
    
        return Dig;
    }
    I added the cout, so I could see what happens, for the input 1234 (in Txt), the output is:

    Code:
    Dscale: 1
    Dscale: 2
    Dscale: 3
    Dscale: 4
    Txt size 4 and Dig size 4
    Txt size 4 and Dig size 4
    Dscale vector item 0 has value 1
    Dscale vector item 1 has value 2
    Dscale vector item 2 has value 3
    Dscale vector item 3 has value 4
    Dscale vector item 4 has value 196715
    Dscale vector item 5 has value 4096
    Dscale vector item 6 has value 4064464
    Dscale vector item 7 has value 4064464
    In short, so you don't have to waste time to figure out the meaning from the code:
    - the Dscale series, gives the values in the string Txt
    - the next two rows give the lengths of both string Txt and vector Dig
    - the Descale vector series, gives the values in the vector Dig

    The last being produced in the condition: while (Dig[++n])
    The reading of Dig is restricted to the first four items for: while (n++ < series.size())

    so it needn't be a problem (and the program in total is working as it should). I'm simply very curious where the other items come from. Especially because the vector is declared empty (vector<int> Dig(0).

    Cheers, Carola

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Where the values come from..? Memory.
    If the index is greater or equal to the size of the vector, the effect is undefined. That means anything may happen. Of course, usually, it will just return memory that is either not owned by the vector or it will return memory that is owned by the vector but never changed, meaning they're still old values.
    The reason for this is that indexing it may be very common, and a security check to make sure the index is within range may become costly, so it's expected the programmer does this. There's another member function named at that does check it for you, although I've never used it, as I simply check it myself.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    you are intruding on another variables address space, maybe code too, C++ does not enforce bounds checking, it gives you the freedom to overrun the buffer. This is a positive thing for C++ programmers, but a rude awakening if you are used to an interpreted language like java or C#. The assumption in C/C++ is that you as the programmer know better than the compiler, and by extension the guy who programmed the compiler, what your program needs to do, and making the language inflexible to coddle to the unenlightened is a bad thing for everyone else that doesn't want or need their hand held.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Note that use can use the function at() instead of operator [] with the vector. That function does bounds checking for you, so if you accidentally go passed the bounds of the vector it will throw and exception and your program can handle it properly. You wouldn't want to do it in time-critical parts of your application, but in most cases it's not a bad idea to use at() instead.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In any case you have found a very bizarre way to loop over a string (and a std::string doesn't even have to be null-terminated!)

    Code:
    for (unsigned i = 0; i != Txt.size(); ++i) {
        ...
    }
    
    //to loop over the vector
    for (unsigned i = 0; i != Dig.size(); ++i) {
        ...
    }
    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).

  6. #6
    Registered User
    Join Date
    Aug 2009
    Location
    Antwerp, Belgium
    Posts
    12
    Thanks a lot for all your answers, I forgot that for a vector bounds are not checked automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf return values
    By pirog in forum C Programming
    Replies: 15
    Last Post: 09-13-2009, 03:58 AM
  2. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  3. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  4. Count the length of strintg without using strlen?
    By yukon in forum C Programming
    Replies: 9
    Last Post: 10-01-2001, 08:32 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

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