sizeof(foo) -1 or sizeof(foo) 
Author Message
 sizeof(foo) -1 or sizeof(foo)

Hello.

    Could You please explain me why in that code snipped (marked as B)
sizeof(Error_Code)-1 is used wheras natural for
me is sizeof(Error_Code) like i.e.

memset(&errc0100, 0 , sizeof(errc0100));
errc0100.Bytes_provided = sizeof(errc0100);
------------------------

Here goes code snniped B mentioned above

typedef struct Qus_EC

      int  Bytes_Provided;
      int  Bytes_Available;
      char Exception_Id[7];
      char Reserved;
    /*char Exception_Data[];*/           /* Varying length        */
   } Qus_EC_t;

Qus_EC_t       *errstr;

char User_ID[11];              /* User ID                       */
   char static File[21];          /* File / Library name           */
   char Type[11];                 /* Object type                   */
   int  Call_Level = 0;           /* Call level                    */
   char Error_Code[201];          /* Error area from QUSROBJD      */

/* Set addressability to the ERROR template              */
    errstr = (Qus_EC_t *) &Error_Code[0];
    errstr->Bytes_Provided = sizeof(Error_Code)-1;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Is that because in B errstr is a pointer to Qus_EC_T wheras in A the case is
structuew ?
Regards.

    Grzegorz Goryszewski



Wed, 24 Nov 2004 17:52:43 GMT  
 sizeof(foo) -1 or sizeof(foo)

Quote:
> Hello.

>     Could You please explain me why in that code snipped (marked as B)
> sizeof(Error_Code)-1 is used wheras natural for
> me is sizeof(Error_Code) like i.e.

It's hard to tell from what you provided.  My guess is, whoever wrote the
code is subtracting 1 for the null terminator of a C string, but that
would imply the Error_Code is always 200 bytes (blank padded?).


Thu, 25 Nov 2004 00:41:51 GMT  
 sizeof(foo) -1 or sizeof(foo)
On Sat, 8 Jun 2002 11:52:43 +0200, "Grzegorz Goryszewski"

Quote:

>Hello.

>    Could You please explain me why in that code snipped (marked as B)
>sizeof(Error_Code)-1 is used wheras natural for
>me is sizeof(Error_Code) like i.e.

You didn't provide enough of the code to know for sure.  The real
answer is it depends on how the array and values are used.

One possible reason is that Error_Code is meant to hold a C string.
Since a string must be terminated with a '\0', the number of useable
bytes in the array is sizeof() - 1.

See other problems annotated below.

Quote:

>memset(&errc0100, 0 , sizeof(errc0100));
>errc0100.Bytes_provided = sizeof(errc0100);
>------------------------

>Here goes code snniped B mentioned above

>typedef struct Qus_EC

You are missing a { here.

Quote:

>      int  Bytes_Provided;
>      int  Bytes_Available;
>      char Exception_Id[7];
>      char Reserved;
>    /*char Exception_Data[];*/           /* Varying length        */
>   } Qus_EC_t;

Note that since the first member of the struct must start at the
beginning of the struct (there is no padding before the first member),
that any struct of this type must be properly aligned for an int.  On
many popular systems, this requires an address that is a multiple of
four.

Quote:

>Qus_EC_t       *errstr;

>char User_ID[11];              /* User ID                       */
>   char static File[21];          /* File / Library name           */
>   char Type[11];                 /* Object type                   */
>   int  Call_Level = 0;           /* Call level                    */
>   char Error_Code[201];          /* Error area from QUSROBJD      */

Note that arrays of char usually have different memory alignment
requirements than int.  On many systems, they have no alignment
requirements and can appear at any address.

Quote:

>/* Set addressability to the ERROR template              */
>    errstr = (Qus_EC_t *) &Error_Code[0];

Due to the two previous notes, this may invoke undefined behavior if
Error_Code is not properly aligned for an int.

Quote:
>    errstr->Bytes_Provided = sizeof(Error_Code)-1;

Any attempt to dereference errstr in a mode other than char* can yield
undefined behavior also.

Quote:
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>Is that because in B errstr is a pointer to Qus_EC_T wheras in A the case is
>structuew ?

What is A and what is structuew?

<<Remove the del for email>>



Thu, 25 Nov 2004 01:00:49 GMT  
 sizeof(foo) -1 or sizeof(foo)


Quote:
> On Sat, 8 Jun 2002 11:52:43 +0200, "Grzegorz Goryszewski"

> One possible reason is that Error_Code is meant to hold a C string.
> Since a string must be terminated with a '\0', the number of useable
> bytes in the array is sizeof() - 1.

And that is the clue. Thanks to all of You. Regards.

    Grzegorz Goryszewski

p.s.
Apologize for incomplete code.



Fri, 26 Nov 2004 19:23:37 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. sizeof(void*) versus sizeof(foo*)

2. question about malloc and sizeof *foo

3. makefile question - foo.c := DUMMY=foo

4. newbie question: foo.exe ->foo.c ??

5. difference between void foo(void) and void foo()

6. extern char *foo vs. extern char foo[]

7. How About #define foo foo

8. (foo == (foo_t *)NULL) vs. ((void *)foo == NULL)

9. relation between sizeof(void*), sizeof(int)

10. sizeof (struct) ! = sizeof struct elements

11. sizeof without sizeof

12. sizeof a/sizeof *a

 

 
Powered by phpBB® Forum Software