Hi everyone,
I'm a C newbie, so please be gentle. I have a program that defines the
following things:
typedef union
{
unsigned int I;
unsigned char b[4];
} dword;
/* Structure for record 0 of the DAWG. */
typedef struct
{
unsigned char magic[5];
unsigned char name[11];
unsigned char title[41];
unsigned char desc[41];
unsigned char author[41];
unsigned char extra[41];
unsigned char ch[64];
unsigned char numchars;
unsigned char catsym[8];
unsigned char catname[8][11];
unsigned char catinclude[8][8];
unsigned char numcategories;
dword numnodes;
} dawghdr;
I expect the size of the dawghdr structure to be 410 bytes. However,
on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412
bytes. I've examined the memory space of this structure, and it seems
to me that 2 extra bytes appear between the numcategories and numnodes
members. Could anyone be kind enough to explain why this might be
happening?
The program writes this structure to a disk file as a record, and I
later read this output file with a python script, but the script falls
over, as it fails to read the correct value of numnodes, since it has
no way of knowing about the 2 extra bytes. 18 1811
Anand Buddhdev <ar*@anand.or g> wrote: I expect the size of the dawghdr structure to be 410 bytes. However, on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412 bytes. I've examined the memory space of this structure, and it seems to me that 2 extra bytes appear between the numcategories and numnodes members. Could anyone be kind enough to explain why this might be happening?
The compiler is allowed to put padding between the elements.
This question is coverd in the faq: http://www.eskimo.com/~scs/C-faq/q2.13.html
The program writes this structure to a disk file as a record, and I later read this output file with a python script, but the script falls over, as it fails to read the correct value of numnodes, since it has no way of knowing about the 2 extra bytes.
This "solution" is highly compiler dependent. You should write an own
method. And don't forget to take care of endianess.
Ciao Chriss
"Anand Buddhdev" <ar*@anand.or g> wrote in message
news:7b******** *************** ***@posting.goo gle.com... Hi everyone,
I'm a C newbie, so please be gentle. I have a program that defines the following things:
typedef union { unsigned int I; unsigned char b[4]; } dword;
/* Structure for record 0 of the DAWG. */ typedef struct { unsigned char magic[5]; unsigned char name[11]; unsigned char title[41]; unsigned char desc[41]; unsigned char author[41]; unsigned char extra[41]; unsigned char ch[64]; unsigned char numchars; unsigned char catsym[8]; unsigned char catname[8][11]; unsigned char catinclude[8][8]; unsigned char numcategories; dword numnodes; } dawghdr;
I expect the size of the dawghdr structure to be 410 bytes. However, on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412 bytes. I've examined the memory space of this structure, and it seems to me that 2 extra bytes appear between the numcategories and numnodes members. Could anyone be kind enough to explain why this might be happening?
Conpiler padding. Check your compiler's documentation
The program writes this structure to a disk file as a record, and I later read this output file with a python script, but the script falls over, as it fails to read the correct value of numnodes, since it has no way of knowing about the 2 extra bytes.
Try writing(and reading) the structure element by element. Using that way
you don't care about padding.
Anand Buddhdev wrote:
.... I expect the size of the dawghdr structure to be 410 bytes. However, on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412 bytes.
....
For crying out loud, doesn't *anyone* check the FAQ before posting?
Martin Ambuhl <ma*****@earthl ink.net> wrote: For crying out loud, doesn't *anyone* check the FAQ before posting?
I did before I replied. :)
Ciao Chriss
Anand Buddhdev said the following, on 07/21/04 07:19: Hi everyone,
I'm a C newbie, so please be gentle. I have a program that defines the following things:
typedef union { unsigned int I; unsigned char b[4]; } dword;
/* Structure for record 0 of the DAWG. */ typedef struct { unsigned char magic[5]; unsigned char name[11]; unsigned char title[41]; unsigned char desc[41]; unsigned char author[41]; unsigned char extra[41]; unsigned char ch[64]; unsigned char numchars; unsigned char catsym[8]; unsigned char catname[8][11]; unsigned char catinclude[8][8]; unsigned char numcategories; dword numnodes; } dawghdr;
I expect the size of the dawghdr structure to be 410 bytes. However, on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412 bytes. I've examined the memory space of this structure, and it seems to me that 2 extra bytes appear between the numcategories and numnodes members. Could anyone be kind enough to explain why this might be happening?
The program writes this structure to a disk file as a record, and I later read this output file with a python script, but the script falls over, as it fails to read the correct value of numnodes, since it has no way of knowing about the 2 extra bytes.
The compiler is allowed to insert padding bytes between elements of a
structure, in order to meet alignment restrictions of the
implementation. This is covered in the FAQ: http://www.eskimo.com/~scs/C-faq/
The solution to your specific problem is going to be
implementation-dependent.
[OT]
Sometimes you can avoid the requirement for padding by placing the
structure elements in descending order of length. YMMV.
--
Rich Gibbs rg****@alumni.p rinceton.edu
Darksun4 <da*******@yaho o.gr> spoke thus: Conpiler padding
That would be structure padding, unless I'm not familiar with the
latest in plush compilers.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
>I expect the size of the dawghdr structure to be 410 bytes. However,
Well, stop expecting that. The size of the structure is what the
compiler decides it will be (sizeof(struct dawghdr), not any
particular fixed integer). Including padding, if it feels like it.
Gordon L. Burditt
Martin Ambuhl <ma*****@earthl ink.net> wrote: Anand Buddhdev wrote:
... I expect the size of the dawghdr structure to be 410 bytes. However, on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412 bytes. ...
For crying out loud, doesn't *anyone* check the FAQ before posting?
It's hard to find a specific issue in the online FAQ, except for the
small subset of issues which are easy to find, and also difficult to
find it in the flat text file if it isn't something you can easily
search for. The only time I've gotten something useful out of it was
when I read it from start to finish. Even then, I only remembered
things because I knew most of it already; for someone new to C
programming it may well be in one ear and out the other.
The HTML FAQ would be much more useful if it:
- had a single page with a hyperlinked summary-line of each entry
- had a text search facility
- was updated (I've heard the HTML ver is lagging behind the text ver)
- had an introductory page with the 20 or so most commonly asked
questions (in the format of all the hyperlinked summary lines at
the top, and all the answers at the bottom). It's a FAQ, not an EAQ
(Ever Asked Questions). Most of its entries are things that haven't
come up for years. It's annoying to wade through all these things.
(Note: this tirade was aimed at explaining why people ask FAQ questions
so often, it wasn't an attack on the FAQ maintainer).
Martin Ambuhl wrote: Anand Buddhdev wrote: I expect the size of the dawghdr structure to be 410 bytes. However, on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412 bytes. For crying out loud, doesn't *anyone* check the FAQ before posting?
well, clearly not you either mr troll. and i quote:
"This is a large and heavy document, so don't assume that
everyone on the net has managed to read all of it in
detail, and please don't roll it up and thwack people
over the head with it just because they missed their
answer in it." ftp://ftp.eskimo.com/u/s/scs/C-faq/faq.gz This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Zero |
last post by:
If we have a structure like:
struct something{
int *a;
int b;
};
We allocate mempry for a using malloc or calloc. The question is when
we want to know the size of the structure, sizeof(struct something),
it will not give us the correct number (i.e. the size of the structure
|
by: Sean |
last post by:
I have a struct that I wrote to test a protocol. The idea I had was
to just declare the elements of the struct in the order in which they
are sent and received as defined by the protocol. However, writing
this struct to a file produces unexpected results.
Here is a test struct I wrote:
struct Tester {
unsigned short first;
unsigned int second;
|
by: thushianthan15 |
last post by:
Greetings!!
Currently i am learning C from K&R.I have DOS & Linux in my
system.When i used sizeof() keyword to compute the size of integer , it
shows different results in DOS and Linux.In DOS it shows integer
occupies 2 bytes , but in Linux it shows 4 bytes.Is sizeof operator
"really" shows the machine data or register size ?
Please help
me!!
|
by: SB |
last post by:
I feel dumb to ask because I bet this is a simple question...
Looking at the code below, can someone please explain why I get two
different values in my Marshal.SizeOf calls (see the commented lines)?
TIA!
sb
|
by: CptDondo |
last post by:
I am missing something about structure declarations....
I am trying to get the size of a structure member using sizeof.
my xml.h file (beware of line wrap):
struct fieldSchedule_t {
uint8_t action;
uint16_t fromBearing, toBearing;
};
| |
by: Chameleon |
last post by:
I have a TGA image header struct.
TGA has 18 bytes header, so the C struct too.
why this return 20?
sizeof(TGAHeader)
I saw this in many structs. I believe compiler round up the size to 4
multiple.
|
by: Banfa |
last post by:
I thought I would write a little article about this because it is regularly asked as a question in the Forums.
Firstly this is a C++ issue, it is strictly against the rules of C to create a class with no members. This makes sense because the only real use for a structure or class with no data members and virtual functions is as the base from which to derive other classes and structures or as a container for non-virtual methods.
The reason...
|
by: Boltar |
last post by:
Hi
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
#include <stdio.h>
struct foo
{
};
|
by: Charming12 |
last post by:
Hi All,
I am using System.Runtime.InteropServices; to marshal a structure using Marshal.structureToPtr().
But to get the size of structure when i get Marshal.sizeOf(), it gives me improper sizes.
Code:
public struct IDName
{
public ushort Id;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |