As long as pointer, x1, and x2 are defined, that will compile; and as long as both x1 and x2 have reasonable values, the program will work. There will be a memory leak, yes, but the program won't crash.
The usual, if not only, method to know whether a pointer points to something valid is by assigning it to zero when it doesn't. If the pointer has any other value, it's assumed that it can be safely dereferenced. However, that's about all the information you can get from a pointer's value. It's impossible to know whether a pointer points to something that can or should be freed, or how it should be freed.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void f(int *&p){
if (p)
delete p;
pointer=newint(0);
}
//...
int *p=0;
f(p); //OK
p=newint;
f(p); //OK
p=newint[10];
f(p); //Memory leak.
int a;
p=&a;
f(p); //Very bad! Will try to delete a pointer to the stack!
void f(int *&p){
if (p)
delete p;
pointer=newint(0);
}
I see this check for if(p) often and it is useless. If p != NULL and not valid the program will crash!
And if p == NULL then nothing will happen since delete NULL; is valid.
The problem with that is that you delete pointer before it has been given a meaningful value. It will not automatically be set to zero when you create it. So it will contain some completely unknown, random value.
Calling delete on that value could cause a crash and/or corrupt other program data.
You should only delete a pointer that is explicitely set to zero or that has been given a value allocated by new:
Notice also then when you delete a pointer you need to specify if it was pointing to an array or just a single charatcer.
The rule is that if you call new, then you call delete, but if you call new[] then you must call delete[].
1 2 3 4 5
char* p = newchar; // allocate one char
delete p; // Good, deleted the single char
p = newchar[256]; // allocate an array of chars
delete[] p; // Good, deleted the whole array
I see this check for if(p) often and it is useless. If p != NULL and not valid the program will crash!
And if p == NULL then nothing will happen since delete NULL; is valid.
Take a look at this. I implemented a smart pointer that is aware of the 'kind' of memory it holds (i.e. static or dynamic) and deallocates it by itself when necessary. Of course more work needs to be done here, I plan on also making it aware of the number of smart pointer instances that hold the same dynamic address and only delete it when the last of them expires. I'll have to also overload the = operator ofc... This is just a simple demonstration of the memory 'kind' awareness and auto-deletion.
I see this check for if(p) often and it is useless. If p != NULL and not valid the program will crash!
And if p == NULL then nothing will happen since delete NULL; is valid.
The explicit check served the purpose of illustrating my point better than just deleting the pointer. Otherwise, I would have depended on OP knowing that behavior.
m4ster r0shi: Well, that's the most useless thing ever. The point of smart pointers is automatic resource management. Why would anyone want a smart pointer that points to things that don't need to be managed?
Now you're entering the point when you shouldn't be using be pointers anymore, and should be using vectors instead.
No, I don't think so.
If I want static memory allocation and bounds checking I'll use a smart pointer. If I want dynamic memory allocation and bounds checking I'll use a smart pointer again. I'll only use a vector if I want dynamic memory allocation, bounds checking and resizability.
Look. A smart pointer by definition is something that can be used as a pointer but also provides some additional features. One of them can be automatic memory management, another can be bounds checking and you can add here anything you think might be useful...
Sorry, but that is useless not from the conceptual point of view but from the practical. You are reinventing the wheel (if understood it properly) why not just use boost/C++0x shared_pointer and weak_pointer?
I am aware of both auto_ptr and boost smart pointers. I did this for educational purposes.
EDIT: Also, I'm not sure that these smart pointers offer the bounds checking functionality for static arrays (correct me if I'm wrong) so, in that case, I'll have to reject your argument too.
They don't implement bounds checking for reasons of performance. The idea is to make auto_ptr and the various
smart pointers equal in performance to a raw pointer. This is doable for the access case (but bounds checking
is impossible) but not for the create/delete case since reference counts need to be bumped.