Adam Naumowicz wrote:
On Wed, 11 Jul 2001, Marco van de Voort wrote:
This is your problem. Use 'New' instead of 'getmem' - i.e., New (a, 3 * sizeof (NameStr)); or New (a, sizeof (shortarray)); or better still, just "New (a);"
Thanks for your answers. But I want my sources to stay BP/Delphi compatible... I still don't know what is wrong with the getmem call. Maybe I should try this example with a newer compiler as Maurice says it works fine for him. It worked fine for me with BP, Delphi and FPC, but all those handle strings in somewhat different manner than GPC. Why is new better than getmem in this example ?
Because automatic initialisation is run that way.
This also goes for FPC and Delphi, not only GPC. Probably you were just lucky, or the compiler was able to detect what you meant.
Or Delphi and FPC used shortstring for this, and GPC schemata. But if you'd use automated types in FPC/Delphi (like ansistring/dynamic arrays) you could run into these problems too. NEW is really better.
Thanks. I read the http://agnes.dida.physik.uni-essen.de/~gnu-pascal/gpc_37.html#SEC37 as the Chief suggested me to do, and now I can see what is the reason of such behaviour. But still I would like to allocate only a part of the array (to simulate a dynamic array - the piece of code I'm working with is from an old non-OOP Pascal version). I also cannot pass size to standard NEW call (at least in BP) ...
Then declare shortarry as an array of string pointers instead of an array of strings, and allocate with new the number of strings you really need. I have done this many times in the past (for large matrices). Still now I prefer the dynamic arrays possible with the schemata of gpc and send BP to hell ... Maurice