Hi NG,
Does the following code invoke undefined behaviour? I compiled it and it
runs fine. I think this is fine because I think the last 3 arguments of
struct a are organised in memory the same way as the 3 arguments of
struct b. But if my previous statement isn't true in all cases or on all
platforms or with all compilers, this probably will invoke undefined
behaviour.
Can anyone tell me if I'm right or not?
Thanks in advance,
Mark
#include <stdio.h>
struct a
{
int a;
int b;
char *foo;
long int c;
};
struct b
{
int b;
char *foo;
long int c;
};
int main(void)
{
struct a foo;
struct b *bar;
foo.a = 1;
foo.b = 2;
foo.c = 3;
bar = (struct b*)&foo.b;
bar->b *= bar->c;
bar->foo = "This is a test\n";
printf("foo.a: %d\nfoo.b: %d\nfoo.c: %ld\nfoo.foo: %s\n", foo.a,
foo.b, foo.c, foo.foo);
return 0;
}
--
<<Remove the del for email>> 7 3019
Capstar <sp***@eg.homei p.net> wrote: Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b.
You think wrong. The padding bytes need not be the same.
struct a { int a; int b; char *foo; long int c; };
struct b { int b; char *foo; long int c; };
For example, they can be layed out like this:
struct a
int a: 2 bytes
int b: 2 bytes
char *foo: 4 bytes
long int c: 4 bytes
struct b:
int b: 2 bytes
padding: 2 bytes /* oops... */
char *foo: 4 bytes
long int c: 4 bytes
Richard
Richard Bos wrote: Capstar <sp***@eg.homei p.net> wrote:
Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b.
You think wrong. The padding bytes need not be the same.
struct a { int a; int b; char *foo; long int c; };
struct b { int b; char *foo; long int c; };
For example, they can be layed out like this:
struct a int a: 2 bytes int b: 2 bytes char *foo: 4 bytes long int c: 4 bytes
struct b: int b: 2 bytes padding: 2 bytes /* oops... */ char *foo: 4 bytes long int c: 4 bytes
Richard
Ah right, didn't think of that.
Thanks
Mark
--
<<Remove the del for email>>
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40******** ********@news.i ndividual.net.. . Capstar <sp***@eg.homei p.net> wrote:
Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b.
You think wrong. The padding bytes need not be the same.
struct a { int a; int b; char *foo; long int c; };
struct b { int b; char *foo; long int c; };
For example, they can be layed out like this:
struct a int a: 2 bytes int b: 2 bytes char *foo: 4 bytes long int c: 4 bytes
struct b: int b: 2 bytes padding: 2 bytes /* oops... */ char *foo: 4 bytes long int c: 4 bytes
Although, an evil spirit inside of me begs to ask, what if the program
checks...
offsetof(a,foo) - offsetof(a,b) == offsetof(b,foo) - offsetof(b,b)
....etc?
I think it's ruled out in that only a pointer to the _first_ member of a
struct can be converted to the corresponding struct pointer meaningfully.
[Possibly alignment of struct objects too?!]
Thoughts anyone?
--
Peter
"Capstar" <sp***@eg.homei p.net> wrote in message
news:bv******** **@news.tudelft .nl... Hi NG,
Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b. But if my previous statement isn't true in all cases or on all platforms or with all compilers, this probably will invoke undefined behaviour.
Can anyone tell me if I'm right or not?
Thanks in advance, Mark #include <stdio.h>
struct a { int a; int b; char *foo; long int c; };
struct b { int b; char *foo; long int c; };
int main(void) { struct a foo; struct b *bar;
foo.a = 1; foo.b = 2; foo.c = 3;
bar = (struct b*)&foo.b;
bar->b *= bar->c; bar->foo = "This is a test\n";
printf("foo.a: %d\nfoo.b: %d\nfoo.c: %ld\nfoo.foo: %s\n", foo.a, foo.b, foo.c, foo.foo);
return 0; }
As others have indicated, alignment issues will
arise when you merely copy the field definitions to
the containing structure. The only way to get it
absolutely right is to embed the "b" struct within
the "a" struct.
=============== ======
struct b
{
int b;
char *foo;
long int c;
};
struct a
{
int a;
struct b b;
};
=============== ======
The embedded "b" struct will be aligned
correctly according to the strictest (largest)
alignment requirement of any of its members.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906 http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
On Tue, 03 Feb 2004 14:27:03 +0100, Capstar <sp***@eg.homei p.net>
wrote in comp.lang.c: Hi NG,
Does the following code invoke undefined behaviour? I compiled it and it
Yes.
runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b. But if my previous statement isn't true in all cases or on all platforms or with all compilers, this probably will invoke undefined behaviour.
Can anyone tell me if I'm right or not?
Thanks in advance, Mark #include <stdio.h>
struct a { int a; int b; char *foo; long int c; };
struct b { int b; char *foo; long int c; };
int main(void) { struct a foo; struct b *bar;
foo.a = 1; foo.b = 2; foo.c = 3;
bar = (struct b*)&foo.b;
bar->b *= bar->c; bar->foo = "This is a test\n";
printf("foo.a: %d\nfoo.b: %d\nfoo.c: %ld\nfoo.foo: %s\n", foo.a, foo.b, foo.c, foo.foo);
return 0; }
The standard makes a specific guarantee about structure types having
an _initial_ set of members of both the same types and the same names.
The common initial set of members may be accessed via an lvalue of
either type.
There is very specifically no such guarantee about any other alignment
of members, and as Richard already correctly pointed out there a
definite possibility of alignment issues.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
On Wed, 4 Feb 2004 01:51:48 +1100, "Peter Nilsson" <ai***@acay.com .au>
wrote in comp.lang.c: "Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message news:40******** ********@news.i ndividual.net.. . Capstar <sp***@eg.homei p.net> wrote:
Does the following code invoke undefined behaviour? I compiled it and it runs fine. I think this is fine because I think the last 3 arguments of struct a are organised in memory the same way as the 3 arguments of struct b.
You think wrong. The padding bytes need not be the same.
struct a { int a; int b; char *foo; long int c; };
struct b { int b; char *foo; long int c; };
For example, they can be layed out like this:
struct a int a: 2 bytes int b: 2 bytes char *foo: 4 bytes long int c: 4 bytes
struct b: int b: 2 bytes padding: 2 bytes /* oops... */ char *foo: 4 bytes long int c: 4 bytes
Although, an evil spirit inside of me begs to ask, what if the program checks...
offsetof(a,foo) - offsetof(a,b) == offsetof(b,foo) - offsetof(b,b)
...etc?
I think it's ruled out in that only a pointer to the _first_ member of a struct can be converted to the corresponding struct pointer meaningfully. [Possibly alignment of struct objects too?!]
Thoughts anyone?
That just means it might work the way the OP wants it to, at least
some of the time, on such a compiler. Or it might not. Either way,
it does not make the behavior any less undefined as far as the
standard is concerned.
Since the standard specifically defines the one and only case in which
structures may be "aliased", all other possible cases generate
undefined behavior.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
On Wed, 04 Feb 2004 03:02:06 GMT, Jack Klein <ja*******@spam cop.net>
wrote: On Tue, 03 Feb 2004 14:27:03 +0100, Capstar <sp***@eg.homei p.net> wrote in comp.lang.c:
(structs with similar members but not starting at beginning)
The standard makes a specific guarantee about structure types having an _initial_ set of members of both the same types and the same names.
Not quite; 6.5.2.3p5 makes the guarantee about initial members with
the same types (and for bitfields widths), with no requirement about
names, *if* they occur in a union. Since an identically declared
struct must be compatible across t.u.s and might occur in a union in
another t.u. even if it doesn't in this one, in practice you can rely
on structs whose initial members have the same types (and widths).
It may be desirable to use the same names if you intend to store and
access the "same" data, but that's a different question.
It is compatibility of struct or union types *between t.u.s* in 6.2.7
that also requires same member names (and tags). Although I see no
good reason the names should matter, and have never seen them do so.
The common initial set of members may be accessed via an lvalue of either type.
There is very specifically no such guarantee about any other alignment of members, and as Richard already correctly pointed out there a definite possibility of alignment issues.
Concur.
- David.Thompson1 at worldnet.att.ne t This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Panchal V |
last post by:
I want to access a variable length record in C, the format is as
follows :
+---+---+-----------+
| A | L | D A T A |
+---+---+-----------+
A - Some Data (1 BYTE)
L - Length the Data that follows (1 BYTE)
then actual data
|
by: Geetesh |
last post by:
Recently i saw a code in which there was a structer defination similar
as bellow:
struct foo
{
int dummy1;
int dummy2;
int last
};
In application the above array is always allocated at runtime using
malloc.In this last member of the structer "int last" is not
|
by: Michael Birkmose |
last post by:
Hi everyone,
I am in a situation where I need to instantiate a struct which has no tag
name:
struct some_struct {
struct {
int a;
} embedded_member;
};
|
by: Kobu |
last post by:
In embedded systems (programmed in C), often times structure
declarations are used to group together several status/control/data
registers of external hardware (or even internal registers). The
example below (from GBD's THE C BOOK) uses this. Is this a portable
method? .
/*
|
by: JS |
last post by:
I have this struct:
struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak;
};
These pointers:
| |
by: Christian Kandeler |
last post by:
Hi,
I'd like to know whether the following code is standard-compliant:
struct generic {
int x;
int a;
};
|
by: redefined.horizons |
last post by:
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.
I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.
That has brought...
|
by: hugo.arregui |
last post by:
Hi!
I have two struts like that:
struct {
int num;
int num2;
struct b arrayOfB;
} a;
|
by: Chris Saunders |
last post by:
Here is the C declaration of a struct from Windows:
typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {
//
// Used to return the required buffer size if return code is
STATUS_BUFFER_OVERFLOW
//
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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...
|
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...
| |