Variable size/Get: Difference between revisions

add RPL
No edit summary
(add RPL)
 
(6 intermediate revisions by 4 users not shown)
Line 147:
Note that when used with a string, <code>LEN</code> reports the length of the string, not its size in memory.
BASIC typically stores information about the string separately from the string itself, usually immediately before the string itself in memory (but some implementations may store such information elsewhere).
 
==={{header|Applesoft BASIC}}===
Simple (non-array) real, integer, or string variables use 7 bytes including the 2 bytes for the variable name. Real variables use all 5 bytes for the value: a 1 byte exponent, and a 4 byte mantissa. Integer variables use 2 bytes for the value, and have 0's in the remaining 3 bytes. String variables use 3 bytes: 1 byte for the length of the string, 2 bytes for the pointer to the string in memory, and have 0's in the remaining 2 bytes.
 
Functions use 2 bytes for the pointer to the function definition in the program, 2 bytes for the pointer to the argument, a 1 byte copy of the first character of the function definition, and 7 bytes to store the argument variable.
 
To get the size of the variable, determine the type of the last variable used. The sizes reported do not include the 2 bytes to store the variable name. The sizes of array values are only for the specific value within an array.
<syntaxhighlight lang="gwbasic"> 100 PRINT "SIZE OF INTEGER I% IS ";:I% = I%: GOSUB 240
110 PRINT "SIZE OF FLOAT I IS ";:I = I: GOSUB 240
120 PRINT "SIZE OF STRING I$ IS ";:I$ = I$: GOSUB 240
130 PRINT " LEN OF STRING I$ IS " LEN (I$)
140 PRINT "SIZE OF FLOAT ARG N IS ";: DEF FN I(N) = 0: GOSUB 240
150 PRINT "SIZE OF FUNCTION FN I IS ";: PRINT MID$ ( STR$ ( FN I(0)),1,0);: GOSUB 240
160 PRINT "ARRAYS:"
170 PRINT "SIZE OF FLOAT I(1) IS ";:I(1) = I(1): GOSUB 240
180 PRINT "SIZE OF INTEGER I%(2) IS ";:I%(2) = I%(2): GOSUB 240
190 PRINT "SIZE OF STRING I$(3) IS ";:I$(3) = I$(3): GOSUB 240
200 PRINT " LEN OF STRING I$(3) IS " LEN (I$(3))
210 PRINT "SIZE OF STRING I$(4) IS ";:I$(4) = "HELLO, WORLD!": GOSUB 240
220 PRINT " LEN OF STRING I$(4) IS " LEN (I$(4))
230 END
240 GOSUB 250: PRINT PEEK (236) + PEEK (237) * 256: RETURN
250 POKE 236,12: POKE 237,0: IF PEEK (129) > 127 AND PEEK (130) < 128 THEN RETURN
260 POKE 236,5
270 IF PEEK (129) < 128 AND PEEK (130) > 127 GOTO 310STR
280 IF PEEK (131) + PEEK (132) * 256 < PEEK (107) + PEEK (108) * 256 THEN RETURN
290 IF PEEK (129) < 128 AND PEEK (130) < 128 THEN RETURN
300 POKE 236,2: IF PEEK (129) > 127 AND PEEK (130) > 127 THEN RETURN
310 IF PEEK (131) + PEEK (132) * 256 > = PEEK (107) + PEEK (108) * 256 THEN POKE 236,3
320 IF PEEK ( PEEK (131) + PEEK (132) * 256) + PEEK (236) > 255 THEN POKE 237,1: POKE 236, PEEK ( PEEK (131) + PEEK (132) * 256) + PEEK (236) - 256: RETURN
330 POKE 236, PEEK ( PEEK (131) + PEEK (132) * 256) + PEEK (236)
340 RETURN</syntaxhighlight>
{{out}}
<pre>
SIZE OF INTEGER I% IS 5
SIZE OF FLOAT I IS 5
SIZE OF STRING I$ IS 5
LEN OF STRING I$ IS 0
SIZE OF FLOAT ARG N IS 5
SIZE OF FUNCTION FN I IS 12
ARRAYS:
SIZE OF FLOAT I(1) IS 5
SIZE OF INTEGER I%(2) IS 2
SIZE OF STRING I$(3) IS 3
LEN OF STRING I$(3) IS 0
SIZE OF STRING I$(4) IS 16
LEN OF STRING I$(4) IS 13
</pre>
 
=={{header|BASIC256}}==
Line 708 ⟶ 756:
<syntaxhighlight lang="j">some_function =: +/ % #
7!:5<'some_function'</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class VariableSize {
 
public static void main(String[] aArgs) {
System.out.println("A Byte variable occupies: " + Byte.SIZE / 8 + " byte");
System.out.println("A Char variable occupies: " + Character.SIZE / 8 + " bytes");
System.out.println("A Short variable occupies: " + Short.SIZE / 8 + " bytes");
System.out.println("A Float variable occupies: " + Float.SIZE / 8 + " bytes");
System.out.println("An Integer variable occupies: " + Integer.SIZE / 8 + " bytes");
System.out.println("A Double variable occupies: " + Double.SIZE / 8 + " bytes");
System.out.println("A Long variable occupies: " + Long.SIZE / 8 + " bytes");
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
A Byte variable occupies: 1 byte
A Char variable occupies: 2 bytes
A Short variable occupies: 2 bytes
A Float variable occupies: 4 bytes
An Integer variable occupies: 4 bytes
A Double variable occupies: 8 bytes
A Long variable occupies: 8 bytes
</pre>
 
=={{header|Julia}}==
Line 927 ⟶ 1,002:
# sizeof(Array.init 10 (fun _ -> String.create 20)) ;;
- : int = 61</syntaxhighlight>
 
=={{header|Odin}}==
 
<syntaxhighlight lang="odin">package main
 
import "core:fmt"
 
main :: proc() {
fmt.println("An int contains", size_of(int), "bytes.")
}</syntaxhighlight>
 
=={{header|Ol}}==
Line 1,170 ⟶ 1,255:
Size of list4 is : 7
Size of list5 is : 5
</pre>
 
=={{header|RPL}}==
Introduced only in 2003, the <code>BYTES</code> command returns both the checksum and the size of an object. Before, Hewlett-Packard provided a table in the user manual to do the math by yourself. Size can be not an integer, as objects are made of nibbles.
{{Works with|HP|49}}
« BYTES NIP » '<span style="color:blue">OBJSIZE</span>' STO
 
42 <span style="color:blue">OBJSIZE</span>
{ 1 2 3 "FOUR" (5,6) [7 8] } <span style="color:blue">OBJSIZE</span>
« BYTES NIP » <span style="color:blue">OBJSIZE</span>
{{out}}
<pre>
3: 6.5
2: 50
1: 20.5
</pre>
 
Line 1,386 ⟶ 1,486:
}</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="textv (vlang)">sizeof(i64)</syntaxhighlight>
{{out}}
<pre>8</pre>
1,150

edits