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. operator new/delete for beginners

operator new/delete for beginners

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 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.
  • T Offline
    T Offline
    Tomerland
    wrote on last edited by
    #1

    Dear gurus, I have the following question. I heard/read somewhere that it does not make sense to check for a zero-pointer if operator new is used, as

    int *p = 0;
    p = new int [5];
    if ( 0==p ) { ..some action here.. }

    It also is allowed to use operator delete even on zero-pointers. ...and more such issues..... Can somebody give me a short link to a good document on such topics? Thank you very much

    C D 2 Replies Last reply
    0
    • T Tomerland

      Dear gurus, I have the following question. I heard/read somewhere that it does not make sense to check for a zero-pointer if operator new is used, as

      int *p = 0;
      p = new int [5];
      if ( 0==p ) { ..some action here.. }

      It also is allowed to use operator delete even on zero-pointers. ...and more such issues..... Can somebody give me a short link to a good document on such topics? Thank you very much

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      See here [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      T 1 Reply Last reply
      0
      • C CPallini

        See here [^]. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        T Offline
        T Offline
        Tomerland
        wrote on last edited by
        #3

        Another question: If I want to use the no-throw-Version: T *pt = new (std :: nothrow) T(); What happens if allocation fails? Can I test 'pt' for 0 then? Or does pt have any undefined value?

        S C A 3 Replies Last reply
        0
        • T Tomerland

          Another question: If I want to use the no-throw-Version: T *pt = new (std :: nothrow) T(); What happens if allocation fails? Can I test 'pt' for 0 then? Or does pt have any undefined value?

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #4

          Are you prearing for any interview?? :D :laugh:

          1 Reply Last reply
          0
          • T Tomerland

            Another question: If I want to use the no-throw-Version: T *pt = new (std :: nothrow) T(); What happens if allocation fails? Can I test 'pt' for 0 then? Or does pt have any undefined value?

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Well, I think you may test in both cases. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            1 Reply Last reply
            0
            • T Tomerland

              Another question: If I want to use the no-throw-Version: T *pt = new (std :: nothrow) T(); What happens if allocation fails? Can I test 'pt' for 0 then? Or does pt have any undefined value?

              A Offline
              A Offline
              Arman S
              wrote on last edited by
              #6

              The C++ Standard guarrantees that new(nothrow) returns NULL if fails.

              -- ===== Arman

              1 Reply Last reply
              0
              • T Tomerland

                Dear gurus, I have the following question. I heard/read somewhere that it does not make sense to check for a zero-pointer if operator new is used, as

                int *p = 0;
                p = new int [5];
                if ( 0==p ) { ..some action here.. }

                It also is allowed to use operator delete even on zero-pointers. ...and more such issues..... Can somebody give me a short link to a good document on such topics? Thank you very much

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Tomerland wrote:

                I heard/read somewhere that it does not make sense to check for a zero-pointer if operator new is used...

                What if new fails?

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                T 1 Reply Last reply
                0
                • D David Crow

                  Tomerland wrote:

                  I heard/read somewhere that it does not make sense to check for a zero-pointer if operator new is used...

                  What if new fails?

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  T Offline
                  T Offline
                  Tomerland
                  wrote on last edited by
                  #8

                  I catch the exception

                  D 1 Reply Last reply
                  0
                  • T Tomerland

                    I catch the exception

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Which is not thrown on all compilers.

                    "Love people and use things, not love things and use people." - Unknown

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    T 1 Reply Last reply
                    0
                    • D David Crow

                      Which is not thrown on all compilers.

                      "Love people and use things, not love things and use people." - Unknown

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      T Offline
                      T Offline
                      Tomerland
                      wrote on last edited by
                      #10

                      Really? But if there is no exception thrown and there is no pointer set to 0. What else should be testet?

                      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