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. realted to new operator.

realted to new operator.

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 4 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.
  • A Offline
    A Offline
    anju
    wrote on last edited by
    #1

    Hi all, can any one explain me what is the difference in the following code. 1.I have a class CSample. 2.I want to create an object on heap by using new operator for CSample. I found there are two methods for this. 1.One is like this CSample * pSamep=new CSample; and 2.second one is like this typedef auto_ptr pSample; pSample(new CSample()); what is the difference with these two methods.which one is more efficient. thanks in advance. regards raju anju

    D 1 Reply Last reply
    0
    • A anju

      Hi all, can any one explain me what is the difference in the following code. 1.I have a class CSample. 2.I want to create an object on heap by using new operator for CSample. I found there are two methods for this. 1.One is like this CSample * pSamep=new CSample; and 2.second one is like this typedef auto_ptr pSample; pSample(new CSample()); what is the difference with these two methods.which one is more efficient. thanks in advance. regards raju anju

      D Offline
      D Offline
      Dave Bryant
      wrote on last edited by
      #2

      anju wrote: typedef auto_ptr pSample; I assume you meant: typedef auto_ptr<CSample> pSample; The auto_ptr is a small structure that holds the pointer to the object, and a flag indicating whether or not it owns the object (it does by default). In it's destructor, it deletes the object if it owns it. This can greatly simplify memory management, as these smart pointers take care of deleting themselves when they go out of scope. In terms of efficiency, the auto_ptr would be every so slightly less efficient, but we are probably talking literally one or two CPU instructions, so it is really irrelevant. Dave http://www.cloudsofheaven.org

      C N 2 Replies Last reply
      0
      • D Dave Bryant

        anju wrote: typedef auto_ptr pSample; I assume you meant: typedef auto_ptr<CSample> pSample; The auto_ptr is a small structure that holds the pointer to the object, and a flag indicating whether or not it owns the object (it does by default). In it's destructor, it deletes the object if it owns it. This can greatly simplify memory management, as these smart pointers take care of deleting themselves when they go out of scope. In terms of efficiency, the auto_ptr would be every so slightly less efficient, but we are probably talking literally one or two CPU instructions, so it is really irrelevant. Dave http://www.cloudsofheaven.org

        C Offline
        C Offline
        Chintan
        wrote on last edited by
        #3

        I have tried the follwing : typedef auto_ptr str; but is not working. Gives error "syntax error : missing ';' before '<'". Is any header file needed? Chintan C.R.Naik

        D 1 Reply Last reply
        0
        • C Chintan

          I have tried the follwing : typedef auto_ptr str; but is not working. Gives error "syntax error : missing ';' before '<'". Is any header file needed? Chintan C.R.Naik

          D Offline
          D Offline
          Dave Bryant
          wrote on last edited by
          #4

          I think it is in the <memory> header file, but it is also defined within the std namespace. If you repost your error message, take not that codeproject thinks that everything between angle brackets is an HTML tag, and so does not display it. You should use < and > Dave http://www.cloudsofheaven.org

          C 1 Reply Last reply
          0
          • D Dave Bryant

            I think it is in the <memory> header file, but it is also defined within the std namespace. If you repost your error message, take not that codeproject thinks that everything between angle brackets is an HTML tag, and so does not display it. You should use < and > Dave http://www.cloudsofheaven.org

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

            previous time I have tried : typedef auto_ptr < CString > str; and again I have tried the following one : typedef std::auto_ptr < CString > str; but both are not working and giving error Chintan C.R.Naik

            D 1 Reply Last reply
            0
            • C Chintan

              previous time I have tried : typedef auto_ptr < CString > str; and again I have tried the following one : typedef std::auto_ptr < CString > str; but both are not working and giving error Chintan C.R.Naik

              D Offline
              D Offline
              Dave Bryant
              wrote on last edited by
              #6

              Are you including <memory> rather than <memory.h>? Dave http://www.cloudsofheaven.org

              C 1 Reply Last reply
              0
              • D Dave Bryant

                Are you including <memory> rather than <memory.h>? Dave http://www.cloudsofheaven.org

                C Offline
                C Offline
                Chintan
                wrote on last edited by
                #7

                Both are unable to solve the problem. C.R.Naik

                1 Reply Last reply
                0
                • D Dave Bryant

                  anju wrote: typedef auto_ptr pSample; I assume you meant: typedef auto_ptr<CSample> pSample; The auto_ptr is a small structure that holds the pointer to the object, and a flag indicating whether or not it owns the object (it does by default). In it's destructor, it deletes the object if it owns it. This can greatly simplify memory management, as these smart pointers take care of deleting themselves when they go out of scope. In terms of efficiency, the auto_ptr would be every so slightly less efficient, but we are probably talking literally one or two CPU instructions, so it is really irrelevant. Dave http://www.cloudsofheaven.org

                  N Offline
                  N Offline
                  nde_plume
                  wrote on last edited by
                  #8

                  I thought it might be useful to explain a little more of the reason why auto_ptr was included in the standard. (There was a great deal of haggling about this and other types of pointer wrappers, and there are a number of other pointer wrappers in boost (boost.org) that will almost certainly make it into the next version of the standard.) The reason why auto_ptr is important relates to exceptions. Consider this: class excep { /* some exception class */}; void g() { try { f(); } catch(excep &e) { handle(e); } } void f() { int* a = new int; *a = might_throw_excep(); other_fn(); delete a; }; Now consider what happens if might_throw_excep throws an exception. In that case, the stack unwinds to the nearest matching catch statement (in g), but that means that a, which is now out of scope, points to heap allocated memory that will never be deleted. auto_ptr solves this problem, by rewriting f() as the following: void f() { auto_ptr a = new int; *a = might_throw_excep(); other_fn(); } In this case, the destructor of a (which is an auto_ptr template class) is called when it goes out of scope. This causes the object a to be deleted. However, what is more important is this: even if might_throw_excep throws, then as part of the exception processing system, the destructors of the classes are called as the stack is unwound to the catch statement. This happens even though other_fn is not called. That means that, even if an exception is thrown, a's destructor is called, and it doesn't leak memory. For that reason, auto_ptr (or something like it) is necessary to write leak free code in the presence of exceptions. This is the motivation, AFAIK, behind why auto_ptr was added to the standard. HTH.

                  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