Address of a variable: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[C]]/[[C plus plus|C++]]: point to C++ instead of C plus plus)
m (Changed over to headers. Got the "C / C++" one to work surprisingly.)
Line 3: Line 3:
Demonstrate how to get the address of a variable and how to set the address of a variable.
Demonstrate how to get the address of a variable and how to set the address of a variable.


==[[Ada]]==
=={{header|Ada}}==
[[Category:Ada]]
===Get The Address===
===Get The Address===
The_Address : System.Address;
The_Address : System.Address;
Line 18: Line 17:
For I'Address use J'Address;
For I'Address use J'Address;


==[[C sharp | C#]]==
=={{header|C Sharp|C #}}==
[[Category:C sharp]]
'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]


Line 36: Line 34:




==[[C]]/[[C++]]==
=={{header|C}} / {{header|C++}}==
[[Category:C]]
[[Category:C++]]
'''Compiler:''' [[GCC]]
'''Compiler:''' [[GCC]]
=== Get the address ===
=== Get the address ===
Line 58: Line 54:
Note that in this case, the variables can be non-static.
Note that in this case, the variables can be non-static.


==[[Delphi]]==
=={{header|Delphi}}==
[[Category:Delphi]]


Pascal supports the @ ( address of ) operator and the var : [type] absolute declaration.
Pascal supports the @ ( address of ) operator and the var : [type] absolute declaration.
Line 80: Line 75:
StrLen : byte absolute Str ;
StrLen : byte absolute Str ;


==[[Forth]]==
=={{header|Forth}}==
[[Category:Forth]]
Variables and created memory blocks return their address when refrerenced. The "fetch" operator '''@''' could also be pronounced "dereference".
Variables and created memory blocks return their address when refrerenced. The "fetch" operator '''@''' could also be pronounced "dereference".
variable foo
variable foo
Line 95: Line 89:
42 buffer 20 cells + !
42 buffer 20 cells + !


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
To get the address, stringify the reference to a variable. Also see Devel::Peek.
To get the address, stringify the reference to a variable. Also see Devel::Peek.
print \my $v; # SCALAR(0x8167cf4)
print \my $v; # SCALAR(0x8167cf4)
Line 103: Line 96:
Changing the address of a variable is not easily possible, but see perlapi. Wanting to go against the automatic memory management is a sign that this is only used to hack around the deficiencies of dafter languages. I can imagine address munging is commonly used to make variable aliasing possible, but Perl already has a higher level syntax for that.
Changing the address of a variable is not easily possible, but see perlapi. Wanting to go against the automatic memory management is a sign that this is only used to hack around the deficiencies of dafter languages. I can imagine address munging is commonly used to make variable aliasing possible, but Perl already has a higher level syntax for that.


==[[Python]]==
=={{header|Python}}==


Python traditionally doesn't support low-level operations on memory addresses, except in the limited sense that one can use the ''mmap'' module where it's available, and manipulate offsets into memory map objects ... including serializing other objects into and out of the memory mapping. New versions of Python support a ''ctypes'' module which permits some low level address operations on C-type objects (see [http://docs.python.org/lib/ctypes-ctypes-reference.html C-types Reference] for details).
Python traditionally doesn't support low-level operations on memory addresses, except in the limited sense that one can use the ''mmap'' module where it's available, and manipulate offsets into memory map objects ... including serializing other objects into and out of the memory mapping. New versions of Python support a ''ctypes'' module which permits some low level address operations on C-type objects (see [http://docs.python.org/lib/ctypes-ctypes-reference.html C-types Reference] for details).
Line 114: Line 107:
(In addition some folks have written binary Python modules which implement "peek" and "poke" operations; but these are non-standard).
(In addition some folks have written binary Python modules which implement "peek" and "poke" operations; but these are non-standard).


==[[Toka]]==
=={{header|Toka}}==
[[Category:Toka]]


===Get the Address===
===Get the Address===

Revision as of 20:30, 11 November 2007

Task
Address of a variable
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

Demonstrate how to get the address of a variable and how to set the address of a variable.

Ada

Get The Address

The_Address : System.Address;
I : Integer;
The_Address := I'Address;

Set The Address

Set the address of a variable to address A100 in hexidecimal

I : Integer;
for I'Address use 16#A100#;

Set the address of one varible to the address of another variable, creating an overlay.

I : Integer;
J : Integer;
For I'Address use J'Address;

C#

Platform: .NET

Language Version: 2.0+

Compiler: Visual C# 2005

Get the address

Note that void* is a "pure" address which doesn't carry the type information anymore. If you need the type information (e.g. to recover the variable itself in a type safe manner), use a pointer to the appropriate type instead; in this case int*.

unsafe
{
  int i = 5;
  void* address_of_i = &i;
}


C / C++

Compiler: GCC

Get the address

Note that void* is a "pure" address which doesn't carry the type information anymore. If you need the type information (e.g. to recover the variable itself in a type safe manner), use a pointer to the appropriate type instead; in this case int*.

int i;
void* address_of_i = &i;

Set the address

While C++ doesn't directly support putting a variable at a given address, the same effect can be achieved by creating a reference to that address:

int& i = *(int*)0xA100;

Overlaying of variables is done with anonymous unions; however at global/namespace scope such variables have to be static (i.e. local to the current file):

static union
{
  int i;
  int j;
};

C++ only: An alternative (and cleaner) solution is to use references:

int i;
int& j = i;

Note that in this case, the variables can be non-static.

Delphi

Pascal supports the @ ( address of ) operator and the var : [type] absolute declaration.

To get the address of any variable, structure, procedure or function use the @ operator.

 var
   Int : integer ;
   p   : pointer ;
 begin
   P := @int ;
   writeln(integer(p^));
 end;

A variable can be declared as absolute ie: to reside at a specific address.

 Var
   CrtMode : integer absolute $0040 ;
   Str     : string[100] ;
   StrLen  : byte absolute Str ;

Forth

Variables and created memory blocks return their address when refrerenced. The "fetch" operator @ could also be pronounced "dereference".

variable foo
foo .  \ some large number, an address
8 foo !
foo @ .  \ 8

You can define a constant or value with an address, which then acts like a variable. This can be used to refer to fixed addresses (such as I/O ports), graphics buffers, or allocated memory.

$3F8 constant LPT1:
8 LPT1: !
100 cells allocate throw value buffer
42 buffer 20 cells + !

Perl

To get the address, stringify the reference to a variable. Also see Devel::Peek.

print \my $v;  # SCALAR(0x8167cf4)

Use Devel::Pointer::PP if you want to dereference a certain address in memory.

Changing the address of a variable is not easily possible, but see perlapi. Wanting to go against the automatic memory management is a sign that this is only used to hack around the deficiencies of dafter languages. I can imagine address munging is commonly used to make variable aliasing possible, but Perl already has a higher level syntax for that.

Python

Python traditionally doesn't support low-level operations on memory addresses, except in the limited sense that one can use the mmap module where it's available, and manipulate offsets into memory map objects ... including serializing other objects into and out of the memory mapping. New versions of Python support a ctypes module which permits some low level address operations on C-type objects (see C-types Reference for details).

The Python id() function returns a unique ID for any object. This just happens to be implemented as the base address of the object in C Python; but that is not guaranteed by the semantics of the language and should not be considered a standard, nor used as such.

foo = object()  # Create (instantiate) an empty object
address = id(foo)

(In addition some folks have written binary Python modules which implement "peek" and "poke" operations; but these are non-standard).

Toka

Get the Address

The default behaviour of a data element in Toka is to return its address. This makes obtaining the address trivial:

variable foo
foo .

Set the Address

You can manually assign a name to any memory address (or other number), but you should make sure it's part of allocated memory first.

 hex abcdef is-data foo
 foo .