2

I'm trying to get better at C++ (I know a little). I'm working on character arrays. I found an exercise where the objective is to reverse a character array (after I convert it from an integer). I'm getting the following error (using VS2005):

Run-Time Check Failure #2 - Stack around the variable 'revBuffer' was corrupted.

When I step through the code, I notice the following:

revBuffer = 0x0012fe40 "100899ÌÌÌÌÌÌÌÌÌÌ998001"

The relevant code is below.

    char buffer[5];
    char revBuffer[5];
    int i;
    int j=5;
    long number = 998001;

    itoa(number, buffer, 10);

    for(i=0; i<strlen(buffer);i++)
    {
        revBuffer[j] = buffer[i];
        j--;
    }

Any help would be great. TIA!

7
  • Just trace through your code - look at the values of the indices that'll be used in the first iteration of the for loop. Commented May 26, 2010 at 15:22
  • 2
    Not related to your bug, but you should hoist strlen(buffer) out of the for loop expression, so that it doesn't get computed needlessly in every loop. strlen works by scanning the entire string until it sees the null terminator. I think that leaving strlen in the loop would be a form of "premature pessimization" (IMHO). Commented May 26, 2010 at 15:36
  • @Emile Premature pessimization, haha. Commented May 26, 2010 at 15:36
  • This is c not c++, I retagged appropriately. The C++ code would be using std::string and .at() which would have indicated the out_of_range issue. Commented May 26, 2010 at 15:37
  • 1
    @Emile: I dunno, buffer is 7 whole bytes (once the bugs are fixed). Count 'em. Count 'em 7 times if you like: I for one am happy to wait while you do that. Agreed though that it's a bad habit in general to turn an O(n) op into an O(n^2) op for no reason. @Matthieu, if you're going to insist on proper use of libraries, then "the" C++ code would use copy_backward, not at(). End of discussion ;-) Commented May 26, 2010 at 16:47

5 Answers 5

7

You are overindexing revBuffer. It is size 5, which means you can index it from 0 to 4, but the first index you use in it in your loop is 5.

Sign up to request clarification or add additional context in comments.

1 Comment

Damn. That was a dumb mistake on my part. All the answers and suggestions were a big help. This site rocks! Thanks!
6

The number you are converting has six digits - the buffer you are using is only big enough to hold 4 plus a null terminator. Make the buffer bigger.

Comments

2

While your excercise is helpful you should keep in mind that there also is:

std::reverse(buffer, buffer + 5);

Comments

2

In addition to what others have said, It doesn't appear that a null terminator will be placed in the last character of revBuffer even after you fix all your indexing issues. You will need to be sure to do

revBuffer[strlen(buffer)] = '\0';

This is also frought with problems because it assumes that buffer is properly terminated :)

Comments

0

For a start, you're trying to use a buffer that's 5 characters long to store a 6 digit number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.