473,399 Members | 3,401 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,399 software developers and data experts.

Alignment of struct members


Hit a super weird bug, which baffled me for some time. Here's the
setup:

struct thing {
unsigned char byte1;
unsigned char byte2;
unsigned char byte3;
unsigned long long big_number;
char* string_ptr;
};

void set_default_values(struct thing* t)
{
t->byte1 = 0;
t->byte2 = 1;
t->byte3 = 2;
t->big_number = 0;
t->string_ptr = strdup("This is a string.");
}

Ok? What am I doing wrong?

After calling set_default values, my struct is:

byte1 = 0, byte2 = 1, byte3 = 2, big_number =
157823974298375928572234, string_ptr = "his is a string."

The big_number there is not exact. I can try to work up a functioning
microprogram if this is at all worth pursuing.

Eventually, after the byte3 member, I added an 'unsigned char unusued'
member, and my problems went away. My understanding was that the
compiler was supposed to align bytes and optionally pad if required by
the os. Is my understanding wrong? I'm using an ordinary gdb on an
ordinary linux. This *can't* be a broken compiler problem, can it? (I
realize it's almost certainly a broken programmer problem; and I
thought I'd call for smacks on the head before pursuing any further.)

-Bluejack

May 28 '07 #1
6 1930
bluejack <bl******@gmail.comwrites:
Hit a super weird bug, which baffled me for some time. Here's the
setup:

struct thing {
unsigned char byte1;
unsigned char byte2;
unsigned char byte3;
unsigned long long big_number;
char* string_ptr;
};

void set_default_values(struct thing* t)
{
t->byte1 = 0;
t->byte2 = 1;
t->byte3 = 2;
t->big_number = 0;
t->string_ptr = strdup("This is a string.");
}

Ok? What am I doing wrong?

After calling set_default values, my struct is:

byte1 = 0, byte2 = 1, byte3 = 2, big_number =
157823974298375928572234, string_ptr = "his is a string."

The big_number there is not exact. I can try to work up a functioning
microprogram if this is at all worth pursuing.

Eventually, after the byte3 member, I added an 'unsigned char unusued'
member, and my problems went away. My understanding was that the
compiler was supposed to align bytes and optionally pad if required by
the os. Is my understanding wrong? I'm using an ordinary gdb on an
ordinary linux. This *can't* be a broken compiler problem, can it? (I
realize it's almost certainly a broken programmer problem; and I
thought I'd call for smacks on the head before pursuing any further.)
It's impossible to be sure without seeing a working complete program.

You mention gdb (a debugger). Is that what you're using to display
the value of the struct? If so, that makes it hard to tell whether
it's a compiler bug or a debugger bug.

See if you can reproduce the problem with a small program that prints
the values itself rather than depending on the debugger.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 28 '07 #2
On May 28, 1:30 am, Keith Thompson <k...@mib.orgwrote:
You mention gdb (a debugger). Is that what you're using to display
the value of the struct? If so, that makes it hard to tell whether
it's a compiler bug or a debugger bug.

See if you can reproduce the problem with a small program that prints
the values itself rather than depending on the debugger.
Yeah, that cleared it up for me. It was a display problem with the
debugger. :/

I encounter problems with my tools so rarely I guess I trust them a
little too much.

There is source for a more recent version of the debugger available;
and I expect that bug will have been corrected. Thanks again.

-Bluejack

May 28 '07 #3
bluejack wrote:
>
Hit a super weird bug, which baffled me for some time. Here's the
setup:

struct thing {
unsigned char byte1;
unsigned char byte2;
unsigned char byte3;
unsigned long long big_number;
char* string_ptr;
};

void set_default_values(struct thing* t)
{
t->byte1 = 0;
t->byte2 = 1;
t->byte3 = 2;
t->big_number = 0;
t->string_ptr = strdup("This is a string.");
}

Ok? What am I doing wrong?
You didn't show a complete compilable program, in particular you
omitted the call to set_default_values, the declaration and
initialization of t, etc.

My guess is that you never allocated data space for t.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 28 '07 #4
bluejack wrote:
Hit a super weird bug, which baffled me for some time. Here's the
setup:

struct thing {
unsigned char byte1;
unsigned char byte2;
unsigned char byte3;
unsigned long long big_number;
char* string_ptr;
};

void set_default_values(struct thing* t)
{
t->byte1 = 0;
t->byte2 = 1;
t->byte3 = 2;
t->big_number = 0;
t->string_ptr = strdup("This is a string.");
}

Ok? What am I doing wrong?
strdup()is non-standard.

How do you print the 'big-number'? Remember, this member has 'long long'
type, which is not supported in C89.

--
Tor <torust [at] online [dot] no>
May 28 '07 #5
On May 28, 4:23 am, CBFalconer <cbfalco...@yahoo.comwrote:
You didn't show a complete compilable program, in particular you
omitted the call to set_default_values, the declaration and
initialization of t, etc.
I know, I was looking for conceptual guidance; which Keith provided.
My guess is that you never allocated data space for t.
Looked like a memory smash to me, too, and I actually spent some time
looking at that; but it turned out to be a display issue in an older
(but "current" according to yum) version of gdb.

The actual bug that surfaced in execution turned out to be
elsewhere... I was pursuing a red herring because it *looked* so
wrong.

Lesson learned: don't always trust your tools; but be careful about
which tool you blame. (I had been thinking it might be a compiler bug,
although I was skeptical, because in my experience it's *never* a
compiler bug; which was why I posted here...)

-b

May 28 '07 #6
bluejack <bl******@gmail.comwrites:
On May 28, 4:23 am, CBFalconer <cbfalco...@yahoo.comwrote:
>You didn't show a complete compilable program, in particular you
omitted the call to set_default_values, the declaration and
initialization of t, etc.

I know, I was looking for conceptual guidance; which Keith provided.
>My guess is that you never allocated data space for t.

Looked like a memory smash to me, too, and I actually spent some time
looking at that; but it turned out to be a display issue in an older
(but "current" according to yum) version of gdb.

The actual bug that surfaced in execution turned out to be
elsewhere... I was pursuing a red herring because it *looked* so
wrong.

Lesson learned: don't always trust your tools; but be careful about
which tool you blame. (I had been thinking it might be a compiler bug,
although I was skeptical, because in my experience it's *never* a
compiler bug; which was why I posted here...)
Another lesson: try narrowing your program down to a small
self-contained program that exhibits the problem. In your case, you
could have wrapped the declarations you posted with another dozen or
so lines of code, creating a small program that calls your
initialization routine and prints the values of the members of the
structure using printf. This is especially useful if your debugger
isn't being as helpful as it should. If the problem is in your code,
you're likely to figure it out while narrowing it down. If you still
don't know where the problem is, you can post the code here; if it's
portable C, we can probably let you know whether the code is behaving
as it should according to the standard.

<OT>Since it apparently turned out to have been a gdb bug, do a Google
search, or check the change log in the latest gdb release, to find out
whether it's been fixed, and in what release.</OT>

There's another possibility that I should have mentioned. You're
using long long, which is a new feature of C99. C99 conformance is
incomplete in most implementations. Since the compiler and the
runtime library are often provided separately, it's entirely possible
for the compiler to support long long, but for printf not to support
the "%lld" format. In this case, you can convert the value to long if
it happens to be in range, or roll your own conversion routine. (That
didn't turn out to be the problem in your case, but others might find
this helpful.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 28 '07 #7

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
5
by: Clint Olsen | last post by:
I did a Google about this, and I've seen this covered quite a few times - some leading to useful tricks with offsetof() to discern alignment and others using a big-union of intrinsic types to...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
5
by: MeJohn | last post by:
Hello, I have a question about the layout of the fields in a structure. I have read in a paper that if two structures contain an initial sequence of fields, all of which have compatible types,...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
9
by: A n g l e r | last post by:
Hi all. I've got the following code as a part of managed C++ library that is loaded by a project in C#: public ref class FlyCaptureEnumsAndStructsManaged { public: typedef enum class...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
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: 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...
0
marktang
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,...
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
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,...
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...
0
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...
0
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,...

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.