Recent

Author Topic: [Solved] Are class variables declared on the stack or the heap?  (Read 1973 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 395
[Solved] Are class variables declared on the stack or the heap?
« on: February 06, 2024, 02:32:19 am »
Consider the following bit of code:
Code: Pascal  [Select][+][-]
  1. type
  2.   Root = class;
  3.   RootClass = Class of Root;
  4.  
  5.   Root = class
  6.   public
  7.     Class var R: RootClass;
  8.     Class var N: String;
  9.   End;
  10.  

Out of curiosity, are R and N allocated on the stack or the heap?
« Last Edit: February 07, 2024, 06:18:51 am by EganSolo »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Are class variables declared on the stack or the heap?
« Reply #1 on: February 06, 2024, 03:15:38 am »
Out 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.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Are class variables declared on the stack or the heap?
« Reply #2 on: February 06, 2024, 06:47:39 am »
I 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.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Are class variables declared on the stack or the heap?
« Reply #3 on: February 06, 2024, 10:13:20 am »
I 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.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: Are class variables declared on the stack or the heap?
« Reply #4 on: February 06, 2024, 10:47:35 am »
R and N are pointers in .data; Their values are on the heap.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Are class variables declared on the stack or the heap?
« Reply #5 on: February 06, 2024, 10:51:34 am »
Yes.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12644
  • FPC developer.
Re: Are class variables declared on the stack or the heap?
« Reply #6 on: February 06, 2024, 11:29:14 am »
Heap is in the global memory area (the data segment), but the inverse is not necessarily true.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Are class variables declared on the stack or the heap?
« Reply #7 on: February 06, 2024, 11:48:25 am »
I know.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

440bx

  • Hero Member
  • *****
  • Posts: 6081
Re: Are class variables declared on the stack or the heap?
« Reply #8 on: February 06, 2024, 02:24:07 pm »
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.
« Last Edit: February 06, 2024, 02:26:08 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

EganSolo

  • Sr. Member
  • ****
  • Posts: 395
Re: Are class variables declared on the stack or the heap?
« Reply #9 on: February 07, 2024, 06:18:31 am »
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

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: [Solved] Are class variables declared on the stack or the heap?
« Reply #10 on: February 07, 2024, 06:37:45 am »
for windows i would recommend drmemory https://drmemory.org/
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018