Thread: VC++ 2003: stack corrupted around variable

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    19

    VC++ 2003: stack corrupted around variable

    Hi,
    i am using some working code from an already existing example program. I use Visual C++ 2003. Here is the method i am using:
    Code:
    void CIDAdmin::CStrToUchar(CString str, UCHAR *ucBuffer, ULONG *ulBufferLen)
    {
    	int Length = 0;
    	int DataLength = 0;
    	char cstr[] ="";
    	char strcstring[512] ="";
    	byte hexval=0x00;
    	int i = 0;
    	
    	Length = str.GetLength();
    	
    	for (i = 0; i<Length; i++)
    		strcstring[i] = str.GetAt(i);
    	
    	DataLength = Length / 2;
    	for (i = 0; i<DataLength; i++)
    	{
    		cstr[0] = strcstring[2*i];
    		cstr[1] = strcstring[2*i+1];
    		sscanf( cstr, "&#37;02x", &hexval );
    		ucBuffer[i]=hexval;
    		
    	}
    	
    	*ulBufferLen = DataLength;	
    }//CStringToUchar
    and when i run my program, i get "run-time check failure #2 - Stack around the variable 'cstr' was corrupted", which refers to the "char cstr[] =""; " of the method above i think. If i try to bypass the error, i also get "run-time check failure #2 - Stack around the variable 'hexval' was corrupted", which refers to the "byte hexval=0x00; ". The only differences -i have noticed so far- is that in my project, i use the "atlstr" header and my compiler and linker settings also differ from the example program... Any ideas?

    Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    char cstr[] = "";
    This declares a char buffer exactly one byte in size. Obviously, that isn't big enough.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Stack corruption around a variable happens when you thrash the memory (hence overwrite the bounds of your array on the stack).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help
    By hellBOY in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 11:59 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM