Click here to Skip to main content
15,941,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what do you think about this code ?
C++
#include <iostream>

using namespace std;

struct exam{
    int id;
    char name[10];
};

void deleteP(void* ptr)
{
    delete []ptr;
    ptr = NULL;
}

int main()
{
    int *p1 = new int [10];
    char *p2 = new char[10];
    double *p3=new double[10];
    exam *p4 =new exam();
    
    //use variable's

    deleteP(p1);
    deleteP(p2);
    deleteP(p3);
    deleteP(p4);


    return 0;
}

is this good to deallocate heap in this way?
Posted
Updated 9-Jun-12 9:31am
v2

C++
void deleteP(void* ptr)
{
    delete []ptr;
    ptr = NULL;
}

You do realise that this will only mark the temporary pointer as NULL,; the original still points to a deallocated block. Also the call to deleteP(p4) is wrong as p4 is not an array.
 
Share this answer
 
Comments
Shmuel Zang 9-Jun-12 16:23pm    
5'ed.
Richard MacCutchan 9-Jun-12 16:33pm    
Thanks but I think Aescleal's answer is much more informative.
Shmuel Zang 9-Jun-12 16:49pm    
Aescleal's answer is much more informative. But, the question is about deallocation of memory using the given function. And, this answer shows the problems of this use. So, in my opinion, this answer is more suitable.
No, it's a rubbish idea, even if it was valid C++ and did what you thought it did. First off in you example there was no need for a dynamically allocated array. Second if you want dynamic arrays then use a vector. If you can't use a vector (for some reason) use a std::shared_ptr or std::unique_ptr.

So using ptr objects:
C++
#include <memory>
 
struct exam{
    int id;
    char name[10];
};

int main()
{
    std::unique_ptr<int>    p1( new int [10]   );
    std::unique_ptr<char>   p2( new char[10]   );
    std::unique_ptr<double> p3( new double[10] );
    std::unique_ptr<exam>   p4( new exam()     );
   
    //use variable's

    // Er, that's it
}
Not a delete in sight, no need to call loads of functions to free memory.

Or preferably using std::vector:
C++
#include <vector>

struct exam{
    int id;
    char name[10];
};
 
int main()
{
    std::vector<int>< p1( 10 );
    std::vector<char> p2( 10 );
    std::vector<double> p3( 10 );
    std::unique_ptr<exam> p4( new exam() );
 
    //use variable's

    // er, that's it again
}
Or even more preferably using automatic objects:
C++
struct exam{
    int id;
    char name[10];
};
 
int main()
{
    int    p1[10];
    char   p2[10];
    double p3[10];
    exam   p4;
    
    //use variable's

    // yet again nothing else to do
}
Basically the first rule is don't fiddle with memory directly. Sometimes you have to but unless one of the other C++ mechanisms will fo the job. The second rule is that if you do have to use the heap then assign the returned value from new to a ptr object so the destructor gets called automagically.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900