[fpc-pascal] pointer to anything

ik idokan at gmail.com
Fri Apr 23 10:57:34 CEST 2010


Try the following:

type list = ^node;
   node = record
       data : Variant;
       next : list;
   end;


Variant can store a lot pf data types, however please note that it's very
slow type.
You also must remember that Pascal is Strong typed, unlike C or duck type
languages.

The Pointer type store only memory address and not the data itself, so it's
not recommended to use it unless you control the memory allocation itself
for each 'data'.

Ido

http://ik.homelinux.org/


2010/4/23 spir ☣ <denis.spir at gmail.com>

> Hello,
>
> Say I want to implement a kind of linked list which node data may be
> anything. Thus I cannot store data on place (in nodes), indeed; so it should
> be referenced. But pointers themselves are supposed to be typed. So, how can
> I do that?
>
> type list = ^node;
>    node = record
>        data : ???;
>        next : list;
>    end;
>
> I tried to use Pointer, but then I get an error saying the compiler found
> <some type> while it expected "untyped". What is the use of Pointer (without
> type for pointed data)?
> I also had a look at Variant; this looks like an ugly hack ;-) And avyway
> is not really generic. I need data to ba able to be anything, including
> first a linked list itself...
> Finaly, when trying to write the the algo for a given type, I get an issue
> since the def above is recursive, recursively pointing in fact. So, I need
> to stop the run somewhere by allocating the memory for non-yet-existing data
> pointed by a pointer -- I guess.
>    p^ := <I'll tell you soon, just book the place>
> (The compiler warns about not initialised pointers.)
>
> Pointers (sic!) to implementations of similar things (including trees)
> welcome...
> More generally, I cannot find in the library units for common data
> structures (sequence, associative table, set (the collection)...).
>
> Denis
> ________________________________
>
> vit esse estrany ☣
>
> spir.wikidot.com
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20100423/a646a9db/attachment.html>


More information about the fpc-pascal mailing list