On 17:44 12/06/04, Frank Heckenbach wrote:
1) Is this correct behavior according to the standards? I know that doing two New()'s on the same pointer is a recipe for disaster,
No, it isn't. Don't know where you got this from.
`Turbo Pascal 6: The Complete Reference', by Stephen O'Brien, published by Borland and Osborne. Said two New()'s on the same pointer will allocate two chunks of memory of sizeof(variable) on the heap, and two corresponding Dispose()'s will not get rid of the two allocations.
but it is unclear to me whether the same applies to this scenario.
No, this scenario *is* a problem. ;-)
According to a standard it's an error that a processor is permitted to leave undetected. It's in the same category as using undefined values etc.
Ahh. Then I guess I should just be careful.
Some people suggest heuristics, such as setting undefined variables to certain "unlikely" values and runtime-testing for these, but they can only catch some of the problems, and are not completely reliable.
No, this wouldn't help. It would just lull people into a false sense of security; better just tell people to be wary of their code (which they should always be, in any case).
You can try linking the "Electric fence" library to catch some kinds of memory management errors. The same caveat as in the previous paragraph apply, but at least it's no big effort to do.
Thanks for the tip; will try it as soon as appropriate.
I suppose that's because in most compilers (including GPC, BP, ...) it's just undefined behaviour.
That would certainly account for the discrepancy. :)