The following code crashes on Solaris 10 when compiled without
optimization:
typedef struct Node Node;
struct Node {
int val;
Node *next;
};
int main(void)
{
Node b = { 2, 0 };
Node a = { 1, &b };
a = *(a.next);
return a.val;
}
What happens is that after a has been written to, and a.next has been
set to null, a.next is dereferenced again (for some obscure reason).
Whose fault is this, the programmer's or the compiler's? Initially I
thought that it's the programmer's fault since there are no sequence
points between the access to a.next and the writing to a, but if that
made the code illegal, how about the following ubiquitous idiom:
Node* p = &a;
p = p->next;
Here, too, we both access p and write to p without sequence points in
between. What's the difference, or is there any?
Thanks in advance.
Lauri 16 1365
In article <et**********@o ravannahka.hels inki.fi>,
Lauri Alanko <la@iki.fiwrote :
>The following code crashes on Solaris 10 when compiled without optimization :
[snip bits that make it compileable]
Node b = { 2, 0 };
Node a = { 1, &b };
a = *(a.next);
>What happens is that after a has been written to, and a.next has been set to null, a.next is dereferenced again (for some obscure reason).
Whose fault is this, the programmer's or the compiler's? Initially I thought that it's the programmer's fault since there are no sequence points between the access to a.next and the writing to a,
As far as I can tell, the old value is accessed only to determine the
new value to be stored (access pointer in a.next to determine where to
find value to store, dereference pointer to get value to store), which
means it's perfectly acceptable according to 6.5#2 of n869, which is
the paragraph that would make it undefined if that were the problem.
So unless I'm missing something, this looks like a compiler bug.
dave
--
Dave Vandervies dj******@csclub .uwaterloo.ca
Well, it's logically consistent and interesting. That appears to be
all mathematicians need.
--James Riden in the scary devil monastery
Lauri Alanko <l...@iki.fiwro te:
The following code crashes on Solaris 10 when compiled without
optimization:
typedef struct Node Node;
struct Node {
int val;
Node *next;
};
int main(void)
{
Node b = { 2, 0 };
Node a = { 1, &b };
This violates constraint 6.7.8p4 which states that an
initialisor has to be a constant.
Try...
static Node b = { 2, 0 };
static Node a = { 1, &b };
>
a = *(a.next);
return a.val;
Note that neither 1 nor 2 are portable values for main to
return to the host. Use 0, EXIT_SUCCESS or EXIT_FAILURE;
the latter two from <stdlib.h>.
}
What happens is that after a has been written to, and a.next
has been set to null, a.next is dereferenced again (for some
obscure reason).
Whose fault is this, the programmer's or the compiler's?
One fault is the programmer's. If fixing that doesn't fix the
problem, then it appears to be the compiler's.
--
Peter
Peter Nilsson wrote:
Lauri Alanko <l...@iki.fiwro te:
>The following code crashes on Solaris 10 when compiled without optimization :
typedef struct Node Node;
struct Node { int val; Node *next; };
int main(void) { Node b = { 2, 0 }; Node a = { 1, &b };
This violates constraint 6.7.8p4 which states that an
initialisor has to be a constant.
No; the cited paragraph says
All the expressions in an initializer for an object that
^^^^^^^^^^^^^^^ ^^^
has static storage duration shall be constant expressions
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
or string literals.
(Emphasis mine.)
>Whose fault is this, the programmer's or the compiler's?
One fault is the programmer's. If fixing that doesn't fix the
problem, then it appears to be the compiler's.
I think (mind you, I say I "think") it's the compiler's fault.
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman <es*****@acm-dot-org.invalidwrit es:
Peter Nilsson wrote:
>Lauri Alanko <l...@iki.fiwro te:
>>int main(void) { Node b = { 2, 0 }; Node a = { 1, &b };
This violates constraint 6.7.8p4 which states that an initialisor has to be a constant.
No; the cited paragraph says
All the expressions in an initializer for an object that
^^^^^^^^^^^^^^^ ^^^
has static storage duration shall be constant expressions
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
or string literals.
But in C89, the corresponding paragraph said:
All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions.
(or something similar; I'm quoting from a draft.)
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Ben Pfaff wrote:
>
Eric Sosman <es*****@acm-dot-org.invalidwrit es:
Peter Nilsson wrote:
Lauri Alanko <l...@iki.fiwro te: int main(void) { Node b = { 2, 0 }; Node a = { 1, &b };
This violates constraint 6.7.8p4 which states that an
initialisor has to be a constant.
No; the cited paragraph says
All the expressions in an initializer for an object that
^^^^^^^^^^^^^^^ ^^^
has static storage duration shall be constant expressions
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
or string literals.
But in C89, the corresponding paragraph said:
All the expressions in an initializer
for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions.
(or something similar; I'm quoting from a draft.)
The same words are in ISO/IEC 9899: 1990.
--
pete
On Mar 23, 2:52 am, Lauri Alanko <l...@iki.fiwro te:
The following code crashes on Solaris 10 when compiled without
optimization:
typedef struct Node Node;
struct Node {
int val;
Node *next;
};
int main(void)
{
Node b = { 2, 0 };
Node a = { 1, &b };
a = *(a.next);
return a.val;
}
What happens is that after a has been written to, and a.next has been
set to null, a.next is dereferenced again (for some obscure reason).
How do you know?
Whose fault is this, the programmer's or the compiler's? Initially I
thought that it's the programmer's fault since there are no sequence
points between the access to a.next and the writing to a.
There's an ugly clause in the standard that defines what's legal.
Informally, it is legal to both read from and write to a variable
without a sequence point, if and only if you must perform the read
in order to compute the value to be written -- ie. there is a
temporal relationship.
In this case, it is OK because you cannot dereference a.next without
first evaluating a.next, which in turn requires that you have already
evaluated 'a'.
On Mar 22, 6:52 am, Lauri Alanko <l...@iki.fiwro te:
The following code crashes on Solaris 10 when compiled without
optimization:
typedef struct Node Node;
struct Node {
int val;
Node *next;
};
int main(void)
{
Node b = { 2, 0 };
Node a = { 1, &b };
a = *(a.next);
return a.val;
}
What happens is that after a has been written to, and a.next has been
set to null, a.next is dereferenced again (for some obscure reason).
Whose fault is this, the programmer's or the compiler's? [...]
<snip>
If you're using C89, it's the programmer's fault. Quoting from my BS/
EN 29899:1993 A4 hardcopy:
6.5.7 Initialization
Constraints [...]
All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions.
Obviously the code "Node a = { 1, &b };" does not satisfy this
constraint. In fact, my compiler complains about it when invoked in
strict C89 mode:
[mark@icepick ~]$ gcc -Wall -O2 foo.c -o foo -ansi -pedantic -std=c89
foo.c: In function 'main':
foo.c:14: warning: initializer element is not computable at load time
With the advent of GNU C and C99, the rules have changed. Quoting
9899:1999 TC2 draft N1124:
6.7.8 Initialization
Constraints [...]
4. All the expressions in an initializer for an object that has
static storage duration shall be constant expressions or string
literals.
Notice that the constraints have been loosened for aggregate and union
types.
Although I certainly cannot speak for the WG, it appears that one of
the reasons this feature has been adopted in C99 is the prevalent use
of the following GNU C extension (which pre-dates C99):
[GCC 2.95.3 Manual, Extensions to the C Language Family]
4.18 Non-Constant Initializers
As in standard C++, the elements of an aggregate initializer for an
automatic variable are not required to be constant expressions in GNU
C.
Initially I
thought that it's the programmer's fault since there are no sequence
points between the access to a.next and the writing to a, but if that
made the code illegal, how about the following ubiquitous idiom:
Node* p = &a;
p = p->next;
Here, too, we both access p and write to p without sequence points in
between. What's the difference, or is there any?
The difference is that the undefined behavior is invoked in the
initialization of the type. This undefined behavior is apparently
causing the assignment to crash, which is one of the things that
undefined behavior often does.
In contrast, the pointer object Node* p is not an aggregate type, and
not subject to the same initialization rules that an aggregate or
union type is.
Mark F. Haigh mf*****@sbcglob al.net
On Mar 22, 2:52 pm, Lauri Alanko <l...@iki.fiwro te:
The following code crashes on Solaris 10 when compiled without
optimization:
typedef struct Node Node;
struct Node {
int val;
Node *next;
};
int main(void)
{
Node b = { 2, 0 };
Node a = { 1, &b };
a = *(a.next);
return a.val;
}
What happens is that after a has been written to, and a.next has been
set to null, a.next is dereferenced again (for some obscure reason).
Whose fault is this, the programmer's or the compiler's? Initially I
thought that it's the programmer's fault since there are no sequence
points between the access to a.next and the writing to a, but if that
made the code illegal, how about the following ubiquitous idiom:
Node* p = &a;
p = p->next;
Here, too, we both access p and write to p without sequence points in
between. What's the difference, or is there any?
Thanks in advance.
Lauri
Since there are complaints about the initialisers (why the hell would
a compiler accept an initialisation if it invokes undefined
behavior? ), could you tell us what happens if you write
int main(void)
{
Node b, a;
b.val = 2; b.next = 0;
a.val = 1; a.next = &b;
a = *(a.next);
return a.val;
}
christian.bau wrote, On 24/03/07 00:39:
<snip>
Since there are complaints about the initialisers (why the hell would
a compiler accept an initialisation if it invokes undefined
behavior? ), could you tell us what happens if you write
<snip>
Look up undefined in a dictionary or the C standard. It means it is not
defined, part of not being defined is that it does not define that a
diagnostic should be produced.
--
Flash Gordon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Carl |
last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O"
http://www.osnews.com/story.php?news_id=5602
I think this is an unfair comparison! I wouldn't dream of developing a
numerical application in Python without using prebuilt numerical libraries
and data objects such as dictionaries and lists.
I have been experimenting with numerical algorithms in Python with a heavy
use of the Numeric module. My experience is that Python...
|
by: Yuri Victorovich |
last post by:
In short my question is:
If I overload "operator new" for class A and return from it
instance of struct B (unrelated with A) as allocated memory
area for A should aliasing rules work and allow optimizer
to "merge" assemblies together ?
My opinion: NO, since aliasing rules talk about one lvalue
for access to two unrelated objects and one of these objects
is not constructed yet w/in operator new.
|
by: Michel |
last post by:
Is there a way I can anti-aliasing a gif to be able to get a
hi-quality resizeable backgroundpicture.
When a GIF of JPG is being resized by the browser you get wurse
pictures because it needs a anti-aliasing. Maybe there is some filter
for it...
Or can I write my own subroutine to do this in client-browsers?
|
by: Adam Warner |
last post by:
Hi all,
Message ID <c1qo3f0tro@enews2.newsguy.com> is one of many informative
articles by Chris Torek about C. The particular message discusses aliasing
and concludes with this paragraph:
Under these strict type-aliasing rules, casting from (e.g.) "int *" to
"short *" is not only quite suspicious, it is also likely to cause
puzzling behavior, at least if you expect your "short *" to access or
modify your "int". Even the time-honored,...
|
by: nicolas.riesch |
last post by:
I try to understand strict aliasing rules that are in the C Standard.
As gcc applies these rules by default, I just want to be sure to
understand fully this issue.
For questions (1), (2) and (3), I think that the answers are all "yes",
but I would be glad to have strong confirmation.
About questions (4), (5) and (6), I really don't know. Please help ! !
!
| |
by: Hallvard B Furuseth |
last post by:
I'm getting horribly lost in the strict aliasing rules.
Is this code correct?
struct A { int x; };
struct B { int x, y; };
int foo( struct A *a ) {
struct B *b = (struct B *) a;
return b->x - b->y;
}
|
by: Old Wolf |
last post by:
Consider the following program:
#include <stdio.h>
int main(void)
{
/* using malloc to eliminate alignment worries */
unsigned long *p = malloc( sizeof *p );
if ( p && sizeof(long) == sizeof(int) )
|
by: Francois Appert |
last post by:
This post was originally in the C# Corner site, but their server is
down. I'd like to see if this group can answer.
I program in C++ and am learning C#.
The issue is: why should anybody bother in C# with pass-by-reference
using the "ref" keyword or "out" for objects in a method parameter
list, when, after all, it appears in C# that for all intents and
purposes a reference is always being passed, rather than the real
object (with, it...
|
by: Paul Brettschneider |
last post by:
Hello all,
consider the following code:
typedef char T;
class test {
T *data;
public:
void f(T, T, T);
void f2(T, T, T);
|
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: 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: 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: 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...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |