Forum > Beginners

Printing a variable's address to the console [SOLVED]

(1/3) > >>

YiannisKam:
In C language I can easily print a variable's address just by using the & operator.

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---int x = 100;printf("Value of x: %d\n", x);printf("Address of x: %p\n", &x); with an output that looks like

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Value of x: 100Address of x: 000000D83FEFFBE4
I'm trying to do the same in Pascal but the output I get doesn't look correct. Or is it?
Here is the code and the output

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---begin  x := 100;  writeln('Value of x: ', x);  writeln('Address of x: ', HexStr(@x));   readlnend.Output:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Value of x: 100Address of x: 0000000100021010
I realized I can't just type something like @x with the writeln function. Is HexStr(@x) the right approach? And no matter how many times I run the program in Pascal I always get as an address numbers between 0,1,2s. That doesn't look ok when comparing with C's hex value.

Bogen85:

--- Quote from: YiannisKam on December 05, 2022, 07:37:23 pm ---I realized I can't just type something like @x with the writeln function. Is HexStr(@x) the right approach? And no matter how many times I run the program in Pascal I always get as an address numbers between 0,1,2s. That doesn't look ok when comparing with C's hex value.

--- End quote ---


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program address;uses sysutils;var  x : integer;begin  x := 100;  writeln(format('Value of x: %d', [x]));  writeln(format('Address of x: %p', [@x]));  readln;end. 

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$ fpc -g address.pasFree Pascal Compiler version 3.2.2 [2022/08/17] for x86_64Copyright (c) 1993-2021 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling address.pasLinking address10 lines compiled, 0.2 sec 

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$ ./addressValue of x: 100Address of x: 000000000047D530 

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$ objdump -t ./address  | fgrep -i d530000000000047d530 l     O .bss   0000000000000002 U_$P$ADDRESS_$$_X000000000045c650 l     O .rodata        000000000000000f .Ld530 
Looks correct to me...

YiannisKam:
I've tried also with the format function with the same result. Maybe because I'm on windows.. I don't know if that's the case.

Bogen85:

--- Quote from: YiannisKam on December 05, 2022, 07:50:43 pm ---I've tried also with the format function with the same result. Maybe because I'm on windows.. I don't know if that's the case.

--- End quote ---

I don't think that is the case (being on windows, though I'm on Linux).

For this:

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#include <stdio.h> int x = 100; int main() {    printf("Value of x: %d\n", x);    printf("Address of x: %p\n", &x);    return 0;} 
If outside of main I get a different address range than if inside main.


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$ gcc -g -o address_c address.c $ ./address_cValue of x: 100Address of x: 0x404024 $ objdump -t ./address_c | fgrep -i 40240000000000404024 g     O .data  0000000000000004              x 
Buf if inside main:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$ ./address_cValue of x: 100Address of x: 0x7fffc1a330fc 
It all depends are where the respective compilers/linkers are allocating these variables, and whether they are from the stack or the heap.


Martin_fr:

--- Quote from: YiannisKam on December 05, 2022, 07:37:23 pm ---Output:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Value of x: 100Address of x: 0000000100021010
I realized I can't just type something like @x with the writeln function. Is HexStr(@x) the right approach? And no matter how many times I run the program in Pascal I always get as an address numbers between 0,1,2s. That doesn't look ok when comparing with C's hex value.

--- End quote ---

The address looks actually quite possible.... Depends on the base address for your exe.

Afaik FPC compiles with a fixed base/relocation address => so the range for global variables will always be the same. (Also for locals as the stack address range will also be similar each time).  Fields of dynamically created objects may be a bit more variable.

Check out the fpc command line option, there may be one to change the default reloc address, or make it dynamic.... (or maybe that is an option that needs to be forwarded to the linker.

Try

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  x,y,z : integer;
Then the addresses of those 3 should be in steps of 4 (or 8, depends on alignment).

Navigation

[0] Message Index

[#] Next page

Go to full version