Skip to main content
GameDev.net gamedev.net
🔒 Locked

Runtime error with delete

Started by pavel989 Dec 18, 2008 at 4:17 PM 7 replies 1.3k views
Original Post
pavel989
pavel989
delete[] buf; where buf is char *buf = new char[row[0].length()]; the row thing is a vector. gives me a runtime Debug Error: HEAP CORRUPTION DETECTED: after Normal Block (#195) at 0x003D8348. CRT detected that the application wrote to memory after end of heap buffer. -- that makes no sense to me. can anyone at least explain that?
DevFred
DevFred
We need more code.
openglJunkie
openglJunkie
It appears the problem isn't with delete, but when you assign buffer to a new char. Try taking off the [row[0].length()] and see if that doesn't fix it and then you can determine what action to take next.

Quote:

HEAP CORRUPTION DETECTED: after Normal Block (#195) at 0x003D8348. CRT detected that the application wrote to memory after end of heap buffer.
Gooberius
Gooberius
You wrote past the end of the buffer you allocated. Make sure you are writing exactly row[0].length() number of chars to buf[] - a common mistake is getting it wrong by 1 so you end up writing row[0].length()+1 bytes and falling off the end of the memory you allocated into an area of memory that you shouldn't be writing to.
pavel989
pavel989
i was thinking that i was off. hm ill check that.

also, taking of the .length() gave me an error too. something about bad memory

______________________________________________________________________________

yeah i was off by 1!

thanks!
Palidine
Palidine
Well, actually IIRC, it doesn't mean your allocation is necessarily bad. What it really means is that somewhere earlier in your program you overwrote an array or wrote to a bad pointer. The error could be anywhere. Often memory errors don't show up until the next new/delete is called. That's the fun of dynamic memory errors.

So, go back and look at everywhere you are using pointers/arrays and make sure your logic is correct.

-me
rip-off
rip-off
Why not:
std::vector<char> buf(row[0].length()];

Most implementations of std::vector<> will do bounds checking during Debug builds, allowing you to track down you problem. In addition, you no longer need to manually delete[] buf. Everyone wins! [smile]
pavel989
pavel989
can std::vector buf(row[0].length()];

be used like char*?
Nyarlath
Nyarlath
Quote:
Original post by pavel989
can std::vector buf(row[0].length()];
be used like char*?

std::vector<char> vec(row[0].length()];char* buf = vec.empty() ? 0 : &vec[0];

That's an abuse but p is valid until the elements of vec are reallocated (may happen on resize or on reserve).

Topic Locked

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

Sign in to reply to this topic.