Pushing record to heap (Basic Question) lazarus / 62-General ---------------------------------------------------------------------- BrainChemistry · 2018-06-08 16:30 Hi there, let's assume you have a large record. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ExampleRecord = record  entry1: Integer;  entry2: AnotherRecord;  entry3: array[0..1024] of Boolean;  .  .   end;  Well, in case of making the ExampleRecord part of a dyn. array, it would be completly pushed into the heap (is that true?). Let's assume I just need one instance of this record, it would be stored in the stack. What can I do now to get it pushed to the heap? If I do this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i New(@ExampleRecord);  what follows for the elements of the array or the fields of AnotherRecord? Are the elements and the fields on the heap, too? ---------------------------------------------------------------------- Thaddy · 2018-06-08 16:46 Quote from: BrainChemistry on June 08, 2018, 06:30:28 pmHi there, let's assume you have a large record. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ExampleRecord = record  entry1: Integer;  entry2: AnotherRecord;  entry3: array[0..1024] of Boolean;  .  .   end;  Well, in case of making the ExampleRecord part of a dyn. array, it would be completly pushed into the heap (is that true?). Let's assume I just need one instance of this record, it would be stored in the stack. If it is a global record, YES, heap, if it is local to a procedure or function, NO, stack! QuoteWhat can I do now to get it pushed to the heap? It isn't! QuoteIf I do this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i New(@ExampleRecord);  what follows for the elements of the array or the fields of AnotherRecord? Are the elements and the fields on the heap, too? You completely misunderstand what's happening. But IF it is a global record or a local pointer to record then yes.. it is on the heap. But if it is declared on the stack the members are also on the stack, unless declared as a pointer to record and new(0 and Dispose() Then it is on the heap. (And inaccessible when declared as local and you forgot to call dispose: memory leak). When you use the record locally (not the pointer) it will be free'd automatically because it is on the stack (in full) if you use new() and Dispose() locally only the pointer disappears from the stack and you have a memory leak if you do not call dispose. Note if your record contains members of a managed type, these are always on the heap (Like AnsiStrings, classes and interfaces). Even if the record is on the stack. ---------------------------------------------------------------------- BrainChemistry · 2018-06-09 00:35 Hi Thaddy, thanks for your reply! Quote from: Thaddy on June 08, 2018, 06:46:13 pmQuote from: BrainChemistry on June 08, 2018, 06:30:28 pmWell, in case of making the ExampleRecord part of a dyn. array, it would be completly pushed into the heap (is that true?). Let's assume I just need one instance of this record, it would be stored in the stack. If it is a global record, YES, heap, if it is local to a procedure or function, NO, stack! Quote from: Thaddy on June 08, 2018, 06:46:13 pmQuote from: BrainChemistry on June 08, 2018, 06:30:28 pmIf I do this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i New(@ExampleRecord);  what follows for the elements of the array or the fields of AnotherRecord? Are the elements and the fields on the heap, too? You completely misunderstand what's happening. But IF it is a global record or a local pointer to record then yes.. it is on the heap. But if it is declared on the stack the members are also on the stack, unless declared as a pointer to record and new(0 and Dispose() Then it is on the heap. (And inaccessible when declared as local and you forgot to call dispose: memory leak). When you use the record locally (not the pointer) it will be free'd automatically because it is on the stack (in full) if you use new() and Dispose() locally only the pointer disappears from the stack and you have a memory leak if you do not call dispose. Note if your record contains members of a managed type, these are always on the heap (Like AnsiStrings, classes and interfaces). Even if the record is on the stack. I messed up the second code snippet since I was in a hurry on writing, sorry for that inconvenience. It should better have read like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PExampleRecord = ^ExampleRecord // type sec...New(PExampleRecord); // begin..end. sec.  Well, about your description I try to translate it into code and I'd appreciate to learn if my assumptions about the used memory is correct. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //global var sec.var    MyGlobalRecord: ExampleRecord;  ..// procedure declarationprocedure AProcedure;var  MyLocalRecord: ExampleRecord;  PMyLocalRecord: ^ExampleRecord;..New(PMyLocalRecord); // the record and all its members are on the heap..MyLocalRecord := AnotherRecord; // MyLocalRecord and all its members are on the stack // begin..end. blockbegin  MyGlobalRecord := AnotherRecord; // MyGlobalRecord and all its members are on the heap  end. ---------------------------------------------------------------------- Thaddy · 2018-06-09 07:01 Here's a summary program. Use the debugger to see the different behavior. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program records;// example of record behavior// assumes record without members that need finalization{$mode objfpc}type  PMyRecord = ^TMyRecord;  TmyRecord = record  a,b:integer;  c:string[255];  end;  procedure Localrecord;var a:TMyRecord;begin  //stack allocated;  a.a := 100;  a.b := 200;  a.c := 'Test Local';  writeln(a.c);end;procedure LocalDynamicRecord;var a:PMyRecord;begin  // Pointer on stack record on heap;  New(a);  // code  a^.a := 100;  a^.b := 200;  a^.c := 'Test Local Dynamic';  writeln(a^.c);  Dispose(a);end;  var   //heap allocated, no clean up necessary  GlobalRecord:TMyrecord;   //dynamically heap allocated, needs clean up.  GlobalDynamicRecord:PMyRecord;   begin  GlobalRecord.a := 100;  GlobalRecord.b := 200;  GlobalRecord.c := 'Test Global';  writeln(GlobalRecord.c);  New(GlobalDynamicRecord);  // code  GlobalDynamicRecord^.a := 100;  GlobalDynamicRecord^.b := 200;  GlobalDynamicRecord^.c := 'Test Global Dynamic';  writeln(GlobalDynamicRecord^.c);  Dispose(GlobalDynamicRecord);  LocalRecord;  LocalDynamicRecord;end. ---------------------------------------------------------------------- BrainChemistry · 2018-06-09 12:22 Thanks for your effort, it helped alot!