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. what is wrong with this code?

what is wrong with this code?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 8 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
    aesthetic crazy
    wrote on last edited by
    #1

    i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

    P C T L A 6 Replies Last reply
    0
    • A aesthetic crazy

      i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      int *pi; instead of int* pi; { pi = new int; } no parentheses, no return. :-D Short meaningless names? Unaligned braces? Not enough whitespace? No pre tags? I dunno.

      1 Reply Last reply
      0
      • A aesthetic crazy

        i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

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

        You never delete the memory allocated with new. :)

        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
        [My articles]

        1 Reply Last reply
        0
        • A aesthetic crazy

          i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

          T Offline
          T Offline
          T2102
          wrote on last edited by
          #4
          1. Add a destructor ~A() { delete pi; } 2) Either A) Initialize the pointer to 0 first A() :pi(0) { pi=new int; } B) Initialize the pointer (preferred in this case since it is highly unlikely the memory allocation will fail) A() :pi(new int) {}
          1 Reply Last reply
          0
          • A aesthetic crazy

            i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            My original comment was misplaced. I suspect that the real problem may be that the variable pi is private and thus can never be accessed outside the class. So creating an object of class A serves no real purpose as you cannot set or get the value of pi.

            I must get a clever new signature for 2011.

            modified on Sunday, January 23, 2011 8:32 AM

            1 Reply Last reply
            0
            • A aesthetic crazy

              i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              Aside from the fact that the code doesn't do anything useful, the really serious problems with this class are: - It will leak in normal operation (I use the term "operation" loosely here as the class doesn't do anything) - It isn't exception safe or neutral To fix these you're going to need a destructor, a copy constructor (or a private declaration of a copy constructor) and a swapping assignment operator (or a private declaration of an assignment operator). In addition (ignoring the questionable sanity of storing an int like that) the implementation could be tightened up to use an initialiser list. The idea here is to make a class that behaves like a object of a type built into the language. The structure your class needs to achieve this is known in some circles as "The Orthodox Canonical Form" and the first place I saw it mentioned and explained properly was in "Advanced C++: Programming Styles and Idioms" By Jim Coplien back in the early 90s. Other authors have picked up the term since then so a search for it might give you some more background. Cheers, Ash

              A P 2 Replies Last reply
              0
              • A Aescleal

                Aside from the fact that the code doesn't do anything useful, the really serious problems with this class are: - It will leak in normal operation (I use the term "operation" loosely here as the class doesn't do anything) - It isn't exception safe or neutral To fix these you're going to need a destructor, a copy constructor (or a private declaration of a copy constructor) and a swapping assignment operator (or a private declaration of an assignment operator). In addition (ignoring the questionable sanity of storing an int like that) the implementation could be tightened up to use an initialiser list. The idea here is to make a class that behaves like a object of a type built into the language. The structure your class needs to achieve this is known in some circles as "The Orthodox Canonical Form" and the first place I saw it mentioned and explained properly was in "Advanced C++: Programming Styles and Idioms" By Jim Coplien back in the early 90s. Other authors have picked up the term since then so a search for it might give you some more background. Cheers, Ash

                A Offline
                A Offline
                Adam Roderick J
                wrote on last edited by
                #7

                I support this answer, this is more reasonable.

                Величие не Бога может быть недооценена.

                1 Reply Last reply
                0
                • A Aescleal

                  Aside from the fact that the code doesn't do anything useful, the really serious problems with this class are: - It will leak in normal operation (I use the term "operation" loosely here as the class doesn't do anything) - It isn't exception safe or neutral To fix these you're going to need a destructor, a copy constructor (or a private declaration of a copy constructor) and a swapping assignment operator (or a private declaration of an assignment operator). In addition (ignoring the questionable sanity of storing an int like that) the implementation could be tightened up to use an initialiser list. The idea here is to make a class that behaves like a object of a type built into the language. The structure your class needs to achieve this is known in some circles as "The Orthodox Canonical Form" and the first place I saw it mentioned and explained properly was in "Advanced C++: Programming Styles and Idioms" By Jim Coplien back in the early 90s. Other authors have picked up the term since then so a search for it might give you some more background. Cheers, Ash

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Oh, sure, if you want to get all technical. :rolleyes:

                  1 Reply Last reply
                  0
                  • A aesthetic crazy

                    i have a code from one of my past papers n it was asked to put the reason that what was wrong with that code. i looked on it very much but didnt find any problem. following is the code, kindly tell me even if there is any efficiency or any other drawback in this code. class A { int *pi; public: A() { pi = new int; } };

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #9

                    Building on some of the already posed answers: As mentioned before, you need a destructor to make sure pi is released. Moreover, if you provide no Copy constructor or assignment operator, the compiler will create these for you. Both will simply copy pi over without checking, und thus also produce a memory leak. This means you need to define both a copy constructor and an assignment operator yourself, to prevent memory leaks from inadvertent copying! Another, minor, issue is that you didn't think to initialize the integer pi is pointing to. I will ignore the apparent uselessness of the class in it's current state, assuming that it's going to be expanded later. Going with the above, I would rewrite this to sth like that:

                    class A {
                    int* pi;
                    public:
                    int* getpi() {
                    if (0 == pi)
                    pi = new int();
                    if (pi != 0)
                    *pi = 0;
                    return pi;
                    }
                    A() : pi(0) {}
                    A(const A& other) : pi(0) {
                    *getpi() = *other.pi; /* deep copy */
                    }
                    ~A() { delete pi; }
                    A& operator=(const A& other) {
                    *getpi() = *other.pi; /* deep copy */
                    return *this;
                    }
                    }

                    Note that you could also implement a shallow copy by just redirecting pi to wherever the other object's pi is pointing, but if you do that, you need some mechanism such as reference counting to prevent deleting an int object while oter instances are still pointing to it! Note also that I only programmed the happy case with respect to allocation: if getpi() ever fails to allocate pi (and thus returns 0), then the call to the copy constructor or assignment operator will throw a NULL pointer exception. You might want to catch that, especially in the constructor. [edit]Made getpi() public to resolve the inaccessibility issue, and added initializion of *pi[/edit]

                    modified on Monday, January 31, 2011 6:02 AM

                    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