Re: [fpc-pascal] How to work with reference-counted strings in dynamically created objects

2015-03-28 Thread Sven Barth
Am 28.03.2015 06:51 schrieb "Howard Page-Clark" :
>
> The following program, compiles and runs as expected using a shortstring
declaration in the TExample record.
> Could someone explain how to adapt it to cope with a TExample record
containing ansistrings?

[snip]
> procedure TExampleList.Initialize(aCount: integer);
> var
>   i: integer;
>   pex: PExample;
> begin
>   for i:=1 to aCount do begin
> GetMem(pex, SizeOf(TExample));
// use New(pex) instead of GetMem(...)
> pex^.Init(Format('Example#%d, Value=%d',[i, Random(100)]));
> FList.Add(pex);
>   end;
> end;
>
[snip]

>
> destructor TExampleList.Destroy;
> var
>   i: integer;
> begin
>   for i:=0 to FList.Count-1 do
> Freemem(FList[i], SizeOf(TExample));
// Use Dispose(PExample(FList[i])) instead of FreeMem(...)
>   FList.Free;
>   inherited Destroy;
> end;
>

It's best to always use New/Dispose if you work with records. Use
GetMem/FreeMem only if you work with unstructured memory areas or where you
don't really know the real size beforehand.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] How to work with reference-counted strings in dynamically created objects

2015-03-28 Thread Howard Page-Clark

On 28/03/2015 08:31, Sven Barth wrote:

Am 28.03.2015 06:51 schrieb "Howard Page-Clark" mailto:h...@talktalk.net>>:
 >
 > The following program, compiles and runs as expected using a
shortstring declaration in the TExample record.
 > Could someone explain how to adapt it to cope with a TExample record
containing ansistrings?

[snip]
 > procedure TExampleList.Initialize(aCount: integer);
 > var
 >   i: integer;
 >   pex: PExample;
 > begin
 >   for i:=1 to aCount do begin
 > GetMem(pex, SizeOf(TExample));
// use New(pex) instead of GetMem(...)
 > pex^.Init(Format('Example#%d, Value=%d',[i, Random(100)]));
 > FList.Add(pex);
 >   end;
 > end;
 >
[snip]

 >
 > destructor TExampleList.Destroy;
 > var
 >   i: integer;
 > begin
 >   for i:=0 to FList.Count-1 do
 > Freemem(FList[i], SizeOf(TExample));
// Use Dispose(PExample(FList[i])) instead of FreeMem(...)
 >   FList.Free;
 >   inherited Destroy;
 > end;
 >

It's best to always use New/Dispose if you work with records. Use
GetMem/FreeMem only if you work with unstructured memory areas or where
you don't really know the real size beforehand.

Regards,
Sven


Thank you

Howard

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] getaddrinfo

2015-03-28 Thread MegaBrutal
Hi,

Can anyone help me with this?


2015-03-20 4:14 GMT+01:00 MegaBrutal :
>
> Hi all,
>
> I'm working on an cross-platform application with DNS resolution with
> IPv6 support, but I can't really get around how should I handle IP
> version independent DNS resolution. After some searching, I see
> getaddrinfo might be a convenient solution, since it is implemented on
> multiple platforms, as it is POSIX-standard. Windows and Linux both
> have it.
>
> Which unit should I link on Windows and Linux to get getaddrinfo? On
> Linux, I managed to link it through cnetdb, though probably there is a
> better way. On Windows, I tried Windows, WinSock and WinSock2, but
> neither has it.
>
> I also need some help with using the function, as it works very
> strangely on Linux, as it returns addrinfo structures, but the ai_addr
> member is always a null-pointer, which is theoretically impossible by
> the manual, in my understanding.
>
> Though if you have a better idea for cross-platform and IP version
> independent network handling with Free Pascal, I'm all ears!
>
>
> ~MegaBrutal
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal