473,761 Members | 5,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structure Query....

typdef struct
{
int i;
char ch;
}str;
str str_var;
char x, y;
main()
{
//do nothing
}

In the above piece of code the structure needs a padding of 3 more
bytes (the total size of the structure variable being 8 bytes). Now
my question is, wont the compiler append x & y to the end of structure
and adds just 1 byte as the padding?
In my case I use MSVC compiler. When I checked the size of obj in the
map file the obj size was 12 bytes....should nt it be 8 bytes ideally??

Thanks in advance...
Regds
Sachin
Nov 14 '05 #1
2 1855
Sachin <sa********@gma il.com> wrote:
typdef struct
{
int i;
char ch;
}str;
str str_var;
char x, y;
main()
int main( void )
{
//do nothing
} In the above piece of code the structure needs a padding of 3 more
bytes (the total size of the structure variable being 8 bytes). Now
my question is, wont the compiler append x & y to the end of structure
and adds just 1 byte as the padding?
First of all, the compiler is entitled to add as much padding as
it wants, so you should never rely on a certain value. Second,
you can't know if the compiler will put x and y at a certain place
in relation to the structure, it could put the before or after the
structure or somewhere completely different. Third, even if both
these variable would come directly after the structure in memory
the structure would still have its full padding - if different
padding would be used dependening on circumstances the value of
'sizeof( struct str )' wouldn't be welldefined. And imagine what
would happen then if you define another such structure and do

memcpy( &str_var, &another_str_st ruct, sizeof( struct str ) );
In my case I use MSVC compiler. When I checked the size of obj in the
map file the obj size was 12 bytes....should nt it be 8 bytes ideally??


No idea what "size of obj in the map file" is, but you can simply
get the size with the sizeof operator. But the compiler is still
free to add as much padding as it likes to (or thinks necessary).
If that's a different value from what you think is necessary you
have to discuss that with the people that wrote the compiler, it
is nothing the C standard specifies.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
sa********@gmai l.com (Sachin) writes:
typdef struct
{
int i;
char ch;
}str;
str str_var;
char x, y;
main()
{
//do nothing
}

In the above piece of code the structure needs a padding of 3 more
bytes (the total size of the structure variable being 8 bytes). Now
my question is, wont the compiler append x & y to the end of structure
and adds just 1 byte as the padding?


No. Then the following:
memset (&str_var, 0, sizeof str_var);
would clear x and y as well as str_var.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 14 '05 #3

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

Similar topics

2
10493
by: Mike | last post by:
Hi all, I am writing a database using SQL as the back end and Access as the front end. My project is to create a rota for when people have to work. The problem I have is setting up the tables so I can query them correctly. I currently have a staff table which holds the staff_id and staff_name fields. I have a worktime table that has the staff_id, date, start time, end time as fields. What I would like to do able to do is have a
3
1887
by: jaf893 | last post by:
I have a series of test results with each result consisiting of 10 test conditions, 5 location conditions and then 3 test results. The table looks a bit like this: (T = test condition, L = location condition, R = test results) T1 T2 .. T10 L1 L2 .. L5 R1 R2 R3 xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
1
1258
by: jim Bob | last post by:
Hi, I can't seem to get my head around a simple DB structure problem... Currently I have DBID LastName FirstName DateOfInterview SitePreference1ID ->SiteTable SitePreference2ID ->SiteTable
0
1599
by: Christoph Haas | last post by:
Hi, list... I have written an application in Perl some time ago (I was young and needed the money) that parses multiple large text files containing nested data structures and allows the user to run quick queries on the data. (For the firewall admins among you: it's a parser and web-based query tool for CheckPoint firewall rulebases. The user can search for source and destination IPs and get the matching rules.) The current application...
4
1422
by: gordon | last post by:
Hi I am still fairly new to C#.net and I sometimes make basic program design mistakes - particularyly in the context of paying attention to OOP principles. At the moment I am working on an application that uses a MS Access database and runs some queries that return output to a datagrid. I would like to consider the best structure of the program so that I can start to get into good habits for other similar projects.
0
2553
by: abhishekjethwani | last post by:
How to write a query to access tree structure form multi table. I m having five tables from them first table give me a data which act as the parameter for query for the second table and the two tables will give me parameter for the third and so on. at present i m writing five query and sending parameter to each query but i want an efficient way for this. its a parent child type relation.
24
2122
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
7
1800
by: erikcw | last post by:
Hi, I'm working on a web application where each user will be creating several "projects" in there account, each with 1,000-50,000 objects. Each object will consist of a unique name, an id, and some meta data. The number of objects will grow and shrink as the user works with their project. I'm trying to decided whether to store the objects in the database
8
1801
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from a slow distant site). - But you want to be able to request data from it, such has "give me all nodes that are under a "//foo/bar" tree, and have a child with an "baz" attribute of value "zzz". Question :
1
4476
by: sibani sahu | last post by:
Hi, I want to generate the organisational structure of my company.I have a table /viewwhich has the emp_id,manager_id and the level_num.The level_num represents the designation level of the employee.So I used a query as:_ select concat(REPEAT('-',a.level_num *5),a.employee_name) as Org_Chart from ALL_EMPLOYEE_VIEW A order BY a.level_num ; This does not create a tree structure but simply arranges the employees according to their...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.