473,400 Members | 2,145 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,400 software developers and data experts.

Adding empty copy constructor makes segfault go away?

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
4 3477
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
1
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
11
by: wij | last post by:
Hi: I don't understand why the following program doesn't compile. Can anyone explain? /* Build: g++ t.cpp */ #include <iostream> class Foo { Foo(const Foo& rhs) { std::cout << "Foo(const...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
7
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: keith | last post by:
Hi, I've been through the FAQ-lite and can't see this mentioned, so here goes... I've got an abstract base class called Base which has no copy constructor at all. In the derived class I have...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.