Re: TCHAR and buffer overflows

From: Alun Jones (alun@texis.com)
Date: 08/26/02


From: alun@texis.com (Alun Jones)
Date: Mon, 26 Aug 2002 21:23:32 GMT

In article <3D6A8140.2090405@127.0.0.1>, Edward Elliott <nobody@127.0.0.1>
wrote:
>Alun Jones wrote:
>> One of my standard #defines is "#define countof(x) (sizeof(x)/sizeof(*x))"
>>
>> I never use sizeof when I mean countof!
>
>Just an aside, I wonder what happens to your macro when fed a C99
>variable length array:
>
>void foo(size_t x)
>{
> int bar[x];
> countof(bar);
>}

To be honest, I don't know. I don't believe my current compiler supports that
- it says "expected constant expression" on the line "int bar[x]".

>Which is to really ask, what happens to sizeof when used on such
>constructs? sizeof is evaluated at compile-time, not run-time. Would
>"sizeof bar" just return the base size of the data type, i.e. "sizeof
>int"? It can't possibly return the size of the array at compile time
>since it isn't known. It appears C99 breaks sizeof constructs for VLAs.

Maybe sizeof(bar) would be a compiler error, since, as you say, the size can
not be determined at run-time, and yet the array definitely has a size, rather
than being an unsized array (where countof always returns 1).

Alun.
~~~~

[Please don't email posters, if a Usenet response is appropriate.]

-- 
Texas Imperial Software   | Try WFTPD, the Windows FTP Server. Find us at
1602 Harvest Moon Place   | http://www.wftpd.com or email alun@texis.com
Cedar Park TX 78613-1419  | VISA/MC accepted.  NT-based sites, be sure to
Fax/Voice +1(512)258-9858 | read details of WFTPD Pro for XP/2000/NT.



Relevant Pages

  • Re: A couple of things from H&S
    ... "If VLA type name appears in a sizeof expression and the value of the ... array size affects the value of sizeof expression, ... Whats analyze? ... compiler determines the type of the expression. ...
    (comp.lang.c)
  • Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned ?
    ... OK, so when I try to pass the variable to SizeOf, the compiler will ... But now I decide I should have a dynamic array instead of a static ... The compiler didn't catch the mistake. ...
    (alt.comp.lang.borland-delphi)
  • Re: De-referencing pointer to function-pointer
    ... >> Either your compiler is broken or you are not invoking it as a C ... >> is a constraint violation to apply the sizeof operator to a function ... >> pointer to the element type of the array. ... >> bytes, of the function, not of a pointer to the function. ...
    (comp.lang.c)
  • Re: TCHAR and buffer overflows
    ... >> I never use sizeof when I mean countof! ... It can't possibly return the size of the array at compile time ... Maybe sizeofwould be a compiler error, since, as you say, the size can ... than being an unsized array (where countof always returns 1). ...
    (comp.os.ms-windows.nt.admin.security)
  • Re: Converting Visual C++ 6.0 application to Unicode
    ... The template version is much more effective as it can't be so easily ... where the _SizeOfArray is coming from or how the use of &_Array, ... The macro _countof calls ... like sizeof and the inferior C version of _countof. ...
    (microsoft.public.vc.mfc)

Quantcast