Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to avoid Buffer Overrun?!

How to avoid Buffer Overrun?!

Scheduled Pinned Locked Moved C / C++ / MFC
performancetutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fareed Rizkalla
    wrote on last edited by
    #1

    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:

    R M S R 4 Replies Last reply
    0
    • F Fareed Rizkalla

      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:

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • F Fareed Rizkalla

        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:

        M Offline
        M Offline
        Moak
        wrote on last edited by
        #3

        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 with new and delete

        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 see new/delete in a code review I get very suspicious. That's just my personal design philosophy, hope it helped. :) /M

        Webchat in Europe :java: Now with 29% more Twitter

        1 Reply Last reply
        0
        • F Fareed Rizkalla

          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:

          S Offline
          S Offline
          Saurabh Garg
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • F Fareed Rizkalla

            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:

            R Offline
            R Offline
            RedZombie125
            wrote on last edited by
            #5

            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?*

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups