Results 1 to 11 of 11

Thread: Question about deleting pointers

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Question about deleting pointers

    Say you have this code:
    Code:
    float *Pi = new float;
    *Pi = 3.141592;
    delete Pi;
    Why wouldn't you just use this:
    Code:
    float Pi = 3.141592;
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Zaei
    Guest
    You could. Using a pointer in that situation is just dumb. A much better example would be:
    Code:
    float* floats = new float[1000];
    Now you have, basically, an array of floats. Then you can delete it as above. Another example is:
    Code:
    myStruct* strct = new myStruct;
    SomeFunc(strct);
    
    ...
    where sizeof(myStruct) > 4
    in this case, you are passing a pointer(DWORD), instead of the entire struct, which increases speed. You can also create pointers to functions, which help a lot when doing some things (callback functions). Its all in knowing when to use a pointer. In your example, there is just no reason. But other times, is speeds execution time up, by not haveing to copy data from location to location.

    Z.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    One thing you missed:
    Code:
    float *i = new float;
    delete i;
    
    float *j = new float[100];
    delete[] j;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    more reasons: the C++ STL library

    Code:
    #include <vektor>
    using namespace std;
    
    class CParentClass
    { // members
    }
    
    class CFirstChildClass : public CParentClass
    {  // Blabla  
    }
    
    class CSecondChildClass : public CParentClass
    {  // Whatever
    }
    
    void SomeFunc()
    {
         CParentClass *pFC = new CFirstChildClass;
         // ask for user input and fill class members
         // insert the pointer into a vektor <CParentClass*> object that is always available
    }
    
    void SomeOtherFuncThatIsCalledAtAnEntirelyOtherTime()
    {
          // you can now access the object
    }
    Advantages:
    1) Polymorphic access: It doesn't matter which child class the contents of the vektor are.
    2) You still have exactly the object you created, not a copy as if you were using vektor<CParentClass>
    3) If you used CFirstChildClass obj; and inserted &obj into the vektor, obj would be out of scope in SomeOtherFunc... and most probably deleted.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    vector not vektor

    You don't need the STL to use virtual inheritance, which is what goes on here:
    Code:
    class Parent {
    public:
        virtual void Func() = 0;
    };
    
    class ChildA : public Parent {
    public:
        void Func() { cout << "Die!" << endl; }
    };
    
    class ChildB : public Parent {
    public:
        void Func() { cout << "Live!" << endl; }
    };
    
    void somecode() {
        Parent *ptr;
        ChildA *thing = new ChildA;
        ChildB *other = new ChildB;
    
        ptr = thing;
        ptr->Func();
    
        ptr = other;
        ptr->Func();
    
        delete thing;
        delete other;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    true, but this way I can at the same time show the "no copy" and the polymorphic advantages.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Okay then I just wouldn't mix templates in if they're still on the "*** do pointers do" stage

    Not quibbling, though
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, maybe write an essay about the difference between heap and stack?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Aaaargghhh yeah!

    Damn that really messed me up when I was learning. Hmm...perhaps another FAQ entry?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's just that I don't have time for it at the moment.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Originally posted by parksie
    Okay then I just wouldn't mix templates in if they're still on the "*** do pointers do" stage
    Heh, that's pretty much me. I know the basics of what they do, but I'm still a little fuzzy on it.
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width