473,387 Members | 1,722 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,387 software developers and data experts.

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 1975
ne*****@tokyo.com (Mantorok Redgormor) wrote in
news:41**************************@posting.google.c om:
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.com (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.com (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
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 ...
7
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...
1
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...
19
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...
11
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
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...
17
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...
23
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.