Delete and Null Pointer - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Delete and Null Pointer

PooBearPooBear Posts: 19Member
I want to check with you guys .
here is an example.
I have a class like this

class Foo
{
int data;
};

int main()
{
Foo *objPtr = new Foo[5];
for ( int i =0; i<5 ;i++ )
{
objPtr[i]->data = i;
if ( i == 3 )
{
objPtr[i] = NULL; //Can I do somthing like this
}
}
delete[]objPtr;
}

in this program can I do objPtr[i] = NULL and then call the delete. does this make sense? Please explain to me.
I need to make the pointer null in my original program it it meets some condition. and finally once I an done with all I will call delete[]. Please someone explain to me.

Thanks
Machado

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    delete works correctly the NULL pointers (it just does nothing when the pointer is NULL).
  • LundinLundin Posts: 3,711Member
    : I want to check with you guys .
    : here is an example.
    : I have a class like this
    :
    : class Foo
    : {
    : int data;
    : };
    :
    : int main()
    : {
    : Foo *objPtr = new Foo[5];
    : for ( int i =0; i<5 ;i++ )
    : {
    : objPtr[i]->data = i;
    : if ( i == 3 )
    : {
    : objPtr[i] = NULL; //Can I do somthing like this
    : }
    : }
    : delete[]objPtr;
    : }
    :
    : in this program can I do objPtr[i] = NULL and then call the delete. does this make sense? Please explain to me.
    : I need to make the pointer null in my original program it it meets some condition. and finally once I an done with all I will call delete[]. Please someone explain to me.
    :
    : Thanks
    : Machado
    :
    :


    objPtr[i] = NULL;

    If objPtr is pointing at dynamicly allocated memory, you will create a memory leak by doining like that. You need to delete[] the memory first, and then set the pointer to NULL.
  • PooBearPooBear Posts: 19Member
    Thanks guys.
    So can I do like this

    delete objPtr;
    objPtr = NULL;

    Does this make sense ?


    : : I want to check with you guys .
    : : here is an example.
    : : I have a class like this
    : :
    : : class Foo
    : : {
    : : int data;
    : : };
    : :
    : : int main()
    : : {
    : : Foo *objPtr = new Foo[5];
    : : for ( int i =0; i<5 ;i++ )
    : : {
    : : objPtr[i]->data = i;
    : : if ( i == 3 )
    : : {
    : : objPtr[i] = NULL; //Can I do somthing like this
    : : }
    : : }
    : : delete[]objPtr;
    : : }
    : :
    : : in this program can I do objPtr[i] = NULL and then call the delete. does this make sense? Please explain to me.
    : : I need to make the pointer null in my original program it it meets some condition. and finally once I an done with all I will call delete[]. Please someone explain to me.
    : :
    : : Thanks
    : : Machado
    : :
    : :
    :
    :
    : objPtr[i] = NULL;
    :
    : If objPtr is pointing at dynamicly allocated memory, you will create a memory leak by doining like that. You need to delete[] the memory first, and then set the pointer to NULL.
    :

  • TheNullinatorTheNullinator Posts: 31Member
    Hi,

    Yeah, you can do:

    [code]
    delete objPtr;
    objPtr = NULL;
    [/code]

    Another thing, it is often a good idea to use a [italic]std::auto_ptr[/italic] to store pointers. The data pointed to gets deleted when the [italic]auto_ptr[/italic] goes out of scope so it is exception safe. A lot of memory leaks can be fixed by using these. They can't be used in every situation, though. It might be worth you while to look them up in a STL reference.

    Cheers
Sign In or Register to comment.