vendredi 7 mars 2014

Padding involved topic




On 03/07/2014 04:13 PM, anish singh wrote:
> Struct abbcd{
> Char c;
> Int b;
> Short d;
> };


The keywords "struct", "char", "int" and "short" are all lower case.
This may seem like quibbling, but C is a case sensitive language - if
you plan to use C, you need to learn to be careful about case.

> What will be the size of abbcd ? If padding involved and without padding?Suppose that the processor has only 4 byte registers.
>
> What will be the size if the particular processor has register for 2 byte, 4 byte and 1 byte?
>
> Note; size of char is 1,size of int, short is 4 and 2 respectively.


The only answer that works across all implementations is "sizeof(struct
abbcd)".

Without padding, the size will be 7 bytes, though it depends upon the
implementation whether you even have the option of avoiding padding.
With padding, it will be larger than 7, and almost certainly smaller
than SIZE_MAX. The actual value within that range depends upon the
implementation. If you want a more specific answer, the information
you've provided is insufficient to answer it. You need to fully specify
which implementation of C you're using: identify which compiler you're
using, and what compiler options you've chosen, including the target
platform. Once you've specified those things, the easiest way to find
out is to print out the value of sizeof(struct abbcd). That's a lot
quicker than asking us.






Aucun commentaire:

Enregistrer un commentaire