Corrupted Stack...

This is a discussion on Corrupted Stack... within the C Programming forums, part of the General Programming Boards category; Hello... As soon as this function returns the Debug Window in VisualC++ reports this error: "Run-Time Check Failure #2 - ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    41

    Corrupted Stack...

    Hello...
    As soon as this function returns the Debug Window in VisualC++ reports this error:

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

    Code:
    int cFile::GetType(LPCWSTR filename)
    {
    	HANDLE hFile = NULL;
    	char fBuffer[3];
    	DWORD nread = 0;
    
    	hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	if (hFile == NULL)
    		return -1;
    
    	ZeroMemory(fBuffer, sizeof(char) * 4);
    	::ReadFile(hFile, fBuffer, 4, &nread, NULL);
    	if (nread < 4)
    		return -1;
    
    	if (fBuffer[0] == 82 && fBuffer[1] == 73 && fBuffer[2] == 70 && fBuffer[3] == 70)
    		return FILE_TYPE_AUDIO;
    
    	if (fBuffer[0] == 77 && fBuffer[1] == 84 && fBuffer[1] == 104 && fBuffer[1] == 100)
    		return FILE_TYPE_MIDI;
    
    	return 0;
    }
    Why??

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Because you're calling ZeroMemory() on a buffer of only 3 chars and telling it to zero out 4.

    BTW, this is the C section, and that's C++ code.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    ReadFile also uses 4 bytes buffer
    You should close the file in this function ( you do not return handle for future use)

    your use of "magic" numbers is also not too good
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    41
    yes...you're right

    I've tried declaring fBuffer[4] instead of fBuffer[3] and everything works fine...
    Anyway shouldn't fBuffer[3] be 4 bytes long (of type char)?

    Thanks

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    char array[SIZE]
    has exactly SIZE bytes... what you ask is what you get.

    Valid indexes are from 0 to SIZE-1
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    41
    Ooops!

    I was thinking that an array had indexes from 0 to size...Well thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

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