Variable size/Set: Difference between revisions
Content added Content deleted
(Applesoft BASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10: | Line 10: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
The 360 architecture data specifications are: |
The 360 architecture data specifications are: |
||
<syntaxhighlight lang="360 assembly"> |
|||
<lang 360 Assembly> |
|||
* Binary interger (H,F) |
* Binary interger (H,F) |
||
I2 DS H half word 2 bytes |
I2 DS H half word 2 bytes |
||
Line 36: | Line 36: | ||
A4 DC A(176) 4 bytes but only 3 bytes used |
A4 DC A(176) 4 bytes but only 3 bytes used |
||
* (24 bits => 16 MB of storage) |
* (24 bits => 16 MB of storage) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
Line 45: | Line 45: | ||
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are. |
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are. |
||
< |
<syntaxhighlight lang="6502asm">MyByte: |
||
byte 0 ;most assemblers will also accept DB or DFB |
byte 0 ;most assemblers will also accept DB or DFB |
||
MyWord: |
MyWord: |
||
word 0 ;most assemblers will also accept DW or DFW |
word 0 ;most assemblers will also accept DW or DFW |
||
MyDouble: |
MyDouble: |
||
dd 0</ |
dd 0</syntaxhighlight> |
||
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum</code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. |
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum</code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. |
||
< |
<syntaxhighlight lang="6502asm">.rsset $00 ;starting at $0400, the following labels represent sequential memory locations of length ".rs n" |
||
VBlankFlag .rs 1 ;$00 |
VBlankFlag .rs 1 ;$00 |
||
soft_PPUCTRL .rs 1 ;$01 |
soft_PPUCTRL .rs 1 ;$01 |
||
Line 61: | Line 61: | ||
soft_SCROLL_Y .rs 1 ;$04 |
soft_SCROLL_Y .rs 1 ;$04 |
||
temp_16 .rs 2 ;$05,$06 |
temp_16 .rs 2 ;$05,$06 |
||
tempStack .rs 1 ;$07</ |
tempStack .rs 1 ;$07</syntaxhighlight> |
||
Assemblers that don't have an <code>enum</code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. Certain 6502 instructions rely on two memory addresses being consecutive. |
Assemblers that don't have an <code>enum</code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. Certain 6502 instructions rely on two memory addresses being consecutive. |
||
< |
<syntaxhighlight lang="6502asm">VBlankFlag equ $00 |
||
soft_PPUCTRL equ $01 |
soft_PPUCTRL equ $01 |
||
soft_PPUSTATUS equ $02 |
soft_PPUSTATUS equ $02 |
||
Line 71: | Line 71: | ||
soft_SCROLL_Y equ $04 |
soft_SCROLL_Y equ $04 |
||
temp_16 equ $05 ;you have to keep track of spacing yourself in this method |
temp_16 equ $05 ;you have to keep track of spacing yourself in this method |
||
tempStack equ $07</ |
tempStack equ $07</syntaxhighlight> |
||
While setting a variable's size is easy, getting it isn't possible without knowing it in advance. The CPU does not (and cannot) know the intended size of a variable. There's no enforcement of types whatsoever on the 6502; anything is fair game. |
While setting a variable's size is easy, getting it isn't possible without knowing it in advance. The CPU does not (and cannot) know the intended size of a variable. There's no enforcement of types whatsoever on the 6502; anything is fair game. |
||
Line 82: | Line 82: | ||
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are. |
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are. |
||
< |
<syntaxhighlight lang="68000devpac">MyByte: |
||
DC.B 0 |
DC.B 0 |
||
EVEN ;you need this to prevent alignment problems if you define an odd number of bytes. |
EVEN ;you need this to prevent alignment problems if you define an odd number of bytes. |
||
Line 88: | Line 88: | ||
DC.W 0 ;this takes up 2 bytes even though only one 0 was written |
DC.W 0 ;this takes up 2 bytes even though only one 0 was written |
||
MyLong: |
MyLong: |
||
DC.L 0 ;this takes up 4 bytes even though only one 0 was written</ |
DC.L 0 ;this takes up 4 bytes even though only one 0 was written</syntaxhighlight> |
||
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum</code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. Assemblers that don't have an <code>enum</code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. These variables are located in the heap and thus there is no need to use <code>EVEN</code> directives to align this data. |
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum</code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. Assemblers that don't have an <code>enum</code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. These variables are located in the heap and thus there is no need to use <code>EVEN</code> directives to align this data. |
||
< |
<syntaxhighlight lang="68000devpac"> |
||
Cursor_X equ $100000 ;byte - only comments can tell you the intended variable size. |
Cursor_X equ $100000 ;byte - only comments can tell you the intended variable size. |
||
Cursor_Y equ $100001 ;byte |
Cursor_Y equ $100001 ;byte |
||
tempWord equ $100002 ;word - also occupies $100003 |
tempWord equ $100002 ;word - also occupies $100003 |
||
tempLong equ $100004 ;long - also occupies $100005,6,7</ |
tempLong equ $100004 ;long - also occupies $100005,6,7</syntaxhighlight> |
||
Line 104: | Line 104: | ||
Syntax will vary depending on the assembler you're using, but the following should apply to most assemblers. |
Syntax will vary depending on the assembler you're using, but the following should apply to most assemblers. |
||
< |
<syntaxhighlight lang="asm"> |
||
.data ;data segment |
.data ;data segment |
||
Line 116: | Line 116: | ||
mov dh, byte ptr [ds:TestValue_00] ;load the value stored at the address "TestValue_00" |
mov dh, byte ptr [ds:TestValue_00] ;load the value stored at the address "TestValue_00" |
||
mov ax, word ptr [ds:TestValue_01] ;load the value stored at the address "TestValue_01"</ |
mov ax, word ptr [ds:TestValue_01] ;load the value stored at the address "TestValue_01"</syntaxhighlight> |
||
For programs that are executed from ROM, such as those on video game cartridges, the above syntax can only be used to define constants. Defining variables will need to be done using <code>equ</code> directives, and you'll need to read the system's documentation to know where the heap is located. For example, the Bandai Wonderswan has its heap located starting at memory address <code>100h</code>. |
For programs that are executed from ROM, such as those on video game cartridges, the above syntax can only be used to define constants. Defining variables will need to be done using <code>equ</code> directives, and you'll need to read the system's documentation to know where the heap is located. For example, the Bandai Wonderswan has its heap located starting at memory address <code>100h</code>. |
||
Line 122: | Line 122: | ||
There is some enforcement of variable sizes, but it's not as strict as most other languages. The two commands above would not have worked had the wrong register sizes been used. However, using the wrong labels is perfectly legal in the eyes of most assemblers. |
There is some enforcement of variable sizes, but it's not as strict as most other languages. The two commands above would not have worked had the wrong register sizes been used. However, using the wrong labels is perfectly legal in the eyes of most assemblers. |
||
< |
<syntaxhighlight lang="asm">mov al, byte ptr [ds:TestValue_02] |
||
;even though this was listed as a dword in the data segment, the assembler lets us do this!</ |
;even though this was listed as a dword in the data segment, the assembler lets us do this!</syntaxhighlight> |
||
In fact, data sizes don't matter to the CPU all that much, as the following definitions are all equivalent: |
In fact, data sizes don't matter to the CPU all that much, as the following definitions are all equivalent: |
||
< |
<syntaxhighlight lang="asm">foo byte 0,0,0,0 |
||
bar word 0,0 |
bar word 0,0 |
||
baz dword 0</ |
baz dword 0</syntaxhighlight> |
||
The data types are more for the programmer's convenience, so that it's clear how the data should be interpreted. |
The data types are more for the programmer's convenience, so that it's clear how the data should be interpreted. |
||
Data can be organized in a table for better readability; however it has no effect on how the data is structured apart from visually. (The data will be organized in a linear fashion regardless, so it makes no difference to the CPU). The following two structures are the same: |
Data can be organized in a table for better readability; however it has no effect on how the data is structured apart from visually. (The data will be organized in a linear fashion regardless, so it makes no difference to the CPU). The following two structures are the same: |
||
< |
<syntaxhighlight lang="asm">Array1 byte 00,01,02,03 |
||
Array2 byte 00,01 |
Array2 byte 00,01 |
||
byte 02,03</ |
byte 02,03</syntaxhighlight> |
||
If you have a variable of a pre-determined size that isn't 1, 2, or 4 bytes you can use the following to define it: |
If you have a variable of a pre-determined size that isn't 1, 2, or 4 bytes you can use the following to define it: |
||
< |
<syntaxhighlight lang="asm">BigNumber byte 256 dup (0) ;reserve 256 bytes, each equals zero</syntaxhighlight> |
||
While it's easy to set a variable's size, getting it is impossible without knowing it in advance. Variables are nothing more than just a section of RAM; the CPU does not (and cannot) know how many bytes your variable is supposed to be. |
While it's easy to set a variable's size, getting it is impossible without knowing it in advance. Variables are nothing more than just a section of RAM; the CPU does not (and cannot) know how many bytes your variable is supposed to be. |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">type Response is (Yes, No); -- Definition of an enumeration type with two values |
||
for Response'Size use 1; -- Setting the size of Response to 1 bit, rather than the default single byte size</ |
for Response'Size use 1; -- Setting the size of Response to 1 bit, rather than the default single byte size</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
Syntax will vary depending on your assembler. VASM uses <code>.byte</code> for 8-bit data, <code>.word</code> for 16-bit data, and <code>.long</code> for 32-bit data, but this is not the norm for most assemblers, as a "processor's word size" is usually the same as its "bitness." I'll use the VASM standard anyway, just because. When defining data smaller than 32-bits, there needs to be sufficient padding to align anything after it to a 4-byte boundary. (Assemblers often take care of this for you, but you can use a directive like <code>.align 4</code> or <code>.balign 4</code>to make it happen. |
Syntax will vary depending on your assembler. VASM uses <code>.byte</code> for 8-bit data, <code>.word</code> for 16-bit data, and <code>.long</code> for 32-bit data, but this is not the norm for most assemblers, as a "processor's word size" is usually the same as its "bitness." I'll use the VASM standard anyway, just because. When defining data smaller than 32-bits, there needs to be sufficient padding to align anything after it to a 4-byte boundary. (Assemblers often take care of this for you, but you can use a directive like <code>.align 4</code> or <code>.balign 4</code>to make it happen. |
||
< |
<syntaxhighlight lang="arm assembly">.byte 0xFF |
||
.align 4 |
.align 4 |
||
.word 0xFFFF |
.word 0xFFFF |
||
.align 4 |
.align 4 |
||
.long 0xFFFFFFFF</ |
.long 0xFFFFFFFF</syntaxhighlight> |
||
Keep in mind that although you may have <i>intended</i> the data to be of a particular size, the CPU does not (and cannot) enforce that you use the proper length version of <code>LDR/STR</code> to access it. It's perfectly legal for the programmer to do the following: |
Keep in mind that although you may have <i>intended</i> the data to be of a particular size, the CPU does not (and cannot) enforce that you use the proper length version of <code>LDR/STR</code> to access it. It's perfectly legal for the programmer to do the following: |
||
< |
<syntaxhighlight lang="arm assembly">main: |
||
ADR r1,TestData ;load the address TestData |
ADR r1,TestData ;load the address TestData |
||
LDRB r0,[r1] ;loads 0x000000EF into r0 (assuming the CPU is operating as little-endian, otherwise it will load 0x000000DE) |
LDRB r0,[r1] ;loads 0x000000EF into r0 (assuming the CPU is operating as little-endian, otherwise it will load 0x000000DE) |
||
BX LR |
BX LR |
||
TestData: |
TestData: |
||
.long 0xDEADBEEF</ |
.long 0xDEADBEEF</syntaxhighlight> |
||
The reverse is also true; you can point a register to the label of a <code>.byte</code> data directive and load it as a long, which will give you the byte along with the padding that it would receive to keep everything aligned. Generally speaking, you don't want to do this, as it can lead to problems where a register doesn't contain what the code assumes it does. However, being able to ignore typing at will can be advantageous, such as when trying to copy large blocks of consecutive memory, where you can achieve more throughput by copying 32 bits per instruction instead of only 8 or 16. |
The reverse is also true; you can point a register to the label of a <code>.byte</code> data directive and load it as a long, which will give you the byte along with the padding that it would receive to keep everything aligned. Generally speaking, you don't want to do this, as it can lead to problems where a register doesn't contain what the code assumes it does. However, being able to ignore typing at will can be advantageous, such as when trying to copy large blocks of consecutive memory, where you can achieve more throughput by copying 32 bits per instruction instead of only 8 or 16. |
||
Line 176: | Line 176: | ||
Variable sizes are in chunks relating to the type of data that they contain. There may also be additional bytes of storage in the variable table that do not show in the dimensions. Typically, strings are allocated in single characters (bytes), so C$(12) in the following example is stored as 12 bytes + additional bytes used for the header in the variable table. In some implementations of basic (such as those that support the storage of variable length strings in arrays), additional terminator characters (such as a trailing Ascii NUL) may also be included. In traditional basic, integers are typically 2 bytes each, so A%(10) contains 10 lots of 2 bytes (20 bytes in total) + additional bytes used for header data in the variable table. Floating point values are typically 8 bytes each, so B(10) holds 10 lots of 8 bytes (80 bytes in total) + additional bytes for header in the variable table: |
Variable sizes are in chunks relating to the type of data that they contain. There may also be additional bytes of storage in the variable table that do not show in the dimensions. Typically, strings are allocated in single characters (bytes), so C$(12) in the following example is stored as 12 bytes + additional bytes used for the header in the variable table. In some implementations of basic (such as those that support the storage of variable length strings in arrays), additional terminator characters (such as a trailing Ascii NUL) may also be included. In traditional basic, integers are typically 2 bytes each, so A%(10) contains 10 lots of 2 bytes (20 bytes in total) + additional bytes used for header data in the variable table. Floating point values are typically 8 bytes each, so B(10) holds 10 lots of 8 bytes (80 bytes in total) + additional bytes for header in the variable table: |
||
< |
<syntaxhighlight lang="basic">10 DIM A%(10): REM the array size is 10 integers |
||
20 DIM B(10): REM the array will hold 10 floating point values |
20 DIM B(10): REM the array will hold 10 floating point values |
||
30 DIM C$(12): REM a character array of 12 bytes</ |
30 DIM C$(12): REM a character array of 12 bytes</syntaxhighlight> |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
Line 188: | Line 188: | ||
Using one character variable names, and reusing variables is one way to save on space. The FRE(0) function can be used to houseclean (garbage collect) unused string space. If arrays are not declared with DIM they default to 11 elements in size. PEEK and POKE can be used to operate on byte and bit sized data. |
Using one character variable names, and reusing variables is one way to save on space. The FRE(0) function can be used to houseclean (garbage collect) unused string space. If arrays are not declared with DIM they default to 11 elements in size. PEEK and POKE can be used to operate on byte and bit sized data. |
||
< |
<syntaxhighlight lang="gwbasic"> 0 CLEAR : PRINT "DATA TYPES":F = 0:F = FRE (0) |
||
1 PRINT "FLOATING POINT:";:G = 0: GOSUB 9 |
1 PRINT "FLOATING POINT:";:G = 0: GOSUB 9 |
||
2 PRINT "INTEGER:";:I0% = 0: GOSUB 9 |
2 PRINT "INTEGER:";:I0% = 0: GOSUB 9 |
||
Line 197: | Line 197: | ||
7 PRINT "ARRAYS OF SIZE 2" |
7 PRINT "ARRAYS OF SIZE 2" |
||
8 PRINT "FLOATING POINT:";: DIM G(1): GOSUB 9: PRINT "INTEGER:";: DIM J%(1): GOSUB 9: PRINT "STRING:";: DIM T$(1): GOSUB 9: END |
8 PRINT "FLOATING POINT:";: DIM G(1): GOSUB 9: PRINT "INTEGER:";: DIM J%(1): GOSUB 9: PRINT "STRING:";: DIM T$(1): GOSUB 9: END |
||
9 PRINT F - FRE (0)" ";:F = FRE (0): RETURN</ |
9 PRINT F - FRE (0)" ";:F = FRE (0): RETURN</syntaxhighlight> |
||
<pre> |
<pre> |
||
DATA TYPES |
DATA TYPES |
||
Line 209: | Line 209: | ||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
The only way to 'set' the size of a scalar numeric variable is to declare it with the appropriate type suffix: |
The only way to 'set' the size of a scalar numeric variable is to declare it with the appropriate type suffix: |
||
< |
<syntaxhighlight lang="bbcbasic"> var& = 1 : REM Variable occupies 8 bits |
||
var% = 1 : REM Variable occupies 32 bits |
var% = 1 : REM Variable occupies 32 bits |
||
var = 1 : REM Variable occupies 40 bits |
var = 1 : REM Variable occupies 40 bits |
||
var# = 1 : REM Variable occupies 64 bits</ |
var# = 1 : REM Variable occupies 64 bits</syntaxhighlight> |
||
If the task is talking about setting the size of a variable ''at run time'' that is only possible with strings, arrays and structures. |
If the task is talking about setting the size of a variable ''at run time'' that is only possible with strings, arrays and structures. |
||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 DIM A(10) |
||
110 NUMERIC ARRAY(1 TO 100) |
110 NUMERIC ARRAY(1 TO 100) |
||
120 NUMERIC MATRIX(1 TO 10,1 TO 10) |
120 NUMERIC MATRIX(1 TO 10,1 TO 10) |
||
Line 222: | Line 222: | ||
140 STRING NAME$(1 TO 100)*24 |
140 STRING NAME$(1 TO 100)*24 |
||
150 STRING LINE$*254 |
150 STRING LINE$*254 |
||
160 STRING CHAR$*1</ |
160 STRING CHAR$*1</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
{{works with|C99}} |
{{works with|C99}} |
||
< |
<syntaxhighlight lang="c">#include <stdint.h> |
||
int_least32_t foo;</ |
int_least32_t foo;</syntaxhighlight> |
||
Here <var>foo</var> is a signed integer with at least 32 bits. [[wp:stdint.h#Minimum-width integer types|stdint.h]] also defines minimum-width types for at least 8, 16, 32, and 64 bits, as well as unsigned integer types. |
Here <var>foo</var> is a signed integer with at least 32 bits. [[wp:stdint.h#Minimum-width integer types|stdint.h]] also defines minimum-width types for at least 8, 16, 32, and 64 bits, as well as unsigned integer types. |
||
< |
<syntaxhighlight lang="c">union u { |
||
int i; |
int i; |
||
long l; |
long l; |
||
double d; |
double d; |
||
/* ... */ |
/* ... */ |
||
};</ |
};</syntaxhighlight> |
||
Here the use of <code>union</code> results in a datatype which is at least as large as the largest type. Unions are sometimes exploited to just meet a minimum size: |
Here the use of <code>union</code> results in a datatype which is at least as large as the largest type. Unions are sometimes exploited to just meet a minimum size: |
||
< |
<syntaxhighlight lang="c">union must_be_at_least_512_bytes { |
||
int interesting_datum; |
int interesting_datum; |
||
char padding[512]; |
char padding[512]; |
||
};</ |
};</syntaxhighlight> |
||
Here, the application will never access <code>padding</code> nor store anything; the padding is there to make the type large enough to meet some requirement. For instance, so that some third party API function which fills in the object, doesn't write past the end of the memory, when the program is only interested in <code>interesting_datum</code>. |
Here, the application will never access <code>padding</code> nor store anything; the padding is there to make the type large enough to meet some requirement. For instance, so that some third party API function which fills in the object, doesn't write past the end of the memory, when the program is only interested in <code>interesting_datum</code>. |
||
Line 250: | Line 250: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{works with|C++11}} or {{works with|Boost}} |
{{works with|C++11}} or {{works with|Boost}} |
||
< |
<syntaxhighlight lang="cpp">#include <boost/cstdint.hpp> |
||
boost::int_least32_t foo;</ |
boost::int_least32_t foo;</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
In D, any variables of static array of zero length has a size of zero. But such data is useless, as no base type element can be accessed. |
In D, any variables of static array of zero length has a size of zero. But such data is useless, as no base type element can be accessed. |
||
< |
<syntaxhighlight lang="d">typedef long[0] zeroLength ; |
||
writefln(zeroLength.sizeof) ; // print 0</ |
writefln(zeroLength.sizeof) ; // print 0</syntaxhighlight> |
||
NOTE: a dynamic array variable's size is always 8 bytes, 4(32-bit) for length and 4 for a reference pointer of the actual storage somewhere in runtime memory.<br> |
NOTE: a dynamic array variable's size is always 8 bytes, 4(32-bit) for length and 4 for a reference pointer of the actual storage somewhere in runtime memory.<br> |
||
The proper candidates of minimum size variable are empty structure, 1-byte size data type variable (include <tt>byte, ubyte, char and bool</tt>), and void, they all occupy 1 byte. |
The proper candidates of minimum size variable are empty structure, 1-byte size data type variable (include <tt>byte, ubyte, char and bool</tt>), and void, they all occupy 1 byte. |
||
< |
<syntaxhighlight lang="d">byte b ; |
||
ubyte ub ; |
ubyte ub ; |
||
char c ; |
char c ; |
||
bool t ;</ |
bool t ;</syntaxhighlight> |
||
<tt>bool</tt> is logically 1-bit size, but it actually occupy 1 byte.<br> |
<tt>bool</tt> is logically 1-bit size, but it actually occupy 1 byte.<br> |
||
<tt>void</tt> can't be declared alone, but <tt>void.sizeof</tt> gives 1.<br> |
<tt>void</tt> can't be declared alone, but <tt>void.sizeof</tt> gives 1.<br> |
||
An empty structure is logically zero size, but still occupy 1 byte. |
An empty structure is logically zero size, but still occupy 1 byte. |
||
< |
<syntaxhighlight lang="d">struct Empty { } |
||
writefln(Empty.sizeof) ; // print 1</ |
writefln(Empty.sizeof) ; // print 1</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Line 283: | Line 283: | ||
Variable sizes are in chunks relating to the type of data that they contain. There may also be additional bytes of storage in the variable table that do not show in the dimensions. Typically, in ERRE strings are allocated in single characters (bytes), so C$[12] in the following example is stored as 12 bytes + additional bytes used for the header in the variable table. Integers are typically 2 bytes each, so A%[10] contains 10 numbers of 2 bytes (20 bytes in total) + additional bytes used for header data in the variable table. Floating point values are typically 4 bytes each, so B[10] holds 10 numbers of 4 bytes (40 bytes in total) + additional bytes for header in the variable table: |
Variable sizes are in chunks relating to the type of data that they contain. There may also be additional bytes of storage in the variable table that do not show in the dimensions. Typically, in ERRE strings are allocated in single characters (bytes), so C$[12] in the following example is stored as 12 bytes + additional bytes used for the header in the variable table. Integers are typically 2 bytes each, so A%[10] contains 10 numbers of 2 bytes (20 bytes in total) + additional bytes used for header data in the variable table. Floating point values are typically 4 bytes each, so B[10] holds 10 numbers of 4 bytes (40 bytes in total) + additional bytes for header in the variable table: |
||
< |
<syntaxhighlight lang="erre">DIM A%[10] ! the array size is 10 integers |
||
DIM B[10] ! the array will hold 10 floating point values |
DIM B[10] ! the array will hold 10 floating point values |
||
DIM C$[12] ! a character array of 12 bytes</ |
DIM C$[12] ! a character array of 12 bytes</syntaxhighlight> |
||
There is also "double" floating point values (8 bytes). Variables of this type use the suffix #. |
There is also "double" floating point values (8 bytes). Variables of this type use the suffix #. |
||
Line 301: | Line 301: | ||
'''selected_int_kind(R)''', where R is the required decimal exponent range. The return value is the kind type parameter for integer values n such that -10^R < n < 10^R. A value of -1 is returned if R is out of range. |
'''selected_int_kind(R)''', where R is the required decimal exponent range. The return value is the kind type parameter for integer values n such that -10^R < n < 10^R. A value of -1 is returned if R is out of range. |
||
< |
<syntaxhighlight lang="fortran">program setsize |
||
implicit none |
implicit none |
||
Line 343: | Line 343: | ||
write(*, form) "iprec4", kind(n7), r6, range(n7) |
write(*, form) "iprec4", kind(n7), r6, range(n7) |
||
end program</ |
end program</syntaxhighlight> |
||
Output |
Output |
||
<pre>KIND NAME KIND NUMBER PRECISION RANGE |
<pre>KIND NAME KIND NUMBER PRECISION RANGE |
||
Line 360: | Line 360: | ||
''See also: [[#Pascal|Pascal]]'' |
''See also: [[#Pascal|Pascal]]'' |
||
Only enumeration type definitions can have a minimum size:< |
Only enumeration type definitions can have a minimum size:<syntaxhighlight lang="pascal">type |
||
{$packEnum 4} |
{$packEnum 4} |
||
enum = (x, y, z);</ |
enum = (x, y, z);</syntaxhighlight> |
||
Only a <tt>{$packEnum}</tt> of <tt>1</tt>, <tt>2</tt>, or <tt>4</tt> Bytes can be specified. |
Only a <tt>{$packEnum}</tt> of <tt>1</tt>, <tt>2</tt>, or <tt>4</tt> Bytes can be specified. |
||
Line 381: | Line 381: | ||
{{trans|Ada}} |
{{trans|Ada}} |
||
For task interpretation this follows the spirit of the Ada example included by the task author. In it, an enumeration type is defined from enumeration values, then a storage size--smaller than the default--is specified for the type. A similar situation exists within Go. Defining types from values is called duck-typing, and the situation where a type smaller than the default can be specified exists when a variable is duck-typed from a numeric literal. |
For task interpretation this follows the spirit of the Ada example included by the task author. In it, an enumeration type is defined from enumeration values, then a storage size--smaller than the default--is specified for the type. A similar situation exists within Go. Defining types from values is called duck-typing, and the situation where a type smaller than the default can be specified exists when a variable is duck-typed from a numeric literal. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 405: | Line 405: | ||
fmt.Println("fMin:", unsafe.Sizeof(fMin), "bytes") |
fmt.Println("fMin:", unsafe.Sizeof(fMin), "bytes") |
||
fmt.Println("cMin:", unsafe.Sizeof(cMin), "bytes") |
fmt.Println("cMin:", unsafe.Sizeof(cMin), "bytes") |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 419: | Line 419: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang="haskell"> |
|||
<lang Haskell> |
|||
import Data.Int |
import Data.Int |
||
import Foreign.Storable |
import Foreign.Storable |
||
Line 436: | Line 436: | ||
task "Int64" i64 |
task "Int64" i64 |
||
task "Int" int |
task "Int" int |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 457: | Line 457: | ||
* lists can be specified with a minimum size (see below): |
* lists can be specified with a minimum size (see below): |
||
< |
<syntaxhighlight lang="icon"> L := list(10) # 10 element list </syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">v=: ''</syntaxhighlight> |
||
Here, v is specified to have a minimum size. In this case, the minimum size of the content is zero, though the size of the representation is somewhat larger. |
Here, v is specified to have a minimum size. In this case, the minimum size of the content is zero, though the size of the representation is somewhat larger. |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">types = [Bool, Char, Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64] |
||
for t in types |
for t in types |
||
Line 476: | Line 476: | ||
println("\nFor the 24-bit user defined type MyInt24, size is ", sizeof(MyInt24), " bytes.") |
println("\nFor the 24-bit user defined type MyInt24, size is ", sizeof(MyInt24), " bytes.") |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
For type Bool size is 1 8-bit bytes, or 8 bits. |
For type Bool size is 1 8-bit bytes, or 8 bits. |
||
For type Char size is 4 8-bit bytes, or 32 bits. |
For type Char size is 4 8-bit bytes, or 32 bits. |
||
Line 497: | Line 497: | ||
The following program shows the range of numbers which the primitive numeric types can accomodate to enable one to choose the appropriate type: |
The following program shows the range of numbers which the primitive numeric types can accomodate to enable one to choose the appropriate type: |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 507: | Line 507: | ||
println("A Float variable has a range of : ${Float.MIN_VALUE} to ${Float.MAX_VALUE}") |
println("A Float variable has a range of : ${Float.MIN_VALUE} to ${Float.MAX_VALUE}") |
||
println("A Double variable has a range of : ${Double.MIN_VALUE} to ${Double.MAX_VALUE}") |
println("A Double variable has a range of : ${Double.MIN_VALUE} to ${Double.MAX_VALUE}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 523: | Line 523: | ||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">TYPE UByte = BITS 8 FOR [0..255];</syntaxhighlight> |
||
Note that this only works for records, arrays, and objects. Also note that the size in bits must be large enough to hold the entire range (in this case, 8 bits is the correct amount for the range 0 to 255) or the compiler will error. |
Note that this only works for records, arrays, and objects. Also note that the size in bits must be large enough to hold the entire range (in this case, 8 bits is the correct amount for the range 0 to 255) or the compiler will error. |
||
Line 533: | Line 533: | ||
3) Nim offers also the possibility to set the size of fields into an object. This is done with the pragma <code>bitsize</code>. For instance, we can specify: |
3) Nim offers also the possibility to set the size of fields into an object. This is done with the pragma <code>bitsize</code>. For instance, we can specify: |
||
< |
<syntaxhighlight lang="nim">type |
||
MyBitfield = object |
MyBitfield = object |
||
flag {.bitsize:1.}: cuint</ |
flag {.bitsize:1.}: cuint</syntaxhighlight> |
||
When Nim uses C as intermediate language, the pragma “bitsize” is translated in C bit fields. The previous example will produce something like that: |
When Nim uses C as intermediate language, the pragma “bitsize” is translated in C bit fields. The previous example will produce something like that: |
||
< |
<syntaxhighlight lang="c">struct mybitfield { |
||
unsigned int flag:1; |
unsigned int flag:1; |
||
};</ |
};</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
Line 546: | Line 546: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<lang |
<syntaxhighlight lang="parigp">default(precision, 1000)</syntaxhighlight> |
||
Alternately, in the gp interpreter, |
Alternately, in the gp interpreter, |
||
<lang |
<syntaxhighlight lang="parigp">\p 1000</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 555: | Line 555: | ||
The GPC (GNU Pascal compiler), however, allows for ''integer'' types the specification of a minimum precision: |
The GPC (GNU Pascal compiler), however, allows for ''integer'' types the specification of a minimum precision: |
||
< |
<syntaxhighlight lang="pascal">type |
||
correctInteger = integer attribute (size = 42);</ |
correctInteger = integer attribute (size = 42);</syntaxhighlight> |
||
''See also: [[#Free Pascal|Free Pascal]]'' |
''See also: [[#Free Pascal|Free Pascal]]'' |
||
Line 580: | Line 580: | ||
mpfr (floating point) variables require the precision to be explicitly specified in binary bits, for example if you want PI to 120 decimal places: |
mpfr (floating point) variables require the precision to be explicitly specified in binary bits, for example if you want PI to 120 decimal places: |
||
{{libheader|Phix/mpfr}} |
{{libheader|Phix/mpfr}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> |
||
Line 587: | Line 587: | ||
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PI with 120 decimals: %s\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">120</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PI with 120 decimals: %s\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">120</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 595: | Line 595: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli"> |
||
declare i fixed binary (7), /* occupies 1 byte */ |
declare i fixed binary (7), /* occupies 1 byte */ |
||
j fixed binary (15), /* occupies 2 bytes */ |
j fixed binary (15), /* occupies 2 bytes */ |
||
Line 612: | Line 612: | ||
y float decimal (16), /* occupies 8 bytes */ |
y float decimal (16), /* occupies 8 bytes */ |
||
z float decimal (33); /* occupies 16 bytes */ |
z float decimal (33); /* occupies 16 bytes */ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang="purebasic"> |
|||
<lang PureBasic> |
|||
EnableExplicit |
EnableExplicit |
||
Line 657: | Line 657: | ||
EndIf |
EndIf |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 802: | Line 802: | ||
<br><br>There's effectively no limit for the precision [or length] for REXX numbers (except for memory), |
<br><br>There's effectively no limit for the precision [or length] for REXX numbers (except for memory), |
||
<br>but eight million is probably the practical limit. |
<br>but eight million is probably the practical limit. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program demonstrates on setting a variable (using a "minimum var size".*/ |
||
numeric digits 100 /*default: 9 (decimal digs) for numbers*/ |
numeric digits 100 /*default: 9 (decimal digs) for numbers*/ |
||
Line 812: | Line 812: | ||
/* [↑] these #'s are stored as coded. */ |
/* [↑] these #'s are stored as coded. */ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">/* Ranges for variables of the primitive numeric types */ |
||
println(s"A Byte variable has a range of : ${Byte.MinValue} to ${Byte.MaxValue}") |
println(s"A Byte variable has a range of : ${Byte.MinValue} to ${Byte.MaxValue}") |
||
println(s"A Short variable has a range of : ${Short.MinValue} to ${Short.MaxValue}") |
println(s"A Short variable has a range of : ${Short.MinValue} to ${Short.MaxValue}") |
||
Line 821: | Line 821: | ||
println(s"A Long variable has a range of : ${Long.MinValue} to ${Long.MaxValue}") |
println(s"A Long variable has a range of : ${Long.MinValue} to ${Long.MaxValue}") |
||
println(s"A Float variable has a range of : ${Float.MinValue} to ${Float.MaxValue}") |
println(s"A Float variable has a range of : ${Float.MinValue} to ${Float.MaxValue}") |
||
println(s"A Double variable has a range of : ${Double.MinValue} to ${Double.MaxValue}")</ |
println(s"A Double variable has a range of : ${Double.MinValue} to ${Double.MaxValue}")</syntaxhighlight> |
||
See it running in your browser by [https://scastie.scala-lang.org/dX0sTLz5Q1ShT8mLL0cv1g Scastie (JVM)]. |
See it running in your browser by [https://scastie.scala-lang.org/dX0sTLz5Q1ShT8mLL0cv1g Scastie (JVM)]. |
||
Line 828: | Line 828: | ||
In Tcl, most values are (Unicode) strings. Their size is measured in characters, and the minimum size of a string is of course 0. |
In Tcl, most values are (Unicode) strings. Their size is measured in characters, and the minimum size of a string is of course 0. |
||
However, one can arrange, via write traces, that the value of a variable is reformatted to bigger size. Examples, from an interactive [[tclsh]] session: |
However, one can arrange, via write traces, that the value of a variable is reformatted to bigger size. Examples, from an interactive [[tclsh]] session: |
||
< |
<syntaxhighlight lang="tcl">% proc format_trace {fmt _var el op} {upvar 1 $_var v; set v [format $fmt $v]} |
||
% trace var foo w {format_trace %10s} |
% trace var foo w {format_trace %10s} |
||
Line 836: | Line 836: | ||
% trace var grill w {format_trace %-10s} |
% trace var grill w {format_trace %-10s} |
||
% puts "/[set grill bar]/" |
% puts "/[set grill bar]/" |
||
/bar /</ |
/bar /</syntaxhighlight>..or limit its size to a certain length: |
||
< |
<syntaxhighlight lang="tcl">% proc range_trace {n _var el op} {upvar 1 $_var v; set v [string range $v 0 [incr n -1]]} |
||
% trace var baz w {range_trace 2} |
% trace var baz w {range_trace 2} |
||
% set baz Frankfurt |
% set baz Frankfurt |
||
Fr</ |
Fr</syntaxhighlight> |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 851: | Line 851: | ||
Here, the buffer holds eight zero bytes, but 4096 bytes is allocated to it: |
Here, the buffer holds eight zero bytes, but 4096 bytes is allocated to it: |
||
< |
<syntaxhighlight lang="txrlisp">(make-buf 8 0 4096)</syntaxhighlight> |
||
Another situation, in the context of FFI, is that some structure needs to achieve some size, but we don't care about all of its members. We can add anonymous padding to ensure that it meets the minimum size. For instance, suppose we want to call <code>uname</code>, and we only care about retrieving the <code>sysname</code>: |
Another situation, in the context of FFI, is that some structure needs to achieve some size, but we don't care about all of its members. We can add anonymous padding to ensure that it meets the minimum size. For instance, suppose we want to call <code>uname</code>, and we only care about retrieving the <code>sysname</code>: |
||
Line 891: | Line 891: | ||
The last feature eliminates the need for explicitly setting the precision of numbers having exact |
The last feature eliminates the need for explicitly setting the precision of numbers having exact |
||
representations, albeit contrary to the convention in physical sciences. |
representations, albeit contrary to the convention in physical sciences. |
||
< |
<syntaxhighlight lang="ursala">p = mpfr..pi 200 # 200 bits of precision |
||
x = mpfr..grow(1.0E+0,1000) # 160 default precision, grown to 1160 |
x = mpfr..grow(1.0E+0,1000) # 160 default precision, grown to 1160 |
||
Line 901: | Line 901: | ||
a = # 180 bits (not the default 160) because of more digits in the constant |
a = # 180 bits (not the default 160) because of more digits in the constant |
||
1.00000000000000000000000000000000000000000000000000000E0</ |
1.00000000000000000000000000000000000000000000000000000E0</syntaxhighlight> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">fn main() { |
||
b := true |
b := true |
||
i := 5 // default type is i32 |
i := 5 // default type is i32 |
||
Line 919: | Line 919: | ||
println("r_min: ${sizeof(r_min)} bytes") |
println("r_min: ${sizeof(r_min)} bytes") |
||
println("f_min: ${sizeof(f_min)} bytes") |
println("f_min: ${sizeof(f_min)} bytes") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b: 1 bytes |
<pre>b: 1 bytes |
||
Line 942: | Line 942: | ||
The programmer cannot specify a minimum size for a Map but can specify a minimum size for a List - in effect the amount of heap storage required to store its elements. This can still be increased dynamically by adding futher elements to the List. Here's an example. |
The programmer cannot specify a minimum size for a Map but can specify a minimum size for a List - in effect the amount of heap storage required to store its elements. This can still be increased dynamically by adding futher elements to the List. Here's an example. |
||
< |
<syntaxhighlight lang="ecmascript">// create a list with 10 elements all initialized to zero |
||
var l = List.filled(10, 0) |
var l = List.filled(10, 0) |
||
// give them different values and print them |
// give them different values and print them |
||
Line 949: | Line 949: | ||
// add another element to the list dynamically and print it again |
// add another element to the list dynamically and print it again |
||
l.add(10) |
l.add(10) |
||
System.print(l)</ |
System.print(l)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 958: | Line 958: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
string 0; \use zero-terminated strings |
string 0; \use zero-terminated strings |
||
char S, |
char S, |
||
Line 970: | Line 970: | ||
I:= I & ~(1<<29); \stores a 0 bit into bit 29 of the integer |
I:= I & ~(1<<29); \stores a 0 bit into bit 29 of the integer |
||
IntOut(0, I>>3 & 1); \displays value of bit 3 |
IntOut(0, I>>3 & 1); \displays value of bit 3 |
||
]</ |
]</syntaxhighlight> |
||
Other than arrays and strings, variables are a fixed size. Integers are |
Other than arrays and strings, variables are a fixed size. Integers are |
||
Line 982: | Line 982: | ||
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are. |
Since these are variables, the value given to them (in this case, 0) is the initial value, and can be changed later at runtime. If you don't care what the initial value is, some assemblers allow you to use a "?" where the 0s are. |
||
< |
<syntaxhighlight lang="z80">MyByte: |
||
byte 0 ;most assemblers will also accept DB or DFB |
byte 0 ;most assemblers will also accept DB or DFB |
||
MyWord: |
MyWord: |
||
word 0 ;most assemblers will also accept DW or DFW |
word 0 ;most assemblers will also accept DW or DFW |
||
MyDouble: |
MyDouble: |
||
dd 0</ |
dd 0</syntaxhighlight> |
||
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum</code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. |
For programs that are executed solely from ROM, such as video game console cartridges, you won't be able to use the above method. The assembler can often use an <code>enum</code> or <code>rsset</code> directive to sequentially assign labels to a series of consecutive memory locations in the system's RAM. |
||
< |
<syntaxhighlight lang="z80">.rsset $C000 ;starting at $C000, the following labels represent sequential memory locations of length ".rs n" |
||
tempByte .rs 1 |
tempByte .rs 1 |
||
tempWord .rs 2 |
tempWord .rs 2 |
||
tempLong .rs 4</ |
tempLong .rs 4</syntaxhighlight> |
||
Assemblers that don't have an <code>enum</code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. When loading from an immediate memory location to a 16-bit register pair (e.g. <code>LD HL,($4000)</code>, the "low register" is loaded with the byte stored at the memory location specified by the operand, and the "high register" is loaded with the byte stored at the memory location after that. |
Assemblers that don't have an <code>enum</code> or <code>rsset</code> directive can use the <code>equ</code> directive instead. This method lets you immediately see what each memory location actually is, but it makes it harder to insert a new one without having to redo all the numbering. When loading from an immediate memory location to a 16-bit register pair (e.g. <code>LD HL,($4000)</code>, the "low register" is loaded with the byte stored at the memory location specified by the operand, and the "high register" is loaded with the byte stored at the memory location after that. |
||
< |
<syntaxhighlight lang="z80">tempByte equ $C000 |
||
tempWord equ $C001 ;high byte at $C002 |
tempWord equ $C001 ;high byte at $C002 |
||
tempLong equ $C003 ;you have to track spacing yourself with this method</ |
tempLong equ $C003 ;you have to track spacing yourself with this method</syntaxhighlight> |
||
While setting a variable's size is easy, getting it isn't possible without knowing it in advance. The CPU does not (and cannot) know the intended size of a variable. There's no enforcement of types whatsoever on the Z80, anything goes. If, for example, you executed <code>LD HL,($4000)</code> and $4000 was <i>intended</i> to be the storage location of an 8-bit value, you'd get the intended value in L and whatever happened to be after it in memory into H. Whether that's a problem or not is up to the programmer, not the CPU. |
While setting a variable's size is easy, getting it isn't possible without knowing it in advance. The CPU does not (and cannot) know the intended size of a variable. There's no enforcement of types whatsoever on the Z80, anything goes. If, for example, you executed <code>LD HL,($4000)</code> and $4000 was <i>intended</i> to be the storage location of an 8-bit value, you'd get the intended value in L and whatever happened to be after it in memory into H. Whether that's a problem or not is up to the programmer, not the CPU. |
||
Line 1,008: | Line 1,008: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
< |
<syntaxhighlight lang="basic">10 DIM a$(10): REM This array will be 10 characters long |
||
20 DIM b(10): REM this will hold a set of numbers. The fixed number of bytes per number is implementation specific |
20 DIM b(10): REM this will hold a set of numbers. The fixed number of bytes per number is implementation specific |
||
30 LET c=5: REM this is a single numerical value of fixed size</ |
30 LET c=5: REM this is a single numerical value of fixed size</syntaxhighlight> |
||
{{omit from|AWK}} |
{{omit from|AWK}} |