Dinamic memory new and delete

Hi guys, I don't speak english, but I going to try it,

problem:

1
2
3
4
5
6
7
8
9
10
11
	int *p = new int[100];

	delete[] p;

	//p = NULL;

	if(!p)
	{
		cerr<<"ERROR";

	}


when "delete" erase p pointer, it is assumed that p is null, but when the "if" is running, the condition becomes false, if p is null, the condition must be fulfilled(you know true!!)

another problem:
1
2
3
4
5
6
7
8
9
10
11
	int *p = new int[100];

	delete[] p;

	p = NULL;//---> now enabled

	if(!p)
	{
		cerr<<"ERROR";

	}

oohhh yeah is workeing, BUTTT WHYYYYYY!!??

and another:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	int *p = new int[100];

	delete[] p;

	p = NULL;//----> Now enable

	if(!p)
	{
		cerr<<"ERROR";

	}

        delete[] p; //this should not be happened, but it is happening and it is not a error

Please tell me what Can I do...

the real code must be:
1
2
3
4
5
6
7
8
9
10
	int *p = new int[100];

	delete[] p;

	if(!p)
	{
	   cerr<<"Im here yeah, problem solved";
           exit(1);
	}
        cout<<"hey what the hell LOL"<<endl;
Last edited on
[code] "Please use code tags" [/code]
When you delete a pointer it is not automagically set to NULL.
Deleting a NULL pointer does nothing.
I'm using visual, when you gonna delete a pointer that was deleted, the program results in errors
Yes, a double delete is an error. So don't do it.
1
2
3
4
5
int *p = new int[10];

	delete[] p;

        delete[] p;


Errorrr

1
2
3
4
5
6
7
8
9
10
11
12
int *p = new int[10];

	/*for(int i=0;i<10;i++)
	{
		p[i] = i;
	}*/

	delete[] p;

	p = NULL;

	delete[] p;


No error
Don't delete pointers that were already deleted. If you're doing this, you have a logic error in your code.
Cubbi you don;t even read my post,

1
2
3
4
5
6
7
8
9
10
11
12
int *p = new int[100];

delete[] p;

if(!p)
{
  cerr<<"If you are here, you are right";
  exit(1);

}

cout<<"if you are here, you are wrong";


This code, never enter in the right zone (if (!p){...} ), why!!?? my books are wrong??
Once you've executed delete[] p;, there is nothing you can do with p, besides assigning a new value to it. "if(!p)" is wrong.
then, how can I know if a variable is null or not null, I mean, if a variable (p in this case), is deleted or not deleted, How Can I know that, the only method is comparing with null or there is another method??
This is only a example of the real code, I'm implementing a destroyer of a class, but whe the program is ending, the errors come back
how can I know if a variable is null or not null, I mean, if a variable (p in this case), is deleted or not deleted,

Well, being null or not and being deleted or not are entirely different things.

There is no way to tell if a given pointer has been deleted or not, unless you track what you do to the pointer.
how can I know if [...] a variable (p in this case), is deleted or not deleted,

You wrote the program, didn't you? You can know that the same way you know if an integer variable is initialized or not initialized. In modern C++, delete isn't used much anyway, since we have containers and smart pointers.
Last edited on
Topic archived. No new replies allowed.