Like Tree3Likes
  • 1 Post By iMalc
  • 2 Post By oogabooga

Constructor calling constructor: SIGSEGV

This is a discussion on Constructor calling constructor: SIGSEGV within the C++ Programming forums, part of the General Programming Boards category; I have a class with two constructors, one constructor has no arguments, the other constructor has one argument. The constructor ...

  1. #1
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69

    Constructor calling constructor: SIGSEGV

    I have a class with two constructors, one constructor has no arguments, the other constructor has one argument. The constructor with no arguments calls the other with a default argument. The issue is, that when I dynamically allocate memory with the first constructor, it does not remain set as control goes to the other constructor. I don't think i've made myself clear, so I will write some code to show you what I mean.

    Code:
    class Aclass {
      public:
        Aclass();
        Aclass(int);
        void test();
      private:
        int *var;
    }
    
    ...
    
    Aclass::Aclass()
    {
      Aclass(10);
    }
    
    Aclass::Aclass(int x)
    {
      var = new int[x]; // allocate memory
      var[0] = x*x;
    }
    
    void Aclass::test()
    {
      // here var is NULL!
      var[1] = 3; // SIGSEGV
      // but if this object was created by calling Aclass(10) directly, there is no problem
    }
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    You need to use Delegating Constructors of C++11 here.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    my first tip do not allocate in constructor bcause it may cause mem leaks - i read that somewhere
    do it in a private init proc so u don't need to call the contructor again that should work

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Well line 14 is creating another instance of your class, which then gets thrown away at line 15.

    If you step through this with the debugger, you should see that your var member for the current instance isn't being modified.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    I wouldn't really call that a default argument, if you want to use a true default argument then a single constructor can do what you are attempting:
    Code:
    class Aclass {
      public:
        Aclass(int);
        void test();
      private:
        int *var;
    };
     
    ...
     
    Aclass::Aclass(int x = 10)
    {
      var = new int[x]; // allocate memory
      var[0] = x*x;
    }
     
    void Aclass::test()
    {
      var[1] = 3;
    }
    In this case if you call the Aclass constructor without an argument, the default value of x will be set to 10 for you automatically.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    Quote Originally Posted by hk_mp5kpdw View Post
    I wouldn't really call that a default argument, if you want to use a true default argument then a single constructor can do what you are attempting:
    Code:
    class Aclass {
      public:
        Aclass(int);
        void test();
      private:
        int *var;
    };
     
    ...
     
    Aclass::Aclass(int x = 10)
    {
      var = new int[x]; // allocate memory
      var[0] = x*x;
    }
     
    void Aclass::test()
    {
      var[1] = 3;
    }
    In this case if you call the Aclass constructor without an argument, the default value of x will be set to 10 for you automatically.
    Thanks. I will probably do that.
    Not sure... someone else's reply might be better. This is only my first C++ program. I have programmed in C and Java ( Got really good at Java! ) and that's why I got as far as I did without running into anything strange...

    Thanks everybody.
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  7. #7
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    Quote Originally Posted by manasij7479 View Post
    You need to use Delegating Constructors of C++11 here.
    How? Looked on wikipedia, did not make sense.
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by NA9x2000 View Post
    my first tip do not allocate in constructor bcause it may cause mem leaks - i read that somewhere
    do it in a private init proc so u don't need to call the contructor again that should work
    My first tip would be that your first tip is rubbish. Allocating memory is best done in a constructor. Please don't try and pass on misunderstood, badly remembered, untrue, and unquoted rubbish information.

    I read somewhere that 50% of all statistics are made up.
    Bugs cause memory leaks. You should probably avoid bugs instead.
    rags_to_riches likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    Please don't try and pass on misunderstood, badly remembered, untrue, and unquoted rubbish information.
    People should know that there's three levels of advice at cprogramming:

    1) Those that don't know what they're talking about.
    2) Me.
    3) Those that do know what they're talking about.

    Choose wisely.
    Elkvis and rags_to_riches like this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by Benji Wiebe View Post
    How? Looked on wikipedia, did not make sense.
    Code:
    #include<iostream>
    class foo
    {
    public:
            foo(int x_):x(x_){};//Normal constructor taking an argument
    
            foo():foo(10){};//Default Constructor calling the above constructor with a default value
    
            int x;
    };
    int main()
    {
            foo y;
            std::cout<<y.x;
            return 0;
    
    }
    You'll need a compiler supporting it though.
    GCC versions >=4.7 do, as I verified this on my machine. (with the -std=c++11 flag, if this is the first C++11 feature you're seeing.)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    If you don't use GCC, you can simple make two constructors which both call some common initialization routine function, that would initialize your pointers and all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-21-2011, 02:19 AM
  2. calling a function in constructor
    By rogster001 in forum C++ Programming
    Replies: 12
    Last Post: 12-30-2009, 05:02 PM
  3. Replies: 10
    Last Post: 06-02-2008, 09:09 AM
  4. Constructor calling another constructor
    By renanmzmendes in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 04:51 PM
  5. Constructor calling another constructor?
    By cpjust in forum C++ Programming
    Replies: 10
    Last Post: 02-11-2008, 02:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21