473,387 Members | 1,440 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.

UB in assignment ?

Hi all,

I have my own string class with a reference counted body.
By mistake I wrote

mystring str = str = "abcde";

Did I invoke undefined behavior ?
(I guess so since I got a segfault)
Here my problem with some similar example code:

///////////////////////
#include <boost/shared_ptr.hpp>

int main()
{
boost::shared_ptr<double> f(new double);
boost::shared_ptr<double> g = g = f; // --- UB ?

return 0;
}
///////////////////////

Maett
Feb 20 '06 #1
3 1173
mystring str = str = "abcde";

I'd be surprised if that compiles.

You're making a fetus give birth.

-Tomás
Feb 20 '06 #2
Am Mon, 20 Feb 2006 19:12:49 GMT schrieb Tomás <NU**@NULL.NULL>:
mystring str = str = "abcde";

I'd be surprised if that compiles.

You're making a fetus give birth.


It *does* compile (GCC 3.4.4, Comeau); otherwise I would have noticed my fault immediately.

Maett
Feb 20 '06 #3
In article <ops5acq3cgmmfhfz@max>, Maett <no@spam.pls> wrote:
Hi all,

I have my own string class with a reference counted body.
By mistake I wrote

mystring str = str = "abcde";

Did I invoke undefined behavior ?
(I guess so since I got a segfault)
Here my problem with some similar example code:

///////////////////////
#include <boost/shared_ptr.hpp>

int main()
{
boost::shared_ptr<double> f(new double);
boost::shared_ptr<double> g = g = f; // --- UB ?

return 0;
}
///////////////////////


Remember, assignment is from right to left, so first g = f is called (on
an uninitialized g, *then* the copy c_tor is called to create g.

If your op= uses the temp-swap trick, it will seg fault every time (if
your lucky.)

This should make it clear what is happening:

class foo {
static int c;
int i;
public:
foo(): i( c++ ) { cout << "default c_tor " << i << '\n'; }
foo( const foo& ): i( c++ ) { cout << "copy c_tor " << i << '\n'; }
~foo() { cout << "d_tor " << i << '\n'; }

foo& operator=( const foo& o ) {
cout << "op= from " << i << " to " << o.i << '\n';
}
};

int foo::c = 0;

int main() {
foo w;
foo i = i = w;
}

On my machine, I got:

default c_tor 0
op= from -1880846991 to 0
copy c_tor 1
d_tor 1
d_tor 0

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 20 '06 #4

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
35
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
20
by: TimeHorse | last post by:
I would like to gauge interest in the following proposal: Problem: Assignment statements cannot be used as expressions. Performing a list of mutually exclusive checks that require data...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.