[Solved] Are class variables declared on the stack or the heap? lazarus / 62-General ---------------------------------------------------------------------- EganSolo · 2024-02-06 01:32 Consider the following bit of code: 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 type  Root = class;  RootClass = Class of Root;   Root = class  public    Class var R: RootClass;    Class var N: String;  End;  Out of curiosity, are R and N allocated on the stack or the heap? ---------------------------------------------------------------------- KodeZwerg · 2024-02-06 02:15 Quote from: EganSolo on February 06, 2024, 02:32:19 amOut of curiosity, are R and N allocated on the stack or the heap?I would say neither heap or stack, I would say in global memory area. ---------------------------------------------------------------------- Thaddy · 2024-02-06 05:47 Quote from: KodeZwerg on February 06, 2024, 03:15:38 amI would say neither heap or stack, I would say in global memory area. heap is the same as global memory area. and yes, on the heap. ---------------------------------------------------------------------- KodeZwerg · 2024-02-06 09:13 Quote from: Thaddy on February 06, 2024, 06:47:39 amQuote from: KodeZwerg on February 06, 2024, 03:15:38 amI would say neither heap or stack, I would say in global memory area.heap is the same as global memory area. and yes, on the heap.AFAIK you are wrong. To demonstrate I attached an Image about memory layout. IMHO his variables are in the .data segment. ---------------------------------------------------------------------- Fibonacci · 2024-02-06 09:47 R and N are pointers in .data; Their values are on the heap. ---------------------------------------------------------------------- Thaddy · 2024-02-06 09:51 Yes. ---------------------------------------------------------------------- marcov · 2024-02-06 10:29 Heap is in the global memory area (the data segment), but the inverse is not necessarily true. ---------------------------------------------------------------------- Thaddy · 2024-02-06 10:48 I know. ---------------------------------------------------------------------- 440bx · 2024-02-06 13:24 Storage for class variable is reserved in the executable's data segment, _not_ the heap. In the OP's example, storage for R and N is reserved in the data area (they are static variables - class scope "writable constants") The _values_ of R and N _are_ accessible _without_ creating an instance of the class which proves they are stored in the executable's data segment, _not_ on the heap. In the case of R and N, they are both stored in the data segment but their value will normally be to somewhere in a heap block but, that's because both, classes and strings (in FPC) are allocated on the heap.  Manually, a programmer could set the value of those pointers to be anywhere he/she wants (but they probably would no longer point to a class or a string.) This is all documented (though only implicitly) at https://www.freepascal.org/docs-html/ref/refsu24.html Pay particular attention to the line that reads Note that the last line of code references the class type itself (cl), and not an instance of the class (cl1 or cl2). that's what tells you _where_ those variables reside.  If they resided on the heap they could not be accessed using only the class identifier, an instance identifier would be needed (as it is needed in the case of fields.) HTH. ---------------------------------------------------------------------- EganSolo · 2024-02-07 05:18 Thanks guys! That's so helpful. I was wondering how these class variables behave at run-time. Your explanation is very helpful. As a side note, in my search to better understand the global data segment, a link popped up about Valgrind, which I had not considered before. I've been using HeapTrc https://wiki.lazarus.freepascal.org/heaptrc. Seeing that Valgrind runs only under Linux, led me to install it on WSL (Windows Subsystem for Linux) alongside Lazarus-ide and I'm going to experiment with Valgrind. I also saw that there is a newer system called Deleaker (it's commercial) and it is touted as the valgrind for Windows. I'm digressing, but this may be of interest to someone. One never knows where a question could take them, which is why we shouldn't be afraid to ask them ---------------------------------------------------------------------- Thaddy · 2024-02-07 05:37 for windows i would recommend drmemory https://drmemory.org/