Variable size/Set: Difference between revisions

From Rosetta Code
Content added Content deleted
(+D)
m (→‎{{header|D}}: mistake, void also has sizeof property)
Line 17: Line 17:
bool t ;
bool t ;
<code>bool</code> is logically 1-bit size, but it actually occupy 1 byte.<br>
<code>bool</code> is logically 1-bit size, but it actually occupy 1 byte.<br>
<code>void</code> can't be declared alone, and has no <code>sizeof</code> property, but it can be deduced that it has 1 byte size, eg.
<code>void</code> can't be declared alone, but <code>void.sizeof</code> gives 1.<br>
byte[] b ; // byte has sizeof 1 bytes
b.length = 8 ; // b now should be 8*1 = 8 bytes of size
b[] = cast(byte[]) new void[8] ;
writefln("byte.sizeof * 8 match void.sizeof * 8");
b.length = 16 ; // b now should be 16*1 = 16 bytes
b[] = cast(byte[]) new void[8] ; // any length of void[] other than 16 gives error
writefln("byte.sizeof * 16 match void.sizeof * 8");
when runs, gives
byte.sizeof * 8 match void.sizeof * 8
Error: lengths don't match for array copy,16 = 8
An empty structure is logically zero size, but still occupy 1 byte.
An empty structure is logically zero size, but still occupy 1 byte.
struct Empty { }
struct Empty { }
writefln(Empty.sizeof) ; // print 1
writefln(Empty.sizeof) ; // print 1

=={{header|Perl}}==
=={{header|Perl}}==
I suppose you could use vec() or similar to twiddle a single bit. The thing is, as soon as you store this in a variable, the SV (the underlying C implementation of the most simple data type) already takes a couple dozen of bytes.
I suppose you could use vec() or similar to twiddle a single bit. The thing is, as soon as you store this in a variable, the SV (the underlying C implementation of the most simple data type) already takes a couple dozen of bytes.

Revision as of 09:19, 11 March 2008

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

Demonstrate how to specify the minimum size of a variable or a data type.

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

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.

typedef long[0] zeroLength ; 
writefln(zeroLength.sizeof) ; // print 0 

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.
The proper candidates of minimum size variable are empty structure, 1-byte size data type variable (include byte, ubyte, char and bool), and void, they all occupy 1 byte.

byte b ;
ubyte ub ;
char c ;
bool t ;

bool is logically 1-bit size, but it actually occupy 1 byte.
void can't be declared alone, but void.sizeof gives 1.
An empty structure is logically zero size, but still occupy 1 byte.

struct Empty { }
writefln(Empty.sizeof) ; // print 1

Perl

I suppose you could use vec() or similar to twiddle a single bit. The thing is, as soon as you store this in a variable, the SV (the underlying C implementation of the most simple data type) already takes a couple dozen of bytes.

In Perl, memory is readily and happily traded for expressiveness and ease of use.