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. Other Discussions
  3. The Weird and The Wonderful
  4. Practice safe programming !

Practice safe programming !

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialquestioncode-review
12 Posts 10 Posters 5 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.
  • L Lost User

    Here is an example from our current development effort:

    CYesNoDlgBar *yesNoBar = new CYesNoDlgBar(this, prompt);
    yesNoBar->SetCadPtr(this);
    ShowCadBarModal(yesNoBar);
    ret = yesNoBar->GetResult() ? 1 : 0;
    if (yesNoBar)
    {
    delete yesNoBar;
    yesNoBar = NULL;
    }

    Unfortunately I can't track down which developer put in this piece of code as we changed source control and lost that history otherwise he would have got a piece of my mind, as would the one who was supposed to do the code review. Cheers, Brett

    N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #3

    Somewhat off topic: Never understood the people who allocate objects on heap when it is easier and safer to do it on stack. I.e.:

    CYesNoDlgBar yesNoBar(this, prompt);
    yesNoBar.SetCadPtr(this);
    ShowCadBarModal(&yesNoBar);
    ret = yesNoBar.GetResult() ? 1 : 0;

    Programming Blog utf8-cpp

    K Y G J 4 Replies Last reply
    0
    • L Lost User

      Here is an example from our current development effort:

      CYesNoDlgBar *yesNoBar = new CYesNoDlgBar(this, prompt);
      yesNoBar->SetCadPtr(this);
      ShowCadBarModal(yesNoBar);
      ret = yesNoBar->GetResult() ? 1 : 0;
      if (yesNoBar)
      {
      delete yesNoBar;
      yesNoBar = NULL;
      }

      Unfortunately I can't track down which developer put in this piece of code as we changed source control and lost that history otherwise he would have got a piece of my mind, as would the one who was supposed to do the code review. Cheers, Brett

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #4

      Too bad you can't find the guy who did that. Who knows what other gems he has laying around.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      1 Reply Last reply
      0
      • N Nemanja Trifunovic

        Somewhat off topic: Never understood the people who allocate objects on heap when it is easier and safer to do it on stack. I.e.:

        CYesNoDlgBar yesNoBar(this, prompt);
        yesNoBar.SetCadPtr(this);
        ShowCadBarModal(&yesNoBar);
        ret = yesNoBar.GetResult() ? 1 : 0;

        Programming Blog utf8-cpp

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #5

        Haven't done any C++ for years but I agree. There's a general guideline I came across somewhere: "always use an object or a reference unless you have to use a pointer." I saw lots of unnecessary heap allocation over the years. Why make trouble for yourself when you don't need to? Perhaps it's a sign of misplaced C++ "machismo?" :)

        Kevin

        N 1 Reply Last reply
        0
        • K Kevin McFarlane

          Haven't done any C++ for years but I agree. There's a general guideline I came across somewhere: "always use an object or a reference unless you have to use a pointer." I saw lots of unnecessary heap allocation over the years. Why make trouble for yourself when you don't need to? Perhaps it's a sign of misplaced C++ "machismo?" :)

          Kevin

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #6

          Kevin McFarlane wrote:

          Perhaps it's a sign of misplaced C++ "machismo?"

          :) On the contrary, I think it comes from the SmallTalk/Java style of OOP; create anonymous object and pass pointers/references to them around. Nothing wrong with that approach if you have a GC to clean up after you, but in C++ it is much better to create objects on stack than to worry about calling delete.

          Programming Blog utf8-cpp

          1 Reply Last reply
          0
          • N Nemanja Trifunovic

            Somewhat off topic: Never understood the people who allocate objects on heap when it is easier and safer to do it on stack. I.e.:

            CYesNoDlgBar yesNoBar(this, prompt);
            yesNoBar.SetCadPtr(this);
            ShowCadBarModal(&yesNoBar);
            ret = yesNoBar.GetResult() ? 1 : 0;

            Programming Blog utf8-cpp

            Y Offline
            Y Offline
            Yusuf
            wrote on last edited by
            #7

            Nemanja Trifunovic wrote:

            Never understood the people who allocate objects on heap when it is easier and safer to do it on stack.

            because C allows you to shoot yourself in the foot, but C++ blows it off... (guess who said it)

            Yusuf

            J 1 Reply Last reply
            0
            • L Lost User

              Here is an example from our current development effort:

              CYesNoDlgBar *yesNoBar = new CYesNoDlgBar(this, prompt);
              yesNoBar->SetCadPtr(this);
              ShowCadBarModal(yesNoBar);
              ret = yesNoBar->GetResult() ? 1 : 0;
              if (yesNoBar)
              {
              delete yesNoBar;
              yesNoBar = NULL;
              }

              Unfortunately I can't track down which developer put in this piece of code as we changed source control and lost that history otherwise he would have got a piece of my mind, as would the one who was supposed to do the code review. Cheers, Brett

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #8

              Outstanding is checking the pointer after using it. At most knowing that delete "survives" a Null-pointer. :-O I think you find more of these eye-candy if you go for it. :~ X|

              Greetings from Germany

              P 1 Reply Last reply
              0
              • K KarstenK

                Outstanding is checking the pointer after using it. At most knowing that delete "survives" a Null-pointer. :-O I think you find more of these eye-candy if you go for it. :~ X|

                Greetings from Germany

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #9

                KarstenK wrote:

                I think you find more of these eye-candy if you go for it.

                I agree and that was the point of my post earlier :) I would suspect the developer probably did such a thing as a coding standard of their own and the chances of more gems like that are probably bound to be found.

                "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                1 Reply Last reply
                0
                • N Nemanja Trifunovic

                  Somewhat off topic: Never understood the people who allocate objects on heap when it is easier and safer to do it on stack. I.e.:

                  CYesNoDlgBar yesNoBar(this, prompt);
                  yesNoBar.SetCadPtr(this);
                  ShowCadBarModal(&yesNoBar);
                  ret = yesNoBar.GetResult() ? 1 : 0;

                  Programming Blog utf8-cpp

                  G Offline
                  G Offline
                  geoffs
                  wrote on last edited by
                  #10

                  Right on. I've seen too many examples of exactly the type of coder you're talking about...

                  1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    Somewhat off topic: Never understood the people who allocate objects on heap when it is easier and safer to do it on stack. I.e.:

                    CYesNoDlgBar yesNoBar(this, prompt);
                    yesNoBar.SetCadPtr(this);
                    ShowCadBarModal(&yesNoBar);
                    ret = yesNoBar.GetResult() ? 1 : 0;

                    Programming Blog utf8-cpp

                    J Offline
                    J Offline
                    Johann Gerell
                    wrote on last edited by
                    #11

                    Nemanja Trifunovic wrote:

                    Somewhat off topic: Never understood the people who allocate objects on heap when it is easier and safer to do it on stack.

                    In the general case, you are absolutely spot on. Also, never use pointer arguments to functions unless 0/NULL is a valid value for input. But, but, but... consider the case when an instance of CYesNoDlgBar eats too much stack by itself, and can therefore only be created on the heap? [1] Not that I actually think that was the case there... [1] I've actually just some moments ago fixed the GIF parts of CxImage to be usable on a handheld device platform, where the default stack is just roughly a handful of 10 KB, because several member arrays of the CxImageGIF were harcoded to sizes accumulating to way above the available stack, causing runtime failures. Using std::vector<> solved it.

                    -- Time you enjoy wasting is not wasted time - Bertrand Russel

                    1 Reply Last reply
                    0
                    • Y Yusuf

                      Nemanja Trifunovic wrote:

                      Never understood the people who allocate objects on heap when it is easier and safer to do it on stack.

                      because C allows you to shoot yourself in the foot, but C++ blows it off... (guess who said it)

                      Yusuf

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #12

                      It's hardly the stack that'll contribute to your foot's annihilation in C++...

                      -- Kein Mitleid Für Die Mehrheit

                      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