Помощь - Поиск - Пользователи - Календарь
Полная версия: Выделение памяти
Форум «Всё о Паскале» > Современный Паскаль и другие языки > Free Pascal, Pascal ABC и другие
klem4
Что-то я не понимаю, почему для 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
Внимательно читаем файл 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
Спасибо, так и думал что какая-то нормализация ... TP так-же выдает одинаковый результат - 8.
klem4
Теперь я окончательно ничего не опнимаю :

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
А теперь посмотри сюда:

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)...

Для начала - глянь здесь: Длинные строки и динамические массивы в Delphi
klem4
Обалдеть ... Спасибо rolleyes.gif
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.