How to avoid Buffer Overrun?!
-
I understand the scientific term, but I don't know how to avoid them. I've always allocated memory with pointers/new and deallocated them with delete variable. Suddenly started getting buffer overrun yet my allocations are accurate and addressing is accurate. :confused:
-
I understand the scientific term, but I don't know how to avoid them. I've always allocated memory with pointers/new and deallocated them with delete variable. Suddenly started getting buffer overrun yet my allocations are accurate and addressing is accurate. :confused:
Hard to help you without seeing any code at all. However, here's a pretty good literature on the topic: An introduction to memory damage problems[^].
“Follow your bliss.” – Joseph Campbell
-
I understand the scientific term, but I don't know how to avoid them. I've always allocated memory with pointers/new and deallocated them with delete variable. Suddenly started getting buffer overrun yet my allocations are accurate and addressing is accurate. :confused:
Hi, two tips straight from our C++ coding standard:
- don't guess the size of a memory block when reading or writing. Never ever.
- use data containers (
STL
) when ever possible, avoid manual memory allocations withnew
anddelete
Generally I use STL or my own buffer/string classes instead of dealing with low-level memory handling, for example
std::vector<unsigned char>
. It is just too easy to miscalculate an array size, even a single byte too less can give you a memory exception in production code, not even speaking about the possibility of code injection via a buffer overrun. If I seenew
/delete
in a code review I get very suspicious. That's just my personal design philosophy, hope it helped. :) /MWebchat in Europe :java: Now with 29% more Twitter
-
I understand the scientific term, but I don't know how to avoid them. I've always allocated memory with pointers/new and deallocated them with delete variable. Suddenly started getting buffer overrun yet my allocations are accurate and addressing is accurate. :confused:
How did you determine if are having buffer overruns? Using a tool? Your program is crashing? You get memory leaks? This is important to determine how to detect the problametic code. Also just to state the obvious if there are no allocation or deallocation problems then you can't have any memory related problems. So the fact that you do have buffer overrun means that there must be a problem somewhere. I know it is bit pedantic but it is important to say it aloud. -Saurabh
-
I understand the scientific term, but I don't know how to avoid them. I've always allocated memory with pointers/new and deallocated them with delete variable. Suddenly started getting buffer overrun yet my allocations are accurate and addressing is accurate. :confused:
I don't actually know what you're trying to do with this, but a pretty common mistake is miscounting the number of elements you're trying to allocate, so while you might think you've allocated accurately, you could be one off, which causes lots of problems. :( For example: "Phil Collins" Has a total of 13 elements, even though there's actually 12 characters in "Phil Collins". A common mistake is to forget the '\0' character at the end of every C-style string, so the whole thing is thrown off. (BTW, I used "Phil Collins" because I named a tree after him in a video game I'm working on... Does Phil Collins work as a good name for a tree..?:confused:) I hope that helps; bits of source code would probably help though. *I can haz a cookie?*