Thread: Interesting Error

  1. #1
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Interesting Error

    I was writing a small program just to mess around and suddenly I got an error that

    Run-Time Check Failure #2 - Stack around the variable 'letter' was corrupted.
    I was wondering what causes them, and kinda what do they indicate?

    Here is the entire offending code...

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <sstream>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	stringstream st;
    	srand( (unsigned)time(NULL) );
    	for( int i = 0; i < 256; i++ )
    	{
    		int num = rand() % 65;
    		num += 65;
    		char letter;
    		itoa( num, &letter, 10 );
    		st << letter;
    	}
    	string theString = st.str();
    
    	cout << theString << endl;
    	return 0;
    }
    Perhaps I am just tired(which I am) but I see nothing wrong with it. If anyone could enlighten me, perhaps you can help my tired eyes see what I am doing wrong. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what compiler are you using? I used VC++ 6.0, changed _tmain to main and TCHAR to char. Your program ran without error on XP.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Edit: I could swear this was on the C board. I believe itoa was incorrectly used. It takes a numerical value and stores it as a string into a buffer you give it, which you only passed a reference to a char as.

    Simple solution: no need for itoa

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <sstream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	stringstream st;
    	srand( (unsigned)time(NULL) );
    	for( int i = 0; i < 256; i++ )
    	{
    		char letter = (rand() % 65) + 65;
    		st << letter;
    	}
    	string theString = st.str();
    
    	cout << theString << endl;
    	return 0;
    }
    Last edited by Tonto; 09-29-2005 at 01:46 PM.

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Hmm thats right, forgot you could create a letter with a number it converts automatically.

    As for compiler using VC .net 2003.

    Still wondering exactly what that error means and how it happens, but thanks tonto, much easier!

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dpro
    Hmm thats right, forgot you could create a letter with a number it converts automatically.

    As for compiler using VC .net 2003.

    Still wondering exactly what that error means and how it happens, but thanks tonto, much easier!
    as tonto pointed out, the function was expecting a null-terminated string, and you were passing it a pointer to a single char. So at runtime, the function couldn't find the end of the string causing undefined behavior. I guess VC++ 6.0 is more forgiving and doesn't catch many errors.

  6. #6
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by Ancient Dragon
    as tonto pointed out, the function was expecting a null-terminated string, and you were passing it a pointer to a single char. So at runtime, the function couldn't find the end of the string causing undefined behavior. I guess VC++ 6.0 is more forgiving and doesn't catch many errors.

    Oh, duh! Sorry, was totally missing the point, I see you are right, thanks for pointing me in the right direction!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM