473,804 Members | 3,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

indeterminate value leading to undefined behavior?

In this context would an indeterminate value lead
down the path to undefined behavior?

(void)foo->member;

I want to keep my interface consisent in my program
so I have this one function which is passed an argument
that it doesn't need.

So I can only think of using it this way as a viable method
of staying consistent with my interface

That is, within a struct I have a pointer to a function
so I can't really change the interface as several other functions
rely on this particular argument.

So if member had an indeterminate value
would it be undefined behavior to use it
in the manner specified above?

I know casting it to (void) does not yield a value
so this is why I am unsure.

--
nethlek
Nov 14 '05 #1
3 1996
ne*****@tokyo.c om (Mantorok Redgormor) wrote in
news:41******** *************** ***@posting.goo gle.com:
In this context would an indeterminate value lead
down the path to undefined behavior?

(void)foo->member;
If foo->member points somewhere sensible, this is a no-op...

If foo->member has an indeterminate value, this would count as undefined
behaviour I think.
I want to keep my interface consisent in my program
so I have this one function which is passed an argument
that it doesn't need.


If it doesn't need it, and it's indeterminate, don't use it! There's
nothing wrong with passing an argument to a generic interface which doesn't
get used in some functions with that interface.

<snip>

To be honest, I can't think of any reason why you'd want to do the no-op
even if foo->member has a sensible value. Perhaps you should explain why
you want to do such a thing?

Ian Woods
--
"I'm a paranoid schizophrenic sado-masochist.
My other half's out to get me and I can't wait."
(c) Richard Heathfield, circa 1979
Nov 14 '05 #2
ne*****@tokyo.c om (Mantorok Redgormor) writes:
In this context would an indeterminate value lead
down the path to undefined behavior?

(void)foo->member;


I'm 95% sure that it is in fact undefined behavior. If the expression
statement

foo->member;

led to undefined behavior, you cannot avoid this by casting the expression
to void. The section in the standard about conversions to void (6.3.2.2#1)

| The (nonexistent) value of a void expression (an expression that has
| type void) shall not be used in any way, and implicit or explicit
| conversions (except to void) shall not be applied to such an expression.
| If an expression of any other type is evaluated as a void expression,
| its value or designator is discarded. (A void expression is evaluated
| for its side effects.)

states that the expression is still evaluated (which would already be
undefined behavior in your case), even if its value is discared
afterwards.

If any C expert disagrees with my interpretation of the standard, I'd be
curious to hear why. :)

Martin
Nov 14 '05 #3
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
ne*****@tokyo.c om (Mantorok Redgormor) writes:
In this context would an indeterminate value lead
down the path to undefined behavior?

(void)foo->member;


I'm 95% sure that it is in fact undefined behavior. If the expression
statement

foo->member;

led to undefined behavior, you cannot avoid this by casting the expression
to void. The section in the standard about conversions to void (6.3.2.2#1)

| The (nonexistent) value of a void expression (an expression that has
| type void) shall not be used in any way, and implicit or explicit
| conversions (except to void) shall not be applied to such an expression.
| If an expression of any other type is evaluated as a void expression,
| its value or designator is discarded. (A void expression is evaluated
| for its side effects.)

states that the expression is still evaluated (which would already be
undefined behavior in your case), even if its value is discared
afterwards.

If any C expert disagrees with my interpretation of the standard, I'd be
curious to hear why. :)


I'm no C expert, nor do I disagree with your assertion, but I'll comment
anyway. ;)

6.8.3p2 (Expression and null statements) states "The expression in an
expression statement is evaluated as a void expression for its side
effects."

So, it doesn't matter whether there's a void cast or not, the expression is
evaluated on the virtual machine. Of course, optimising out statements with
no side effects is perfectly within the realm of 'undefined behaviour' in
the situation where the evaluation may involve an indeterminate value.

--
Peter
Nov 14 '05 #4

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

Similar topics

4
5435
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is typedef struct Rec { unsigned char msg; unsigned long len;
7
1705
by: Anon Email | last post by:
Hi people, I'm playing around with Bartosz Milewski's code at the moment, and I got the following strange results upon execution of the code included further below. Please be aware that I deliberately tweaked the code to produce these results: Matter for 0 created Hello from world 1. Matter for 2009196833 created
1
1268
by: Jakob Bieling | last post by:
Hi, in a post from 1995 ('why pass references?') in this group, Steve Clamage said reading the value of an indeterminate pointer (no dereferencing taking place) is undefined behavior. While I am in no doubt about this, I could not find it in the Standard. Apart from the fact that I did not know where to look, the search function did not turn up anything useful. Can somebody please provide a page/paragraph number? Thanks!
19
2706
by: Holger Hasselbach | last post by:
- The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used (7.20.3.4). Something like this (include and checkings omitted): p = malloc(sizeof(*p) * 5); p = 1; p = 1; p = 1;
11
6921
by: Sweety | last post by:
hello to all members, i have strange question in C. main() { printf("%d",main) ; } here o/p is same for all m/c in TC++ version 3.0 i.e 657. I think this is not garbage.
6
2276
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain an `correct' return value when the argument `Num' is larger than or equal to the Macro `NUM_SYS'. If the argument is less than the Macro, the function without a `return' get an undefined value. I've made a test on Ms Windows 2000 and VC 6.
17
2352
by: minlar | last post by:
Hello,everyone: what the value of the variables in the next programe: { int x=35; char str; strcpy(str,"www.google.com");} what's the value of x and strlen(str)? any help is welcome, and I am puzzled.
23
2197
by: Tomás | last post by:
Anything wrong with the following code?: #include <cstdlib> int main() { for (unsigned i = 0; i != 1000; ++i) { int *p = reinterpret_cast<int*>( std::rand() );
9
2074
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
0
9705
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, 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...
0
9576
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,...
0
10567
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
10323
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
10310
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
10074
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
6847
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
5515
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...
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.