sizeof

sizeof as byte

Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1372
Registered: 03 / 2005
Subject:

sizeof

 · 
Posted: 04.02.2013 - 12:18  ·  #1
Hi Rolf.

For most structures sizeof(...) returns a value of less than 256, so it would be nice if the following were allowed without error

Code
var
  iLen : byte;
begin
  iLen := sizeof( ...);


provided, of course, that the result (which is, after all, a constant, albeit one calculated by the compiler) was less than 255.

One can, of course, use

Code
var
  iLen : byte;
begin
  iLen := byte(sizeof( ...));


but it is less elegant, and actually less safe, because if the structure happens to be more than 255 bytes the above is legal but wrong.

Also in structures like

Code
var
  i : byte;
begin
  for i := 0 to sizeof( ...) - 1 do


rather than

Code
var
  i : byte;
begin
  for i := 0 to byte(sizeof( ...)) - 1 do


which is quite ugly.

Obviously not an important or urgent refinement...

Regards

Merlin.
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: sizeof

 · 
Posted: 04.02.2013 - 14:07  ·  #2
Hi Merlin,

you suggest that this function result should be dependend of the destination size?
If the destination is a byte then SizeOf(byteType) should be a byte.
If the destination is a word then SizeOf(byteType) should be a word.
And if the result is a word (SizeOf(wordType)) and the destination is a byte the
compiler raises an error.
If the result is a byte and the destination is a word it should work as already does.
Right?
But it must be clear that no byte of code then is saved. It is only optical...

rolf
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1372
Registered: 03 / 2005
Subject:

Re: sizeof

 · 
Posted: 04.02.2013 - 14:28  ·  #3
Hi Rolf.

Yes, exactly so on all counts, if I understand what you are saying correctly. 'byteType' I take to mean a structure 255 bytes or fewer in size, and 'wordType' means a structure bigger than this.

Yes, it is purely visual, and the resultant assembler code will be identical to now.

But easier to read (and write!) code is good.

Regards

Merlin.
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: sizeof

 · 
Posted: 05.02.2013 - 14:54  ·  #4
Hello Merlin,

sorry, I could not implement this for some reasons.

rolf
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 14 · Cache Hits: 14   79   93 · Page-Gen-Time: 0.02813s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI