Re: Test if memory pointer is valid?




"Richard A. DeVenezia" <rdevenezia@xxxxxxxxxxxx> wrote in message
news:43tfb0F1npa5qU1@xxxxxxxxxxxxxxxxx
> Is there a way or technique for knowing if a pointer points to memory that
> has been freed ?
>
> The example is a little silly, in reality, P & Q might fields in instances
> of disparate classes
>
> P: Pointer;
> Q: Pointer;
> BuffSize: Integer;
>
> BuffSize := 32000;
> GetMem(P, BuffSize);
> F.Read(P^,BuffSize);
> ...
> Q := P;
> ...
> if ...
> FreeMem(P,BuffSize)
> ...
>
> if [ Q points to memory that has not been freed ] // what technique, if
> any, can test this?
> FreeMem (Q,BuffSize);
>
> end;
>
> --
> Richard A. DeVenezia
>
>
I am not totally comfortable with pointers, especially as they are handled
in Delphi as compared to C, but at the basic level, a pointer is just a
memory address that contains the memory address of some data, which could be
anything from a single byte to an array, structure, or object, which may in
turn contain other pointers. If the pointer is nil, or all zeroes, it does
not point to anything. When it is initialized, it should contain the address
of memory which has been allocated for the size of the data type it
represents. Thus, if it is set to nil when it is freed, it can be checked
for validity.

My problem is that I don't know why a second variable should be set to point
to the same block of memory. I can possibly understand setting a pointer to
the original pointer, and perhaps interpreting the memory space in a
different way, using a typecast. This may be similar to the "C" concept of a
Union, in which several variable names all point to the same memory, but are
interpreted by the compiler in different ways. However, I think each
variable is compiled as the same address, so if any one is Freed and made
nil, a reference to any one is also nil.

I can't think of any reason to have two separate pointers to the same
address, but it must be common, as I have been informed previously about
multiple references. I can see setting a new pointer initially to the
address of the beginning of an array, for example, and then incrementing the
pointer to address subsequent elements, but that would usually be done
inside a function, where the new pointer would go out of scope at the end of
the function and be reinitialized if it is called again. I would like to see
a specific example where it is necessary to have multiple references such as
this where it would be necessary to check for it having been freed.

Possibly, I can see where two objects on a form, perhaps, might reference
fields in each other, but unless the object itself had been freed, the
reference should be valid, and you can always check to see if the object is
not nil.

If there is such a reason, would it be possible to assign a pointer to the
original pointer, and then use dereferencing to determine if the original
pointer was nil and therefore free, or otherwise safe to use?

Thanks for any insight, comments, explanations, and examples.

Paul E. Schoen


.



Relevant Pages

  • Re: How java passes object references?
    ... to think of them as being a specific location in a larger block of memory ... Whether a language passes by reference or by value, ... is a pointer pointing at the memory block. ... So when allocating local memory for o, it would simply allocate a ...
    (comp.lang.java.programmer)
  • Re: Lock-free reference counting
    ... Incrementing and decrementing scattered memory locations is the ... main reason reference counting is so slow. ... large blocks of such adjustments per page, ... to contain any long-lived pointer loops, ...
    (comp.programming)
  • Re: in defense of GC (was Re: How come Ada isnt more popular?)
    ... You can still have memory leaks ... pointer that still refers to allocated memory, ... Instead one simply clears the reference to the root of entire complex ...
    (comp.lang.ada)
  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)