[fpc-pascal] Pointer to an array of PChar strings

Marco van de Voort marcov at stack.nl
Tue Jan 19 15:39:47 CET 2010


In our previous episode, Graeme Geldenhuys said:
> 
> I'm working with a C library that returns an array of strings. So that is
> type PPChar.  I C library does the array allocation, but doesn't do the
> freeing of the array.
> 
> How am I supposed to free an array of PChar strings? I think I need to
> improve my iteration too, because I'm moving the pointer of wlst, so I
> probably need to make a backup of the original pointer before the iteration.


The array is 0 terminated, iow the last array of pchar is nil.

var p2 : ppchar;

If assigned(p) then
  begin
    p2:=p;
    while assigned(p2) do 
      begin
        cfree(p2);
        inc(p2);
      end;
    cfree(p);
  end; 

where cfree is the free() corresponding to the malloc with which it was
allocated in the lib. (usually libc's)
 



More information about the fpc-pascal mailing list