delete[] word array = error

Started by
5 comments, last by twan 19 years, 6 months ago
Hi, I tried the search but that gave a odbc error so i'm sorry to post such a noob question :$. At the delete[] data; I get the next error: Debug Error! DAMAGE: after Normal block(#76 at 0x(bignumber) Can somebody tell me what i'm doing wrong ? If i replace the WORD with a int and the fscanf %u with %i everything works fine.

WORD *data = new WORD[amount];

for(i=0;i<Header.iIndices;i++)
{
  fscanf(file, "%u", &data);
}

delete[] data;
Advertisement
My best guess is that you are writing out of bounds. Are you sure 'Header.iIndices' is smaller or equal to 'amount'
A word is 2 bytes, and an unsigned integer is 4 bytes (in this case), so fscanf writes 2 bytes past the end of the array.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Try "%hu".
Quote:Original post by smart_idiot
A word is 2 bytes, and an unsigned integer is 4 bytes (in this case), so fscanf writes 2 bytes past the end of the array.


I knew that a word is 2 bytes and a int 4 but why does fscanf write 2 bytes past the end of the array ?

Yes both values are the same. I get the same error when i hardcode the values.
Because %u is an unsigned integer, it's an array of words, and an integer is 2 bytes longer than a word. As hh10k pointed out, use the h modifier.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Ok i replaced the u with a hu and now i get no error's. But however the other problem now is that the data in my array makes totally no sense :s. Instead of 0 2 it's now 5232 or something like that.
Is that because i use a text file instead of a binary file ? If i read the data as a %i i get the correct data. Can somebody please give me a push into the right direction ?

ps. strange i looked in my book and in the msdn help file but i couldn't find a h for fscanf.

[Edited by - twan on October 23, 2004 10:14:41 AM]

This topic is closed to new replies.

Advertisement