KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q44424: sizeof() Returns 0 for Dereferenced Pointers to Arrays

Article: Q44424
Product(s): See article
Version(s): 2.00
Operating System(s): MS-DOS
Keyword(s): ENDUSER | buglist2.00 | mspl13_c
Last Modified: 18-MAY-1989

The program below demonstrates a problem with the sizeof() keyword in
the Microsoft QuickC Version 2.00 Compiler. When compiled at any
warning level with any options, the program causes the following error
to be generated on each line with the sizeof() expression:

   warning C4034: sizeof returns 0

This problem occurs when the argument to sizeof() is a dereferenced
pointer to an array of any type, regardless of whether or not it has
been typedef'ed.

The Microsoft C Version 5.10 and QuickC 1.01 compilers handle the
sizeof() expression properly.

Microsoft has confirmed this to be a problem in QuickC Version 2.00.
We are researching this problem and will post new information as it
becomes available.

The following program demonstrates the problem:

#include <stdio.h>

typedef char        STRING80[81];

STRING80            *pstr;
int                 (*z)[10];
int                 i;

void main()
{
  i = sizeof( *pstr );  /* Should be 81 */
  i = sizeof( *z );     /* Should be 20 */
}

Examination of the data generated by the sizeof() expression in such a
case reveals that a value of 0 is, in fact, being used. The code
executes but without the desired result.

Because the sizeof() keyword is implemented by the compiler, rather
than as a library function or at the preprocessor level, there is no
way of replacing sizeof(). The workaround for the first sizeof()
statement in the program above is to replace *pstr with STRING80 as
follows:

   i = sizeof ( STRING80 );

The workaround for the second case is to use a constant instead of
calling sizeof().

Microsoft is researching this problem and will post new information as
it becomes available.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.