Sign in to follow this  
pavel989

Runtime error with delete

Recommended Posts

pavel989    114
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?

Share this post


Link to post
Share on other sites
openglJunkie    100
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.

Share this post


Link to post
Share on other sites
Gooberius    226
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.

Share this post


Link to post
Share on other sites
pavel989    114
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!

Share this post


Link to post
Share on other sites
Palidine    1315
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

Share this post


Link to post
Share on other sites
rip-off    10979
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]

Share this post


Link to post
Share on other sites
Nyarlath    104
Quote:
Original post by pavel989
can std::vector<char> 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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this