Adding empty copy constructor makes segfault go away?

Discussion in 'C++' started by William Payne, Aug 26, 2004.

  1. 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
    William Payne, Aug 26, 2004
    #1
    1. Advertising

  2. William Payne

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Aug 26, 2004
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Aug 26, 2004
    #3
  4. William Payne

    Andre Heinen Guest

    On Thu, 26 Aug 2004 19:16:50 +0200, "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


    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"
    Andre Heinen, Aug 27, 2004
    #4
  5. 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 ----
    Karthiik Kumar, Aug 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juho Saarikko
    Replies:
    6
    Views:
    389
    Christophe Cavalaria
    May 18, 2004
  2. Tim Peters
    Replies:
    1
    Views:
    319
    Juho Saarikko
    May 17, 2004
  3. Alexander Stippler

    copy constructor - optimized away or not?

    Alexander Stippler, Oct 30, 2005, in forum: C++
    Replies:
    5
    Views:
    312
    Valentin Samko
    Oct 30, 2005
  4. Andrey Vul
    Replies:
    8
    Views:
    670
    Richard Bos
    Jul 30, 2010
  5. cinsk
    Replies:
    35
    Views:
    2,556
    James Kanze
    Oct 11, 2010
Loading...

Share This Page