🔒 Locked
delete [] problem
Advertisement
Original Post
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.
|
Use increments wisely.
[Resist Windows XP''s Invasive Production Activation Technology!]
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.
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.
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
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
Don''t make is so complex.
[Resist Windows XP''s Invasive Production Activation Technology!]
|
[Resist Windows XP''s Invasive Production Activation Technology!]
quote:
Original post by synthexp
Tried your suggestion and still the same error came out.
Well, then:
1) You have more errors than the obvious one I pointed out. You could be overrunning your buffer. You are using an unsafe input method, so that''s a possibility.
2) You wrote something incorrectly.
[Resist Windows XP''s Invasive Production Activation Technology!]
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.
------------------------------
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.
Isn''t there something like ''file.read(buffer,size)''? I don''t use the C++ i/o library much. With the C library it uses ''fread(buffer,size,count,file)'', but that''s not very useful to you
.
[Resist Windows XP''s Invasive Production Activation Technology!]
[Resist Windows XP''s Invasive Production Activation Technology!]
|
Should probably be:
|
But why not replace it with:
|
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement