Thread: Corrupted Stack...

  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,210
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char array[SIZE]
    has exactly SIZE bytes... what you ask is what you get.

    Valid indexes are from 0 to SIZE-1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM