Pointer
-
Hey ppl, I have two pointers and one value:
int* p1 = new int;
*p1 = 10;int *p2;
p2 = p1;Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:
delete p1;
p1 = NULL;Now p1 is NULL and referenced the only integer is deleted. Now if I try
delete p2;
p2 = NULL;Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?
-
Hey ppl, I have two pointers and one value:
int* p1 = new int;
*p1 = 10;int *p2;
p2 = p1;Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:
delete p1;
p1 = NULL;Now p1 is NULL and referenced the only integer is deleted. Now if I try
delete p2;
p2 = NULL;Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?
dehseth wrote:
p2 = p1;
But, why one would like to do that ? Heard of need for copy c'tor and assingment operators ? Or dangling pointers ?
-
Hey ppl, I have two pointers and one value:
int* p1 = new int;
*p1 = 10;int *p2;
p2 = p1;Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:
delete p1;
p1 = NULL;Now p1 is NULL and referenced the only integer is deleted. Now if I try
delete p2;
p2 = NULL;Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?
dehseth wrote:
I read about shared pointers with templated which are not good for me to use right now?
In case you change your mind: shared_ptr[^] from Boost[^].
Steve
-
Hey ppl, I have two pointers and one value:
int* p1 = new int;
*p1 = 10;int *p2;
p2 = p1;Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:
delete p1;
p1 = NULL;Now p1 is NULL and referenced the only integer is deleted. Now if I try
delete p2;
p2 = NULL;Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?
You could always try designing your software so you've got a clear, single owner of resources such as allocated heap memory. Alternatively,
- Use .NET - garbage collection means you don't need to worry about object ownership quite as much
- Use Objective C 2.0 - it allows garbage collection, and garbage collection means you don't need to worry about object ownership quite as much
- Use the Boehm allocator[^] - it's garbage collected, and garbage collection means you don't need to worry about object ownership quite as much
- As other posters have suggested, use Boost's shared_ptr class.
If you want to stick with Windows and C++, I'd go for designing your software to manage resources or Boost shared_ptrs.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
dehseth wrote:
p2 = p1;
But, why one would like to do that ? Heard of need for copy c'tor and assingment operators ? Or dangling pointers ?
-
dehseth wrote:
I read about shared pointers with templated which are not good for me to use right now?
In case you change your mind: shared_ptr[^] from Boost[^].
Steve
-
You could always try designing your software so you've got a clear, single owner of resources such as allocated heap memory. Alternatively,
- Use .NET - garbage collection means you don't need to worry about object ownership quite as much
- Use Objective C 2.0 - it allows garbage collection, and garbage collection means you don't need to worry about object ownership quite as much
- Use the Boehm allocator[^] - it's garbage collected, and garbage collection means you don't need to worry about object ownership quite as much
- As other posters have suggested, use Boost's shared_ptr class.
If you want to stick with Windows and C++, I'd go for designing your software to manage resources or Boost shared_ptrs.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Not as much as pointer errors will :-)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p