473,666 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding UB

I have a somewhat flippant question regarding undefined behaviour. Does
an operation which invokes undefined behaviour affect the whole program,
or are earlier statements guaranteed to execute correctly?

For example:

#include <stdio.h>

int main(void) {
int i;
printf("Hello, world!\n");
fflush(stdout);
i = i++; /* or some other undefined behaviour */
return 0;
}

Is the printf() statement guaranteed to execute? Is "Hello, world!\n"
guaranteed to be sent to stdout? Or could a conforming implementation
refuse to compile this program, or accept it and let loose nasal demons
without even greeting the world first?
Feb 26 '08 #1
5 1580
Philip Potter wrote:
I have a somewhat flippant question regarding undefined behaviour.
Does an operation which invokes undefined behaviour affect the whole
program, or are earlier statements guaranteed to execute correctly?

For example:

#include <stdio.h>

int main(void) {
int i;
printf("Hello, world!\n");
fflush(stdout);
i = i++; /* or some other undefined behaviour */
return 0;
}

Is the printf() statement guaranteed to execute? Is "Hello, world!\n"
guaranteed to be sent to stdout? Or could a conforming implementation
refuse to compile this program, or accept it and let loose nasal
demons without even greeting the world first?
I *think* a conforming implementation may do anything it wants with a
program that invokes undefined behaviour, including refusing to
translate it. If the latter, then a diagnostic is required. During
runtime, no constraint on program behaviour is imposed.

Having said that of course, one would be hard pressed to point to actual
implementations where invoking undefined behaviour affects behaviour of
the preceding program statements.

Feb 26 '08 #2
santosh wrote:
I *think* a conforming implementation may do anything it wants with a
program that invokes undefined behaviour, including refusing to
translate it. If the latter, then a diagnostic is required. During
runtime, no constraint on program behaviour is imposed.
FWIW, I can't find a normative reference to that effect (just notes,
footnotes).

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Feb 26 '08 #3
Philip Potter wrote:
I have a somewhat flippant question regarding undefined behaviour. Does
an operation which invokes undefined behaviour affect the whole program,
or are earlier statements guaranteed to execute correctly?

For example:

#include <stdio.h>

int main(void) {
int i;
printf("Hello, world!\n");
fflush(stdout);
i = i++; /* or some other undefined behaviour */
return 0;
}

Is the printf() statement guaranteed to execute? Is "Hello, world!\n"
guaranteed to be sent to stdout? Or could a conforming implementation
refuse to compile this program, or accept it and let loose nasal demons
without even greeting the world first?
Hard to say. Note that fflush() delivers any buffered
output "to the host environment," which may not be the same
thing as "to its final destination" -- which might, for
example, be on the far end of a TCP/IP connection currently
undergoing a network storm. fflush() will probably deliver
the data to the network stack and not wait for acknowledgment
from the other end, and then when the U.B. strikes and takes
the local machine down in flames, the remote side's socket
may just time out and give up. No greeting is received.

Also, there are undefined behaviors that are less strongly
tied to a particular execution path. For example,

/* main.c */
#include <stdio.h>
int answer = 42;
int function(void);
int main(void) {
printf ("%d\n", answer == 0 ? function() : answer);
return 0;
}

/* function.c */
extern double answer;
int function(void) { return answer; }

.... produces undefined behavior because the two declarations of
`answer' are in conflict (6.2.7p2). It's U.B. even though function()
is not called -- maybe the linker catches the mismatch and refuses
to produce an executable program, maybe the linker succeeds but
the program's frammis mapping is whirligigged, ...

--
Eric Sosman
es*****@ieee-dot-org.invalid
Feb 26 '08 #4
On Feb 26, 2:07*am, Philip Potter <p...@doc.ic.ac .ukwrote:
I have a somewhat flippant question regarding undefined behaviour. Does
an operation which invokes undefined behaviour affect the whole program,
or are earlier statements guaranteed to execute correctly?

For example:

#include <stdio.h>

int main(void) {
* * int i;
* * printf("Hello, world!\n");
* * fflush(stdout);
* * i = i++; /* or some other undefined behaviour */
* * return 0;

}

Is the printf() statement guaranteed to execute? Is "Hello, world!\n"
guaranteed to be sent to stdout? Or could a conforming implementation
refuse to compile this program, or accept it and let loose nasal demons
without even greeting the world first?
Consider:
int main(void) {
int i;
printf("Hello, world!\n");
memset(&i, 0, 1000000000);
fflush(stdout);
return 0;
}

The previous printf() call completes successfully.
Then the memset() function call causes some horrible crash and we
never see the output.
That would be an example of subsequent undefined behavior affecting
previously correct code.
I do not think it is safe even after the fflush() call because :

"2 Ifstream points to an output stream or an update stream in which
the most recent
operation was not input, the fflush function causes any unwritten data
for that stream
to be delivered to the host environment to be written to the file;
otherwise, the behavior is
undefined."

So the data has been delivered to the operating system, but I do not
think it is a guarantee that it is written (indeed, for some operating
systems you must have a successful fsync() call or some such thing to
really guarantee delivery to the output stream).

So it is clear that we cannot depend on earlier code that is correct
to produce expected output after the introduction of undefined
behavior later in the program.
Feb 26 '08 #5
Philip Potter wrote:
>
I have a somewhat flippant question
regarding undefined behaviour.
Does an operation which invokes undefined behaviour
affect the whole program,
Yes.
or are earlier statements guaranteed to execute correctly?
No.
For example:

#include <stdio.h>

int main(void) {
int i;
printf("Hello, world!\n");
fflush(stdout);
i = i++; /* or some other undefined behaviour */
return 0;
}

Is the printf() statement guaranteed to execute?
No.
Is "Hello, world!\n" guaranteed to be sent to stdout?
No.
Or could a conforming implementation
refuse to compile this program,
or accept it and let loose nasal demons
without even greeting the world first?
Yes.

UB is only what the standard says that it is.

What the standard guarantees, and how a program is likely to run,
are two different things.

The C standard comittee doesn't care about
what any program which contains any undefined behavior, does.

All that is required for a kind of code to be undefined,
is a lack of interest in that kind of code by the standard comittee.

--
pete
Feb 26 '08 #6

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

Similar topics

4
1807
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP. I have looked for a python web framework to build a web site, a site that I had start in php (and quite finish), but for some reason I wont explain, I lost everything. I have started writting app with python 6
2
3385
by: Jo_Calico | last post by:
I love the Dynamic Drive cross browser marquee script. I'd like to make the text loop immediately after completion, so the beginning runs right after the end (does that make sense?). Could anyone tell me how to do this? Here's the link: http://www.calicosystems.com/pilla/index1.htm Here's the code: <script language="JavaScript1.2">
3
2204
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace myErrorHandler
7
2454
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to *(MyArray+x), what statement is functionally identical to MyArray? I ask this question because when I create a dynamic array with one
6
2112
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
10
3437
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net 2005 created any AssemblyInfo.cs file.
14
1829
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
2
1624
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon command to close out the other session?
6
1512
by: Jon | last post by:
All, I'm working in a fairly robust content management system for our company's websites, and have a question regarding the file and directory structure of the site. Currently, I'm populating all content from each page using data in a MySQL DB, and php to call the DB and push out the info. Fairly standard right? Well, my question lies in the best approach to take regarding the file structure. Basically, as of now, I can make the...
5
1795
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to false as i want to start processing only on user specified time. I am setting interval as difference between user sepcified time and current time. And when that elapsed event occured i am again setting
0
8871
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
8783
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...
1
8552
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,...
0
8640
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
7387
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
6198
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
5666
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1776
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.