473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem in structure

hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc
if any one find the answer could you explain how it is happening

Dec 6 '06 #1
4 1125
RAKHE wrote:
hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc
if any one find the answer could you explain how it is happening
Probably due to padding. See:
<http://c-faq.com/struct/padding.html>
<http://c-faq.com/struct/align.esr.html>

Dec 6 '06 #2
RAKHE wrote:
>
hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc
Because sizeof(struct name) _is_ 8.
if any one find the answer could you explain how it is happening
It's called "padding" and it's in the FAQ somewhere. I'm sure
someone will post the section shortly.

Basically, your system probably requires (or at least "prefers") to
have shorts on even byte boundaries (and, perhaps, longs on 4-byte
boundaries). Therefore, the compiler adds a byte of padding between
n2 and n1. Use the offsetof() macro to demonstrate this, by printing
the offset of n2, n1, and n3.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Dec 6 '06 #3

santosh wrote:
RAKHE wrote:
hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc
if any one find the answer could you explain how it is happening

Probably due to padding. See:
<http://c-faq.com/struct/padding.html>
<http://c-faq.com/struct/align.esr.html>
thank u santhosh for ur kind information

Dec 11 '06 #4
RAKHE wrote:
santosh wrote:
.... snip ...
>>
Probably due to padding. See:
<http://c-faq.com/struct/padding.html>
<http://c-faq.com/struct/align.esr.html>

thank u santhosh for ur kind information
I don't believe santoshs initial is 'u', nor that he resides in ur,
which is an ancient city in Mesopotamia. In other words, don't use
childish abbreviations in Usenet.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Leszek Dubiel | last post by:
----------------------------------------- BACKGROUND In my company (www.glass.biz) we use ERP software to compute what has to be done to do products for our customers. Main algorithm takes data...
3
by: Muhammad Farooq-i-Azam | last post by:
Hi, I am trying to define an arp structure but having problem doing so. I think I have define the correct arp structure but I find myself in a strange problem. The size of structure that I have...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
2
by: Sakharam Phapale | last post by:
Hi All, Yestrday I had problem regarding WAVEFORMAT structure in WaveInOpen API function call. Following structure works properly. But next to that didn't. Can anybody tell me why should I used...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
1
by: John | last post by:
Hi I have an array of structure as below; Structure Section Structure MasterTable Dim Name As String End Structure End Structure
0
by: magickarle | last post by:
Hi to all. Little introduction on what I'm trying to do: I'm in charge of creating an access database for our supervisor where they can grade their agents. Per ex: agents are talking to customers...
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.