Invalid number of occurrencies in arrays

Carlo Wood carlo@alinoe.com
Fri Jun 8 06:52:00 GMT 2001


This indeed seems to be a bug because the initialization occurs
as you expected as it evident from:

-----------------------------
#include <stdio.h>

enum VT_ARGS { ONE=1,  TWO, THREE };

typedef unsigned short VARTYPE;

typedef struct tagINTRINSIC
{
    char * szType;
    VARTYPE vt;
} INTRINSIC;

INTRINSIC rgIntrinsic [] =
{
    "one",   ONE,
    "two",   TWO,
    "three", THREE
};

int main(void)
{

  printf("sizeof(rgIntrinsic) = %d - Should be 24.\n", sizeof(rgIntrinsic));


  printf("Number of occurencies: %d - ", sizeof(rgIntrinsic)/sizeof(INTRINSIC));
  printf("Should be 3.\n");

  for (int i = 0; i < sizeof(rgIntrinsic)/sizeof(INTRINSIC); ++i)
    printf("%s %d\n", rgIntrinsic[i].szType, rgIntrinsic[i].vt);

  return 0;
}
-----------------------------

>./a.out
sizeof(rgIntrinsic) = 48 - Should be 24.
Number of occurencies: 6 - Should be 3.
one 1
two 2
three 3
(null) 0
(null) 0
(null) 0


The problem is that you forgot to add { } around the initializers however.
Using

INTRINSIC rgIntrinsic [] =
{
    { "DATE",         VT_DATE },
    { "HRESULT",      VT_HRESULT },
    { "LPSTR",        VT_LPSTR },
... etc

solves the problem.

On Fri, Jun 08, 2001 at 10:24:55AM +0200, Stuehlmeyer, Wolfgang wrote:
> Hello,
> 
> i have segmentation violations with snapshot 2001-06-04.
> 
> The reason is a array with a invalid number of occurrencies.
> 
> 
> Best regards 
> Wolfgang
> 
> 
> Example:
> 
> #include <stdio.h>
> 
> enum VT_ARGS { VT_DATE=1,  VT_HRESULT, VT_LPSTR, VT_LPWSTR, VT_ERROR,
> VT_BOOL,  VT_BSTR, VT_VARIANT, VT_CY, VT_DECIMAL};
> 
> typedef unsigned short VARTYPE;
> 
> typedef struct tagINTRINSIC
> {
>     char * szType;
>     VARTYPE vt;
> } INTRINSIC;
> 
> INTRINSIC rgIntrinsic [] =
> {
>     "DATE",         VT_DATE,
>     "HRESULT",      VT_HRESULT,
>     "LPSTR",        VT_LPSTR,
>     "LPWSTR",       VT_LPWSTR,
>     "SCODE",        VT_ERROR,
>     "VARIANT_BOOL", VT_BOOL,
>     "wireBSTR",     VT_BSTR,
>     "BSTR",         VT_BSTR,
>     "VARIANT",      VT_VARIANT,
>     "wireVARIANT",  VT_VARIANT,
>     "CURRENCY",     VT_CY,
>     "CY",           VT_CY,
>     "DATE",         VT_DATE,
>     "DECIMAL",      VT_DECIMAL
> };
> 
> 
> 
> int main(int argc, char ** argv)
> {
> 
>   printf("sizeof(rgIntrinsic) = %d - MUST BE 112!!!\n",
> sizeof(rgIntrinsic));
> 
> 
>   printf("Number of occurencies: %d - ",
> sizeof(rgIntrinsic)/sizeof(INTRINSIC));
>   printf("MUST BE  14!!!\n");
> }

-- 
Carlo Wood <carlo@alinoe.com>



More information about the Gcc-bugs mailing list