Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?



Skybuck Flying wrote:
No the point is:

If the variable version is banned then you can't make that programming mistake.

The mistake being that of passing a static-array variable to the 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 a compile error.

And then a good programmer like you will automatically know what the problem is, must use SetLength for dynamic arrays.

OK, so when I try to pass the variable to SizeOf, the compiler will 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.

--
Rob
.



Relevant Pages

  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > attributes helps both the writer and the compiler. ... You can build algorithms around language provided ... > Maybe, if one gets used to template programming, the basis of the ... > Try telling someone that useful array attributes, ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > attributes helps both the writer and the compiler. ... You can build algorithms around language provided ... > Maybe, if one gets used to template programming, the basis of the ... > Try telling someone that useful array attributes, ...
    (comp.lang.cpp)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.cpp)
  • Re: How to use Arrays that are not Evil
    ... > explicitly takes a pointer to array of 10 ints. ... > I know you do not, but I think your years of C programming have blinded ... >>Same compiler switches. ...
    (alt.comp.lang.learn.c-cpp)