Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-01

Re: memory allocation of dynamic array

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt
to Marc Santhoff: a while ago I asked if I can assume the memory held by dynamic arrays is allocated in one…
Marc Santhoff wrote 17 lines
Hi, a while ago I asked if I can assume the memory held by dynamic arrays is allocated in one contiguous block. The answer was sth. like "No, never!". I wrote a small testing programm using SetLenght() on an array multiple times using a bigger size every time and comparing the addresses of the allocated elements. I wasn't able to provoke getting something else than sizeof(integer), which is the base type of that array. My question: In which cases will it happen that the memory of a dynamic array is fragmented into multiple non-contiguous blocks?

Currently this is not done, but you should not assume that this behaviour is intentional. If the compiler people decide to change the implementation (for instance for efficiency reasons), your code will break.

Michael.

Jonas Maebe
to Marc Santhoff: a while ago I asked if I can assume the memory held by dynamic arrays is allocated in one…
Marc Santhoff wrote
My question: In which cases will it happen that the memory of a dynamic array is fragmented into multiple non-contiguous blocks?

Even if the current implementation allocates dynamic arrays always in one block (I think it does, but I'm not certain), there is probably no guarantee it will stay that way forever (and counting on that may consequently break your program in the future).

Counting on implementation details of opaque entities (such as set formats, anything regarding reference counted types such as when references are added or removed and when how many references will exist, rtti format, code generation details such as evaluation order of parameters or operands, memory manager allocation behaviour, ...) is almost 100% guaranteed to break your code at one point or another in time (and often at multiple points in time).

Jonas

Marc Santhoff

Hi,

a while ago I asked if I can assume the memory held by dynamic arrays is allocated in one contiguous block.

The answer was sth. like "No, never!".

I wrote a small testing programm using SetLenght() on an array multiple times using a bigger size every time and comparing the addresses of the allocated elements.

I wasn't able to provoke getting something else than sizeof(integer), which is the base type of that array.

My question:
In which cases will it happen that the memory of a dynamic array is fragmented into multiple non-contiguous blocks?

TIA,
Marc

mm
to Jonas Maebe: Even if the current implementation allocates dynamic arrays always in one block (I think…
Jonas Maebe wrote 5 lines
Counting on implementation details of opaque entities (such as set formats, anything regarding reference counted types such as when references are added or removed and when how many references will exist, rtti format, code generation details such as evaluation order of parameters [...]

What do you mean with "evaluation order of parameters"?

mm

Florian Klaempfl
to mm
Show quoted text 9 lines
Jonas Maebe a écrit : > Counting on implementation details of opaque entities (such as set > formats, anything regarding reference counted types such as when > references are added or removed and when how many references will > exist, rtti format, code generation details such as evaluation order > of parameters [...] What do you mean with "evaluation order of parameters"?

When doing

f(f1(1),f2(2));

there are no assumptions possible if the compiler calls first f1 or f2.

Jonas Maebe
to mm: What do you mean with "evaluation order of parameters"? mm
mm wrote 9 lines
Jonas Maebe a écrit : > Counting on implementation details of opaque entities (such as set > formats, anything regarding reference counted types such as when > references are added or removed and when how many references will > exist, rtti format, code generation details such as evaluation > order of parameters [...] What do you mean with "evaluation order of parameters"?
var
   a: longint;
function f: longint;
begin
   result:=a;
   a:=a+1;
end;
procedure test(a,b: longint);
begin
   writeln(a,' ',b);
end;
begin
   a:=1;
   test(f,f);
end.

The above program can write either "1 2" or "2 1", and what it writes out can change between different compiler versions, platforms and optimization levels (i.e., the parameter evaluation order is undefined).

Jonas