C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 20th, 2013, 04:47 AM
ghost_rider ghost_rider is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 ghost_rider User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 5 sec
Reputation Power: 0
Bit fields

the foll code when run on gcc compiler gave 12 as output


Code:
struct aa{char a:3;int b:30;char c:3;}; printf("%d",sizeof(struct aa));

but since sizeof int is 4 and char is 1 then why does it give 12 instead of 8 as output????

Reply With Quote
  #2  
Old March 20th, 2013, 06:44 AM
eramit2010 eramit2010 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Location: India
Posts: 23 eramit2010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 26 m 13 sec
Reputation Power: 0
it because of structure padding.
compiler use memory in the pack of 4 bytes.
thats why its giving u output as 12 byte.

to get 6 byte as output use

Code:
#pragma pack(1)


so compiler will not use structure padding.

Reply With Quote
  #3  
Old March 20th, 2013, 10:07 AM
ghost_rider ghost_rider is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 ghost_rider User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by eramit2010
to get 6 byte as output use



I think it should be 5 not six 4 for int and 1 for char

Reply With Quote
  #4  
Old March 20th, 2013, 02:26 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 18 h 45 m 47 sec
Reputation Power: 1774
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Bit fields

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap