Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Bugs and Fixes

sizeof Array[]

A community discussion post from the original code:zIRON forum.

0CodErr member
Posts
201
Threads
37
Location
Unknown

sizeof Array[]

program RAW_IMAGE 'test';

#set bits 32;

dword arr[] = [0, 1, 2, 3, 4, 5, 6, 7];
$echo sizeof arr;

The code above gets only 4.

Also is it possible to use $echo like print()? For example:

$echo 'Size = ', sizeof arr;

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

I think it may be worth to write a separate macro for such case... e.g. inline procedure echof .....

I may write one today to add to new rtl.

As for size of, I will correct this.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

OK, corrected sizeof and I have added a new macro $echof.

$echof('Hello World ', 123, ' etc!');
0CodErr member
Posts
201
Threads
37
Location
Unknown

Don't know is it right or not:

program RAW_IMAGE 'test';

#set bits 32;

const s1 = '123456';

byte s2 [] = '123456';
byte s3 [] = s1;

$echof 'sizeof s1 = ', sizeof s1;
$echof 'sizeof s2 = ', sizeof s2;
$echof 'sizeof s3 = ', sizeof s3;

Result:

sizeof s1 = 7
sizeof s2 = 6
sizeof s3 = 6

Seems that zero-byte also was added to s1. But not in s3.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Yes, consts add null byte, but arrays just use the data which was written in the string, adding null byte is users option.

Edit: I have added string special char x for next release.

e.g.

byte s2 [] = '123456\x0';
0CodErr member
Posts
201
Threads
37
Location
Unknown

Another way -- use #define instead of const. But result the same. It strange :)

#define s1 '123456';

byte s2 [] = '123456';
byte s3 [] = s1;

And for example NASM version:

%define s1 '123456';

s2 db '123456';
s3 db s1;

Result:

0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
Colin admin
Posts
928
Threads
49
Location
Bulgaria

This is still normal behaviour, when assigning any string to an array, the null byte is not included unless you explicitly add it.