Hello,
An object passes a pointer as a parameter to a function call.
Is there a was to find if the memory was freed by the calling function and the pointer is not valid?
BR,
Cristian
Hello,
An object passes a pointer as a parameter to a function call.
Is there a was to find if the memory was freed by the calling function and the pointer is not valid?
BR,
Cristian
you can test if the pointer is valid by seeing if it is NULL or not. If it is, this does not however mean that the memory was released - the pointer may just have been assigned to a different address.
No, that's something you have to keep track of yourself.Originally posted by Cristian Negres
Hello,
An object passes a pointer as a parameter to a function call.
Is there a was to find if the memory was freed by the calling function and the pointer is not valid?
BR,
Cristian
This is the reason NULL pointers exists. NULL pointer => Whatever it pointed at has been freed/doesn't exist any more.Originally posted by Cristian Negres
Hello,
An object passes a pointer as a parameter to a function call.
Is there a was to find if the memory was freed by the calling function and the pointer is not valid?
BR,
Cristian
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Hi,
Thank you for your help.
BR,
Cristian
Really? I thought that delete only deallocated memory...not change the pointer.Originally posted by Magos
This is the reason NULL pointers exists. NULL pointer => Whatever it pointed at has been freed/doesn't exist any more.
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
I never said that delete sets the pointer to NULL. That is up to you. You should always set the pointer to NULL after deletion to make sure the adress cannot be accessed or a call to delete again doesn't screw something up.Originally posted by pianorain
Really? I thought that delete only deallocated memory...not change the pointer.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
The NULL pointer always (to my knowledge... there may be exceptions) points to address 0 in memory. Most operating systems never assign any object there. It's strictly off-limits. A pointer set to this place will never contact a real object.
Also, any attempt to access this space in memory automatically results in a Seg Fault. If anything attempts to dereference the pointer, the program will tell them immediately, and the program will crash.
If the pointer wasn't reallocated, the memory the pointer's accessing could be garbage, and the program would still run. This makes finding the error much harder.
Unfortunately, the instance of the pointer that is deallocated with delete is not necessarily the one that will cause a seg fault. Most likely, it will be another copy of the pointer that is unaware that the memory is freed and has no way of knowing. The common solution to this is reference counted "smart" pointers.
The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.
A good structure in your program would never put you in this position.Originally posted by IfYouSaySo
The common solution to this is reference counted "smart" pointers.
Last edited by Magos; 02-22-2003 at 04:59 PM.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
>A good structure in your program would bever put
>you in this position.
If you say so.