niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Questions on bitfields and struct
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
sarathy
*nix forums beginner


Joined: 17 Jul 2006
Posts: 12

PostPosted: Thu Jul 20, 2006 1:58 pm    Post subject: Questions on bitfields and struct Reply with quote

Hi,

1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3

# include <stdio.h>

struct struct1
{
const :16;
volatile :4;
};

struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};

int main()
{
printf ("Size of struct 1 = %d\n",sizeof(struct struct1)); /*
Prints 4 */
printf ("Size of struct 2 = %d\n",sizeof(struct struct2)); /*
Prints 3 */

}

2. Also what is meant by "incomplete type" in C?
3. What does tag refer to in a struct/union declaration?

Regards,
Sarathy
Back to top
Walter Roberson
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1300

PostPosted: Thu Jul 20, 2006 3:01 pm    Post subject: Re: Questions on bitfields and struct Reply with quote

In article <1153403911.312195.193670@p79g2000cwp.googlegroups.com>,
sarathy <sps.sarathy@gmail.com> wrote:

Quote:
1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3

struct struct1
{
const :16;
volatile :4;
};

struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};

With SGI's IRIX cc compiler, both are 3. With gcc 3.3 on the same
machine, the first is 4 and the second is 3. Which is to say that the
alignment mechanisms are implementation dependant, and are not required
to be consistant.

It is, for example, entirely legal for the compiler to examine the
first structure, note that the first field is the same size as a short
int on that implementation, and decide that the structure shall be
built for fast access as a pair of shorts aligned on a short boundary,
for a total of 4 bytes.

The same compiler could look at the second structure and decide that it
is complicated enough that space is the important factor rather than
speed, and decide to pull bits out of 3 bytes.

But compilers are not required to allow bitfields to cross word
boundaries, so the compiler -could- have decided to put the first two
fields into one short, the third field into a second short, and the
fourth field into a third short, for a total of 6 bytes. Or it could
have decided that since the total fits within 32 bits that it would
pull bits out of a long, for a total of 4 bytes.

Decisions about when to move to the next word and how much padding
to use before that word are completely up to the compiler: the
closest that the C standard comes on this point is to say "if it fits".


Quote:
2. Also what is meant by "incomplete type" in C?

The C faq probably talks about that in better words than I could
come up with in a reasonable time.


Quote:
3. What does tag refer to in a struct/union declaration?

A structure tag is a programmer-given name for that variety of
structure. It is not a variable, but rather a reference to the type.
As a rough analogy: "Form 37/J" might be a name given to a particular
layout of income tax form, but "Form 37/J" is not a particular -copy-
of the form, it is the name of the -kind- of form.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Back to top
sarathy
*nix forums beginner


Joined: 17 Jul 2006
Posts: 12

PostPosted: Thu Jul 20, 2006 6:30 pm    Post subject: Re: Questions on bitfields and struct Reply with quote

Walter Roberson wrote:
Quote:
In article <1153403911.312195.193670@p79g2000cwp.googlegroups.com>,
sarathy <sps.sarathy@gmail.com> wrote:

1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3

struct struct1
{
const :16;
volatile :4;
};

struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};

With SGI's IRIX cc compiler, both are 3. With gcc 3.3 on the same
machine, the first is 4 and the second is 3. Which is to say that the
alignment mechanisms are implementation dependant, and are not required
to be consistant.

It is, for example, entirely legal for the compiler to examine the
first structure, note that the first field is the same size as a short
int on that implementation, and decide that the structure shall be
built for fast access as a pair of shorts aligned on a short boundary,
for a total of 4 bytes.

The same compiler could look at the second structure and decide that it
is complicated enough that space is the important factor rather than
speed, and decide to pull bits out of 3 bytes.

But compilers are not required to allow bitfields to cross word
boundaries, so the compiler -could- have decided to put the first two
fields into one short, the third field into a second short, and the
fourth field into a third short, for a total of 6 bytes. Or it could
have decided that since the total fits within 32 bits that it would
pull bits out of a long, for a total of 4 bytes.

Decisions about when to move to the next word and how much padding
to use before that word are completely up to the compiler: the
closest that the C standard comes on this point is to say "if it fits".


2. Also what is meant by "incomplete type" in C?

The C faq probably talks about that in better words than I could
come up with in a reasonable time.


3. What does tag refer to in a struct/union declaration?

A structure tag is a programmer-given name for that variety of
structure. It is not a variable, but rather a reference to the type.
As a rough analogy: "Form 37/J" might be a name given to a particular
layout of income tax form, but "Form 37/J" is not a particular -copy-
of the form, it is the name of the -kind- of form.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest

Thanks a lot. I got the things clear now. The next fun starts...
Consider the code

struct struct1
{
short a1:16;
short b1:4;
};



struct struct2
{
int a2:16;
unsigned b2:1;
short c2 :16;
short d2 :4;
};



int main()
{
struct struct1 *str1=(struct struct1 *)malloc(sizeof(struct
struct1));
str1->a1=24;
str1->b1=4;
print_bits(str1,sizeof(str1));
printf ("Size of struct1 = %d\n",sizeof(struct struct1));
/* Prints 4 */
printf ("Size of str1 = %d\n",sizeof(str1)); /* Prints 4
*/



struct struct2 *str2=(struct struct2 *)malloc(sizeof(struct
struct2));
str2->a2=1;
str2->b2=1;
str2->c2=6;
str2->d2=12;
print_bits(str2,sizeof(struct struct2));
printf ("Size of struct2 = %d\n",sizeof(struct struct2));
/* Prints 8 */
printf ("Size of str2 = %d\n",sizeof(str2)); /* Prints 4
*/
}


Output:
----------

00000000 00000100 00000000 00011000
Size of struct1 = 4
Size of str1 = 4
00000000 00001100 00000000 00000110 00000000 00000001 00000000 00000001
Size of struct2 = 8
Size of str2 = 4

Is there any difference in sizeof (struct tag_name) and
sizeof(struct_instance_identifier)
In the third case, clearly from the results, 8 is correct due to
padding. But why is 4 being printed in the last printf.
Back to top
Walter Roberson
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1300

PostPosted: Thu Jul 20, 2006 9:52 pm    Post subject: Re: Questions on bitfields and struct Reply with quote

In article <1153420215.987105.86630@p79g2000cwp.googlegroups.com>,
sarathy <sps.sarathy@gmail.com> wrote:
Quote:

Walter Roberson wrote:

[lots]

Please trim down quotations to just the portion needed for discussion.


Quote:
Consider the code

struct struct2 *str2=(struct struct2 *)malloc(sizeof(struct
struct2));

printf ("Size of str2 = %d\n",sizeof(str2)); /* Prints 4
*/

Size of str2 = 4


Quote:
Is there any difference in sizeof (struct tag_name) and
sizeof(struct_instance_identifier)

No.


Quote:
In the third case, clearly from the results, 8 is correct due to
padding. But why is 4 being printed in the last printf.

Because str2 is not an instance of struct struct2.
str2 is a *pointer* to an instance of struct struct2.
sizeof(str2) is printing out the size of the pointer.

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Mon Dec 01, 2008 8:07 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Net:TFTPd questions - Want to upload configs via TFTP Adam Perl 0 Thu Jul 20, 2006 4:59 pm
No new posts Net:TFTPd questions - Want to upload configs via TFTP Adam modules 1 Thu Jul 20, 2006 4:29 pm
No new posts Question on bitfields & structs sarathy C 1 Thu Jul 20, 2006 12:18 pm
No new posts SLED 10 Questions Moif Murphy Suse 2 Thu Jul 20, 2006 12:11 pm
No new posts questions to anyone who uses wxPython nuno python 2 Thu Jul 20, 2006 4:12 am

Low Interest Credit Card | Internet Advertising | Mortgage Calculator | Watch Gossip Girl Online | Gas Suppliers
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2579s ][ Queries: 16 (0.1558s) ][ GZIP on - Debug on ]