473,854 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ teaser: Is this a compiler bug, or is this expected behavior?

Compile the following snippet of code and run it. If the program spits
out bat:bat instead of bat:zat, what would you say? Would you say that
the compiler has a problem, or would you lay the blame on "undefined
execution of function parameters" in the C/C++ standard and "sequence
points"?

/////// Code snippet begins ///////

#include <iostream>
char foo[10]="cat";
char* writestring()
{
foo[0]='b';
return foo;
}

char* write2()
{
foo[0]='z';
return foo;
}
int main(void)
{ std::cout << writestring() << ":" << write2() << std::endl; }

/////// Code snippet ends ///////

Thanks,
Bhat
[Purists who hold that this NG is meant to discuss compiler neutral,
standard C++ issues only may not proceed beyond this point;-)]




For those of you who are "trivially inclined", here's some
background..... .
I stumbled upon a "bug" in my C++ compiler (g++ 3.3.1), which I
promptly reported to Bugzilla. The code snippet above was actually
provided by someone from the GCC volunteer community. They attributed
the unexpected behavior to the undefined behavior of execution of
function parameters and sequence points. In my original code snippet,
I was maintaining an STL map between IP addresses e.g. 105.52.20.33,
5000 and 47.32.68.95, 6000.

When I displayed the entries in the map, the second IP address was
displayed incorrectly. So instead of the mapping:

105.52.20.33, 5000 >>-->> 47.32.68.95, 6000
I got

105.52.20.33, 5000 >>-->> 105.52.20.33, 6000

The bug does not manifest when the code is compiled using native
Solaris C++
compiler version "WorkShop Compilers 5.0 02/04/10 C++ 5.0 Patch
107311-17"

Here's my original code snippet

/////// Code snippet begins ///////
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string>
#include <map>
#include <iostream>

using namespace std;
struct addrLessThan:pu blic binary_function <const struct sockaddr_in,
const
struct sockaddr_in, bool>
{
bool operator()(cons t struct sockaddr_in addr1, const struct
sockaddr_in
addr2) const
{
bool retVal = true;

string addrStr1 = inet_ntoa(addr1 .sin_addr);
string addrStr2 = inet_ntoa(addr2 .sin_addr);

if(addrStr1 > addrStr2)
retVal = false;
else if(addrStr1 == addrStr2)
retVal = (addr1.sin_port < addr2.sin_port) ;

return retVal;
}
};

typedef map<struct sockaddr_in, struct sockaddr_in, addrLessThan>
IpV4AddrMap;

main()
{
struct sockaddr_in actualAddress, mappedAddress;

actualAddress.s in_port=5000;
actualAddress.s in_addr.s_addr = inet_addr("105. 52.20.33");

mappedAddress.s in_port=6000;
mappedAddress.s in_addr.s_addr = inet_addr("47.3 2.68.95");

IpV4AddrMap map;

map[actualAddress] = mappedAddress;

IpV4AddrMap::it erator itor = map.find(actual Address);

if(itor != map.end())
{
cout << "Key: " << inet_ntoa(itor->first.sin_addr )
<< ", " << itor->first.sin_po rt << endl
<< "Value: " << inet_ntoa(itor->second.sin_add r)
<< ", " << itor->second.sin_por t << endl
<< endl;
}
return 0;
}

/////// Code snippet ends ///////
For more details, you can go to
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22265

Jul 23 '05 #1
13 1664
Generic Usenet Account wrote:
Compile the following snippet of code and run it. If the program spits
out bat:bat instead of bat:zat, what would you say? Would you say that
the compiler has a problem, or would you lay the blame on "undefined
execution of function parameters" in the C/C++ standard and "sequence
points"?

/////// Code snippet begins ///////

#include <iostream>
char foo[10]="cat";
char* writestring()
{
foo[0]='b';
return foo;
}

char* write2()
{
foo[0]='z';
return foo;
}
int main(void)
{ std::cout << writestring() << ":" << write2() << std::endl; }
[..]


Yes, the latter, the correct term is "the order of evaluation of the
function arguments is unspecified". A simpler expression is

cout << writestring() << write2();

which is the same as

( cout.operator<< ( writestring() ) ) . operator<< ( write2() );

in which 'write2()' is allowed to be evaluated before 'writestring()'
as I understand it. The part to the right of the second dot is the
function with its arguments. The left part is the object, which also
needs to be evaluated...

V
Jul 23 '05 #2
Generic Usenet Account wrote:
Compile the following snippet of code and run it. If the program
spits out bat:bat instead of bat:zat, what would you say? Would
you say that the compiler has a problem, or would you lay the blame
on "undefined execution of function parameters" in the C/C++
standard and "sequence points"?

#include <iostream>
char foo[10]="cat";
char* writestring()
{
foo[0]='b';
return foo;
}

char* write2()
{
foo[0]='z';
return foo;
}

int main(void)
{ std::cout << writestring() << ":" << write2() << std::endl; }
The behaviour is unspecified (NOT undefined), and
"bat:bat", "bat:zat", and "zat:zat" are all valid outputs.
(But "zat:bat" is not.)

You must remember that writestring() can be called at any point
between the start of this statement's execution, and the point
where its return value is needed. The same goes for write2().

Sequence points are not an issue here, because there are
no instances of multiple side-effects occuring without an
intervening sequence point (a function call has a sequence
point after its arguments have been evaluated, and another one
as it returns).

The example has some similarities to:

foo( a(), b() );

where there is no reason to suspect that a() will be called
before b().

BTW, Why are you posting to comp.sources.d ?
The code snippet above was actually provided by someone from
the GCC volunteer community. They attributed the unexpected
behavior to the undefined behavior of execution of function
parameters and sequence points.
If that was their exact wording, then they are wrong (or
expressed their intention incorrectly).

The behaviour is only unexpected if you were expecting
the wrong thing :)
I stumbled upon a "bug" in my C++ compiler (g++ 3.3.1), which I
promptly reported to Bugzilla. cout << "Key: " << inet_ntoa(itor->first.sin_addr )
<< ", " << itor->first.sin_po rt << endl
<< "Value: " << inet_ntoa(itor->second.sin_add r)
<< ", " << itor->second.sin_por t << endl
<< endl;


Unfortunately you have wasted the time of the Bugzilla people.
You have correctly identified the essence of the "problem",
namely that inet_ntoa() returns a pointer into a static buffer.
In fact, on my system, the inet_ntoa manpage specifically
says:
The string is
returned in a statically allocated buffer, which
subsequent calls will overwrite.

If you still think this is a bug, then what do you think the
'fix' should be? The most common suggestion that people make
on comp.lang.c (or c++) is to force left-to-right evaluation
of function parameters.

This has been discussed to death before, but the main reason
for opposing it is that it would force compilers to produce
slower code in many cases. For example, some calling conventions
feature parameters being pushed onto a stack, with the right-most
parameters pushed first. A function with this calling convention
would need the compiler to jump through some hoops, instead of
a few simple function calls followed by a stack push of the
return value.

Jul 23 '05 #3
Geo
Old Wolf wrote:
The behaviour is unspecified (NOT undefined), and
"bat:bat", "bat:zat", and "zat:zat" are all valid outputs.
(But "zat:bat" is not.)


Attempting to modify a literal value is undefined behaviour, surely ?

Jul 23 '05 #4


Geo schreef:
Old Wolf wrote:
The behaviour is unspecified (NOT undefined), and
"bat:bat", "bat:zat", and "zat:zat" are all valid outputs.
(But "zat:bat" is not.)


Attempting to modify a literal value is undefined behaviour, surely ?


It would be. However, char[10] is not a literal. It can be modified.
It's equivalent to { int foo = 10; ++foo; } That doesn't modify 10.

HTH,
Michiel Salters

Jul 23 '05 #5
Geo


msalters wrote:
Geo schreef:
Old Wolf wrote:
The behaviour is unspecified (NOT undefined), and
"bat:bat", "bat:zat", and "zat:zat" are all valid outputs.
(But "zat:bat" is not.)


Attempting to modify a literal value is undefined behaviour, surely ?


It would be. However, char[10] is not a literal. It can be modified.
It's equivalent to { int foo = 10; ++foo; } That doesn't modify 10.

HTH,
Michiel Salters


No it's not equivalent at all,
char foo[10]="cat";

reserves 10 character slots and points char[0] at the address of "cat",
which is a literal. Later, foo[0] = 'z' is an attempt to modify the
first chatacter of "cat", i.e. modify the literal, which is undefined
behaviour.

Jul 23 '05 #6
Geo wrote:

msalters wrote:
Geo schreef:
Old Wolf wrote:

> The behaviour is unspecified (NOT undefined), and
> "bat:bat", "bat:zat", and "zat:zat" are all valid outputs.
> (But "zat:bat" is not.)

Attempting to modify a literal value is undefined behaviour, surely ?


It would be. However, char[10] is not a literal. It can be modified.
It's equivalent to { int foo = 10; ++foo; } That doesn't modify 10.

HTH,
Michiel Salters


No it's not equivalent at all,

char foo[10]="cat";

reserves 10 character slots and points char[0] at the address of "cat",
which is a literal. Later, foo[0] = 'z' is an attempt to modify the
first chatacter of "cat", i.e. modify the literal, which is undefined
behaviour.


You might want to reread your 'C++ begining programmers intorduction'
to figure out what
char foo[10] = "cat";
realy does.
Hint: It does not what you describe above.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #7
Geo wrote:

msalters wrote:
Geo schreef:

Attempting to modify a literal value is undefined behaviour, surely ?
It would be. However, char[10] is not a literal. It can be modified.
It's equivalent to { int foo = 10; ++foo; } That doesn't modify 10.



No it's not equivalent at all,
char foo[10]="cat";

reserves 10 character slots and points char[0] at the address of "cat",
which is a literal. Later, foo[0] = 'z' is an attempt to modify the
first chatacter of "cat", i.e. modify the literal, which is undefined
behaviour.


I may be wrong, but I was under the impression that:
char foo[10]="cat";
results in an array of size 10 in which members are initialised from
the string "cat" (including terminating \0). Whereas
char *bar="cat";
results in a pointer which points to the address of the literal "cat".

--
imalone
Jul 23 '05 #8


Geo schreef:
msalters wrote:
Geo schreef:
Old Wolf wrote:

> The behaviour is unspecified (NOT undefined), and
> "bat:bat", "bat:zat", and "zat:zat" are all valid outputs.
> (But "zat:bat" is not.)

Attempting to modify a literal value is undefined behaviour, surely ?


It would be. However, char[10] is not a literal. It can be modified.
It's equivalent to { int foo = 10; ++foo; } That doesn't modify 10.

HTH,
Michiel Salters


No it's not equivalent at all,
char foo[10]="cat";

reserves 10 character slots and points char[0] at the address of "cat",
which is a literal. Later, foo[0] = 'z' is an attempt to modify the
first chatacter of "cat", i.e. modify the literal, which is undefined
behaviour.


That's the description for { const char* foo = "cat"; }

You can't even point foo[0] to "cat". foo[0] is a char, check typeid()
or sizeof() if you don't believe me. A 'char' is not a 'char*', and
only the latter points.

Also, if you could point foo to "cat", you surely could later point
it to "dog". However, the compiler will tell you that

char foo[10]="cat";
foo = "dog"

is illegal. Of course,

const char* foo = "cat";
foo = "dog";

is legal.

HTH,
Michiel Salters

Jul 23 '05 #9
Geo
Sorry my mistake, you are all of course correct, I'll shut up now.

Jul 23 '05 #10

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

Similar topics

37
4703
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the values in (as would be required if I used the hideous, evil, much-abused #define :) ----------- const int constantA = 10; static const int constantB = 20;
5
1509
by: Steven T. Hatton | last post by:
What should I expect the following code to print? Is it defined in the Standard? What does it produce for you? I was kind of surprised by what GCC 4.0.2 made of it. #include <string> #include <iostream> typedef std::string object; class arrow{
24
1803
by: s.subbarayan | last post by:
Dear all, According to standards is this valid: char TmpPtrWriteBuffer; void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to void* though the reverse is true.But they said this is valid. I just can't agree with them with out a valid explaination.Can any C standard experts clarify me this?
8
1451
by: aditya | last post by:
Hi all, Can body please me that why the following code in not working as expected.Basically,my aim was to shift the control from one function to another as soon as I presses Control-c keys. In other words,I was expecting the program to execute in the following way- 1. Initially,the control is in the while loop of the main function.
5
1437
by: Stephen Mayes | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main (void) { static char * contents = "Line1\nLine2\nLine3\nLine4"; FILE * tmp; char readbuf; size_t len, n = 0;
15
2603
by: Chung Leong | last post by:
Here's a little brain teaser distilled from a bug that took me a rather long time to figure out. The two functions in the example below behave differently. The difference is easy to spot, of ocurse. The challenge is correctly explaining why this is so. Why does the second function seemingly corrupt the cloned copy of an object? Sample code: <?php
14
1596
by: hsharsha | last post by:
Consider the below code snippet: #include <iostream> using namespace::std; class myclass { public: myclass() { cout << "constructor" << endl;
4
1634
by: duffdevice | last post by:
Hi, I came across this unexpected behavior while working on something else. I am attempting to return a custom type by value from a global function. I have a trace in the custom class's copy constructor, and I expected this code to tell me that the custom copy constructor had been called twice. Instead, it's only called once and the program executes correctly. Is this an optimization related to my compiler (g++ v4.0.1) or is this...
32
2079
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
0
9901
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
9750
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
11024
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...
1
10744
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
10364
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
9510
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...
0
5738
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...
1
4550
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
3
3182
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.