By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Adding empty copy constructor makes segfault go away?

P: n/a
Hello, I was under the impression that if I made a class Foo and if I didn't
specify a copy constructor I would get one anyway that simply assigns the
member variables (and that won't work for dynamically allocated member
variables). Anyway, I have a program that segfaults without a copy
constructor but if I add an empty one, the segfault is gone. The code is
ugly indeed so I don't want to post it, but, in general terms, what sort of
error could I be avoiding when I define my own empty copy constructor
instead of using the one the compiler would generate for me if I had none?
The class doesn't allocate any memory dynamically itself.

/ WP
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
William Payne wrote:
Hello, I was under the impression that if I made a class Foo and if I didn't
specify a copy constructor I would get one anyway that simply assigns the
member variables (and that won't work for dynamically allocated member
variables).
There is no such thing as "dynamically allocated member variables". There
are member variables that are pointers that potentially point to some kind
of dynamic object (or array of objects).

The default copy c-tor performs member-by-member copy construction. That
includes pointers that simply get their values copied. If the object
assumes that the pointer is to a dynamically allocated object (or array),
then it will try deallocating it at some point (another assumption, that
the object _owns_ the memory). As you can see "that won't work" is based
on at least two assumptions. If you don't follow those assumptions
blindly, it will work fine.
Anyway, I have a program that segfaults without a copy
constructor but if I add an empty one, the segfault is gone. The code is
ugly indeed so I don't want to post it, but, in general terms, what sort of
error could I be avoiding when I define my own empty copy constructor
instead of using the one the compiler would generate for me if I had none?
The class doesn't allocate any memory dynamically itself.


Ionno <shrug>... The class probably has a member that does some dynamic
memory allocation, and whose copy c-tor is not proper. When you write
an empty copy c-tor, the members are default-initialised instead of being
copy-initialised (as in the case of compiler-generated copy c-tor).

Victor
Jul 22 '05 #2

P: n/a
William Payne wrote:
Hello, I was under the impression that if I made a class Foo and if I
didn't specify a copy constructor I would get one anyway that simply
assigns the member variables (and that won't work for dynamically
allocated member variables). Anyway, I have a program that segfaults
without a copy constructor but if I add an empty one, the segfault is
gone. The code is ugly indeed so I don't want to post it, but, in general
terms, what sort of error could I be avoiding when I define my own empty
copy constructor instead of using the one the compiler would generate for
me if I had none? The class doesn't allocate any memory dynamically
itself.

/ WP


I do not understand,

what is an "empty copy constructor"? Are you sure, the copy constructor
copies?
Best

Kai-Uwe Bux
Jul 22 '05 #3

P: n/a
On Thu, 26 Aug 2004 19:16:50 +0200, "William Payne"
<mi**************@student.liu.se> wrote:
Hello, I was under the impression that if I made a class Foo and if I didn't
specify a copy constructor I would get one anyway that simply assigns the
member variables
Members of class type will be copy-constructed, and members of
integrated types (including pointers) will be bitwise-copied. I
suppose that's what you meant, but better make it clear.
(and that won't work for dynamically allocated member
variables). Anyway, I have a program that segfaults without a copy
constructor but if I add an empty one, the segfault is gone. The code is
ugly indeed so I don't want to post it, but, in general terms, what sort of
error could I be avoiding when I define my own empty copy constructor
instead of using the one the compiler would generate for me if I had none?
In this particular case, any error caused by an inappropriate
copy constructor of a member. If you create an empty copy
constructor, it won't do anything (except, of course, calling
default constructors for members of class type). Your members of
integrated types will be left uninitialized, but as you have no
pointer members, I can't imagine how this could cause a segfault.
The class doesn't allocate any memory dynamically itself.


Then I have no clue, other than checking the copy semantics of
your members of class type.

HTH,

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
Jul 22 '05 #4

P: n/a
William Payne wrote:
Hello, I was under the impression that if I made a class Foo and if I didn't
specify a copy constructor I would get one anyway that simply assigns the
member variables (and that won't work for dynamically allocated member
variables). Anyway, I have a program that segfaults without a copy
constructor but if I add an empty one, the segfault is gone. The code is
ugly indeed so I don't want to post it, but, in general terms, what sort of
error could I be avoiding when I define my own empty copy constructor
instead of using the one the compiler would generate for me if I had none?
The class doesn't allocate any memory dynamically itself.

/ WP

By adding an empty copy constructor, you are in effect nullifying
the whole notion of 'copying'. It probably would not make sense at all.
One explanation that could be given for the disappearance for the seg.
fault is that ,

* The pointers might have been '0' (The standard probably defines this,
but no good programmer would depend on such trivia when writing code.
Instead he/she would explicitly set it to zero to make the maintenance
easy).

* And the destructor might delete only those pointers that are not '0'.

--
Karthik.
------------ And now a word from our sponsor ---------------------
For a secure high performance FTP using SSL/TLS encryption
upgrade to SurgeFTP
---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.