sizeof

sizeof as byte

Merlin
Administrator
Avatar
Geschlecht:
Alter: 26
Beiträge: 1478
Dabei seit: 03 / 2005
Betreff:

sizeof

 · 
Gepostet: 04.02.2013 - 12:18 Uhr  ·  #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
Geschlecht:
Herkunft: Germany
Alter: 26
Homepage: e-lab.de
Beiträge: 5558
Dabei seit: 03 / 2002
Betreff:

Re: sizeof

 · 
Gepostet: 04.02.2013 - 14:07 Uhr  ·  #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
Geschlecht:
Alter: 26
Beiträge: 1478
Dabei seit: 03 / 2005
Betreff:

Re: sizeof

 · 
Gepostet: 04.02.2013 - 14:28 Uhr  ·  #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
Geschlecht:
Herkunft: Germany
Alter: 26
Homepage: e-lab.de
Beiträge: 5558
Dabei seit: 03 / 2002
Betreff:

Re: sizeof

 · 
Gepostet: 05.02.2013 - 14:54 Uhr  ·  #4
Hello Merlin,

sorry, I could not implement this for some reasons.

rolf
Gewählte Zitate für Mehrfachzitierung:   0

Registrierte in diesem Topic

Aktuell kein registrierter in diesem Bereich

Die Statistik zeigt, wer in den letzten 5 Minuten online war. Erneuerung alle 90 Sekunden.
MySQL Queries: 15 · Cache Hits: 14   79   93 · Page-Gen-Time: 0.040257s · Speichernutzung: 2 MB · GZIP: ein · Viewport: SMXL-HiDPI