Skip to main content
gamedev.net
🔒 Locked

delete [] problem

Started by synthexp 10 replies 750+ views
Original Post
synthexp
synthexp
I''m having deleting a buffer dynamically allocated by the new operator. Here''s the code fragment. ---------------------------- BYTE* pBuff = new BYTE[dwWidth * dwHeight]; while (!m_pRAW.eof ()) { m_pRAW >> *pBuff++; // Get the data into the buffer } // Close/delete all objects delete [] (pBuff); m_pRAW.close (); ---------------------- *m_pRAW is an ifstream object that has been properly constructed. The error occurs right in the delete [] line. If I comment the while loop, the delete [] statement would execute fine. I''m not sure what went wrong but I''m suspecting that the *pBuff is referenced into the next unallocated memory space after the file reaches EOF. I already tried allocating a single byte for the last statement and re-referencing the pBuff pointer into the first element of the array but it still persist.
Regards,synthexp
synthexp
synthexp
Isn''t that when you use the new operator, the assignment would point the pointer at the first element in the array?

What do I have to do? I''ve tried allocating an extra byte for the forward referencing in the buffer. Still no good

==============
BYTE* pBuff = new BYTE[dwWidth + dwHeight + 1];
for (INT i = 0; i < dwHeight; i++)
for (INT j = 0; j < dwWidth; j++)
m_pRAW >> *pBuff++;
==============
I even made the changes above but still the error would still come out.

Regards,synthexp
Dragonskin
Dragonskin
You can treat pBuff just like an array (pBuff[0], etc.). Just use a counter to get the index location.

Or, using that counter again, I think you could use *(pBuff + counter). That SHOULD do the same thing (I'm full of shoulds).

Edited by - Dragonskin on December 3, 2001 8:31:23 PM
synthexp
synthexp
Dragonskin :

Did that and another error came out. Here''s the modified code as you suggested.

m_pRAW >> *(pBuff + i * j);

Regards,synthexp
synthexp
synthexp
Null :

Tried your suggestion and still the same error came out.

Regards,synthexp
synthexp
synthexp
An update
------------------------------
BYTE* pBuff = new BYTE[dwWidth * dwHeight];

for (INT i = 0; i < dwHeight; i++)
for (INT j = 0; j < dwWidth; j++)
m_pRAW >> *(pBuff + i * j);

// Convert the image
//ConvertImage (pBuff, dwWidth, dwHeight);
SendMessage (GetDlgItem (GetHDLG (), IDC_PRGCURRENT), PBM_STEPIT, 0, 0);

// Close/delete all objects
delete [] pBuff;

------------------------------
Well, I tried assigning only one byte to pBuff and was hoping that the loop would die after j reaches 1 but it turned that it did not. Because I assigned only one byte to pBuff and the memory space is good only for one byte or when j == 1, next assignment to pBuff would generate exception. Now I''m at loss on why it did not happen. Can anybody care to explain?

One more thing, how do I read the whole file and dump the whole of them into pBuff without going through the loop. If someone have any idea on how to read a RAW file using ifstream, I''ll be happy if someone would care to share it. Thanks.
Regards,synthexp
Brother Bob
Brother Bob
  
for (INT i = 0; i < dwHeight; i++)
for (INT j = 0; j < dwWidth; j++)
m_pRAW >> *(pBuff + i * j);

Should probably be:
  
for (INT i = 0; i < dwHeight; i++)
for (INT j = 0; j < dwWidth; j++)
m_pRAW >> *(pBuff + (i * dwWidth + j)); // <-- change


But why not replace it with:
  
m_pRAW.read(pBuf, dwHeight * dwWidth * sizeof BYTE);
synthexp
synthexp
Thanks Brother Bob,

The last suggestion worked. Instead of byte per byte copying, the whole file content is dumped to the buffer. Again, many thanks to all of you too.
Regards,synthexp

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.