Версия для печати темы

Нажмите сюда для просмотра этой темы в обычном формате

Форум «Всё о Паскале» _ Free Pascal, Pascal ABC и другие _ Выделение памяти

Автор: klem4 10.08.2006 15:24

Что-то я не понимаю, почему для Byte и LongInt выделяется одно и тоже кол-во памяти ?

var
pByte : ^Byte;
pLong : ^LongInt;
mem1, mem2 : LongInt;

begin
mem1 := GetHeapStatus.TotalFree;
New(pByte);
mem2 := GetHeapStatus.TotalFree;
writeln('Для pByte выделено ', mem1 - mem2);
New(pLong);
mem1 := GetHeapStatus.TotalFree;
writeln('Для pLong выделено ', mem2 - mem1);
end.

Автор: volvo 10.08.2006 16:01

Внимательно читаем файл prog.pdf (из документации на FPC):

Цитата
8.4.1 Heap allocation strategy

The heap is a memory structure which is organized as a stack. The heap bottom is stored in the variable HeapOrg. Initially the heap pointer (HeapPtr) points to the bottom of the heap. When a variable is allocated on the heap, HeapPtr is incremented by the size of the allocated memory block. This has the effect of stacking dynamic variables on top of each other.

Each time a block is allocated, its size is normalized to have a granularity of 16 bytes.

Автор: klem4 10.08.2006 16:10

Спасибо, так и думал что какая-то нормализация ... TP так-же выдает одинаковый результат - 8.

Автор: klem4 10.08.2006 17:46

Теперь я окончательно ничего не опнимаю :

uses crt;
type
TArray = array of byte;

var
A : TArray;

begin
clrscr;
writeln('Before Init : ', GetHeapStatus.TotalFree);
SetLength(A, 100);
writeln('After Init : ', GetHeapStatus.TotalFree);
SetLength(A, 0);
writeln('After Free : ', GetHeapStatus.TotalFree);
readln;
end.


Цитата

Before Init: 65456
After Init : 98112
After Free : 98224

Автор: volvo 10.08.2006 20:55

А теперь посмотри сюда:

uses crt;
type
TArray = array of byte;

var
A : TArray;

begin
SetLength(A, 10);
SetLength(A, 0);

clrscr;
writeln('Before Init : ', GetHeapStatus.TotalFree);
SetLength(A, 100);
writeln('After Init : ', GetHeapStatus.TotalFree);
SetLength(A, 0);
writeln('After Free : ', GetHeapStatus.TotalFree);
readln;
end.
Чувствуешь? Если ты пытаешься обратиться к GetHeapStatus перед первым обращением к SetLength, то получаешь совсем не то, что получишь после первого SetLength... Скорее всего, для объяснения придется копать в сторону счетчика ссылок (reference count)...

Для начала - глянь здесь: http://www.rsdn.ru/article/Delphi/dynarrays.xml

Автор: klem4 10.08.2006 21:01

Обалдеть ... Спасибо rolleyes.gif