Variable size/Get: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Perl}}: Changed over to works with and libheader templates)
m (→‎{{header|Perl}}: Added white line.)
Line 55: Line 55:
{{libheader|Devel::Size}}
{{libheader|Devel::Size}}
use Devel::Size;
use Devel::Size;
my $var = 9384752;
my $var = 9384752;
my @arr = (1, 2, 3, 4, 5, 6);
my @arr = (1, 2, 3, 4, 5, 6);

Revision as of 22:23, 20 February 2008

Task
Variable size/Get
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate how to get the size of a variable.

Ada

Ada represents the size of a variable in bits, not bytes like many other languages.

Int_Bits : constant Integer := Integer'size;
Whole_Bytes : constant Integer := Int_Bits / Storage_Unit; -- Storage_Unit is the number of bits per storage element

C

printf("An int contains %d bytes.\n", sizeof(int));

C++

Store the size of an int in bytes:

#include <cstdlib>
std::size_t intsize = sizeof(int);

Note: sizeof can be used without the header <cstdlib>; the latter is only needed for the type std::size_t, which is an alias for whatever type is used to store sizes for the given compiler.

Output the number of bits of an int:

#include <climits>
#include <cstdlib>
std::size_t intbits = CHAR_BITS*sizeof(int);

Note: the type char is always 1 byte (which, however, need not be 8 bits).

Get the size of a variable in bytes:

#include <cstdlib>
int a = 1;
std::size_t a_size = sizeof a;

Note: Parentheses are needed around types, but not around variables.

Get the size of an expression's type:

#include <cstdlib>
std::size_t size = sizeof (3*6 + 7.5);

Delphi

i := sizeof([any variable or structure]);

IDL

IDL is array based, so its size() function is geared towards that:

arr = intarr(3,4)
print,size(arr)
;=> prints this:
       2           3           4           2          12

The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.

Perl

Works with: Perl version 5.x
use Devel::Size;

my $var = 9384752;
my @arr = (1, 2, 3, 4, 5, 6);
print size($var);
print total_size(@arr);

Pop11

From abstract point of view Pop11 variables are bindings between identifiers and values. In concrete terms Pop11 variables store references to values in the heap. Each reference takes one machine word (4 bytes on 32-bit machines and 8 bytes on 64-bit machines). Pop11 identifiers take 3 machine words, but are only needed for "permanent" variables (lexical variables do not need identifiers after compilation). Additionally variable names (words) need space (4 machine for word + space for string corresponding to the word). The bottom line is: variable needs one machine word plus some overhead due to introspection.

Form user point of view more important is space taken by values (size of values referenced by a single variable typically varies during program execution). The datasize function gives amount (in machine words) of space directly used by given value:

;;; Prints 0 because small integers need no heap storage
datasize(12) =>
;;; Prints 3: 3 character fits into single machine word, 1 word
;;; for tag, 1 for length
datasize('str') =>
;;; 3 element vector takes 5 words: 3 for values, 1 for tag, 1 for
;;; length
datasize({1 2 3}) =>
;;; Prints 3 because only first node counts
datasize([1 2 3]) =>

Note that large amount of data my be referenced from given value, but this data is potentially shared, so there is no canonical way to assign it to a single value or variable.

Tcl

Since all variables are ultimately strings in Tcl, this is easy:

string bytelength $var

Toka

There are two primary data types in Toka, cells and characters. The size of these can be obtained easily:

 char-size .
 cell-size .

If you load the floating point support, you can also obtain the size of a floating point number:

 needs floats
 float-size .

All sizes are returned in bytes.