Adding empty copy constructor makes segfault go away?

W

William Payne

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
 
K

Kai-Uwe Bux

William said:
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
 
V

Victor Bazarov

William said:
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
 
A

Andre Heinen

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,
 
K

Karthiik Kumar

William said:
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 ----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top