Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned ?
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Mon, 07 Jan 2008 22:16:21 -0600
Skybuck Flying wrote:
Rob Kennedy wrote:
Skybuck Flying wrote:No the point is:The mistake being that of passing a static-array variable to the
If the variable version is banned then you can't make that
programming mistake.
SizeOf function. There's of course no problem with doing that since
the size of the variable really is the size of the type.
If you do try to make that mistake the compiler will give you aOK, so when I try to pass the variable to SizeOf, the compiler will
compile error.
And then a good programmer like you will automatically know what
the problem is, must use SetLength for dynamic arrays.
complain and I'll know to fix something. The fix will not be to call
SetLength, though, since that doesn't tell me anything about the size
of the variable.
Instead, I'll need to go look up the declared type of the variable
and enter that type as the argument to SizeOf instead of the variable
itself. The program compiles and all is well.
But now I decide I should have a dynamic array instead of a static
array. So I go change the type declaration of the array type. I
remove the array bounds from the declaration, press Ctrl+F9, and
everything continues to compile.
Oops. The compiler didn't catch the mistake. The mistake that you
were hoping to eliminate by forbidding asking for the size of a
variable. Being required to ask for the size of a type doesn't gain
you anything in this scenario.
Ok, when changing from static array type to dynamic array type, sizeof
would be legal and return 4 bytes for the dynamic array type.
That doesn't fix anything. Delphi already behaves that way.
Your claim is that by forbidding SizeOf on variables, and forcing SizeOf on types only, the compiler will catch errors. That's simply not true.
I want to see an example where calling SizeOf on a variable leads to a mistake that would have been fixed by calling SizeOf on a type instead.
By the way, there is one situation in Delphi where calling SizeOf on a variable is the only thing you can do -- it's impossible to call SizeOf on that variable's type.
However why would somebody dynamically allocate a static array instead
of using a dynamic array in the first place. Doesn't make too much
sense.
When did I say the static array was being allocated dynamically? If it were, the same code wouldn't have compiled when the array type was changed to dynamic.
Static arrays on the stack are a bad idea anyway, think "buffer
overrun".
Who said anything about the stack?
--
Rob
.
- References:
- Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable) be banned ?
- From: Skybuck Flying
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable) be banned ?
- From: Rob Kennedy
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Rudy Velthuis
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Skybuck Flying
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Rob Kennedy
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Skybuck Flying
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Rob Kennedy
- Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable) be banned ?
- Prev by Date: Re: Need Fast 2D Graphics
- Next by Date: Re: Can't load gdiplus dynamically
- Previous by thread: Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- Next by thread: Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned?
- Index(es):
Relevant Pages
|