I'm losing the address of an object... segmentation fault!

Discussion in 'C++' started by ChristophK, Feb 23, 2006.

  1. ChristophK

    ChristophK Guest

    Hallo,

    this is my first post and I hope I'm doing well, but if not, please let
    me know.

    My problem:
    First all classes and a main-file (I simplified everything to get to
    the core of the problem):

    /*---------------- start of code ---------------------------*/
    #include <iostream>

    /*! -------- class A --------- */
    class A
    {
    public:
    A(){};
    ~A(){};

    void func(double* value);
    protected:
    double* classvalue;
    };

    void A::func(double* value)
    {
    std::cerr << "-DEBUG- A::func - this: " << this << std::endl;
    classvalue = value;
    }

    class ADer:public A
    {
    public:
    ADer(){};
    ~ADer(){};
    };
    //yes, I need this class, though it looks needless

    /*! -------- class B --------- */
    class B
    {
    public:
    B(){};
    B(int Nr);
    ~B(){delete cA;};

    A* getA(int i) {return cA + i;};
    void func(int start, int end, double* value);
    private:
    A* cA;
    };

    B::B(int Nr)
    {
    cA = new A[Nr];
    }

    void B::func(int start, int end, double* value)
    {
    for(int i=start; i<end; ++i)
    {
    cA.func(value);
    }
    }

    class BDer:public B
    {
    public:
    BDer(){};
    BDer(int Nr);
    ~BDer(){};

    ADer* getA(int i) {return cA + i;};
    private:
    ADer* cA;
    };

    BDer::BDer(int Nr)
    {
    cA = new ADer[Nr];
    }

    /*! -------- main --------- */
    BDer* mBDer;

    int main(int argc, char **argv)
    {

    mBDer = new BDer(10);
    std::cerr << "-DEBUG- main - A: " << mBDer->getA(0) << std::endl;
    mBDer->func(0, 10, 0);

    }
    /*---------------- end of code ---------------------------*/

    I'm able to compile this cpp file with:
    g++ -o test test.cpp

    When I run the my programm a strange (at least for me) thing happens:
    >./test

    -DEBUG- main - A: 0x80493f4
    -DEBUG- A::func - this: 0
    Segmentation fault
    >


    Can anyone tell me what I'm doing wrong?

    Thanks in advance for all help that I get
    Greetings
    Christoph
    ChristophK, Feb 23, 2006
    #1
    1. Advertising

  2. You declare cA twice, this wont work...

    The second cA of BDer only hides cA of B, but dosent replace it.
    And func in B will use the cA of B... with is still uninitialized.
    Frank Schmidt, Feb 23, 2006
    #2
    1. Advertising

  3. ChristophK wrote:
    > this is my first post and I hope I'm doing well, but if not, please let
    > me know.
    >
    > My problem:
    > First all classes and a main-file (I simplified everything to get to
    > the core of the problem):
    >
    > /*---------------- start of code ---------------------------*/
    > #include <iostream>
    >
    > /*! -------- class A --------- */
    > class A
    > {
    > public:
    > A(){};
    > ~A(){};
    >
    > void func(double* value);
    > protected:
    > double* classvalue;


    Why a pointer?

    > };
    >
    > void A::func(double* value)
    > {
    > std::cerr << "-DEBUG- A::func - this: " << this << std::endl;
    > classvalue = value;


    You're assigning to the member something that is not necessarily coming
    from any place clean. Who knows where it's been?

    > }
    >
    > class ADer:public A
    > {
    > public:
    > ADer(){};
    > ~ADer(){};
    > };
    > //yes, I need this class, though it looks needless
    >
    > /*! -------- class B --------- */
    > class B
    > {
    > public:
    > B(){};


    What value does 'cA' get here?

    > B(int Nr);
    > ~B(){delete cA;};


    If you manage dynamic memory in your object, where is your copy-c-tor
    and where is your assignment operator?

    >
    > A* getA(int i) {return cA + i;};
    > void func(int start, int end, double* value);
    > private:
    > A* cA;
    > };
    >
    > B::B(int Nr)
    > {
    > cA = new A[Nr];
    > }
    >
    > void B::func(int start, int end, double* value)
    > {
    > for(int i=start; i<end; ++i)
    > {
    > cA.func(value);
    > }
    > }
    >
    > class BDer:public B
    > {
    > public:
    > BDer(){};
    > BDer(int Nr);
    > ~BDer(){};


    Here you don't even bother to delete 'cA'. Why?

    >
    > ADer* getA(int i) {return cA + i;};
    > private:
    > ADer* cA;
    > };
    >
    > BDer::BDer(int Nr)
    > {
    > cA = new ADer[Nr];
    > }
    >
    > /*! -------- main --------- */
    > BDer* mBDer;
    >
    > int main(int argc, char **argv)
    > {
    >
    > mBDer = new BDer(10);
    > std::cerr << "-DEBUG- main - A: " << mBDer->getA(0) << std::endl;
    > mBDer->func(0, 10, 0);
    >
    > }
    > /*---------------- end of code ---------------------------*/
    >
    > I'm able to compile this cpp file with:
    > g++ -o test test.cpp
    >
    > When I run the my programm a strange (at least for me) thing happens:
    >
    >>./test

    >
    > -DEBUG- main - A: 0x80493f4
    > -DEBUG- A::func - this: 0
    > Segmentation fault
    >
    >
    > Can anyone tell me what I'm doing wrong?


    You're mismanaging your dynamic memory. Do you really _need_ to do that?
    Your objects of class 'A' are better off just keeping 'double' instead of
    'double*'. The objects of type 'B' and 'Bder' are much better off keeping
    a _vector_ of 'A' instead of a dynamic array. Hint: do not attempt to use
    dynamic memory for members of object before you master using it for plain
    variables in functions. Better yet, don't manage your dynamic memory.
    Leave it to professionals and use standard containers and strings.

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Feb 23, 2006
    #3
  4. ChristophK

    Mark P Guest

    ChristophK wrote:

    [lots of code snipped]

    On top of what has already been mentioned, note also that when you
    dynamically allocate with new T[] you need to deallocate via delete [],
    not delete.
    Mark P, Feb 24, 2006
    #4
  5. ChristophK

    ChristophK Guest

    Thanks,
    so is it a better idea to use A as an "abstract" base class and define
    ADer1 and ADer2 for the different usages? I thought that since cA is a
    private member of B you can just redeclare it in the derived class BDer
    and C++ knows, depending on which class you have declared, what object
    cA is.

    But why I get the correct address using "ADer* getA(int i) {return cA +
    i;};" but no address using "void A::func(double* value)" or even with
    "void ADer::func(double* value)"?
    ChristophK, Feb 24, 2006
    #5
  6. ChristophK

    ChristophK Guest

    Thanks,
    I just forgot the brackets in this code snippet..
    ChristophK, Feb 24, 2006
    #6
  7. ChristophK

    ChristophK Guest

    Thanks,

    >You're assigning to the member something that is not necessarily coming
    >from any place clean. Who knows where it's been?

    I'm taking care of this something...

    >Why a pointer?

    the reason for the pointer is that in my real code I keep as a member
    an abstract class (not a double). In the function B::func() I assign
    the derived class to my abstract class pointer. I'm using this because
    I need a pool of different functions all expressed by only one function
    which is the interface of my abstract class. Maybe there is better
    solution for this problem?
    ****
    extremly simplified (my function are a bit more complex):
    C.func(a,b) = 0;
    CDer1.func(a,b){return a+b;};
    CDer2.func(a,b){return a-b;};
    CDer3.func(a,b){return a*b;};
    CDer4.func(a,b){return a/b;};
    ****
    >Here you don't even bother to delete 'cA'. Why?

    I just forgot it in this snippet. Next time I keep more care on my
    code, even it is only a simplified version, sorry.

    And the _vector_ idea is not the worst, maybe I will change this
    ChristophK, Feb 24, 2006
    #7
  8. "ChristophK" <> schrieb im Newsbeitrag
    news:...
    > Thanks,
    > so is it a better idea to use A as an "abstract" base class and define
    > ADer1 and ADer2 for the different usages?


    It would maybe a way that would work... but sounds like a realy bad
    design...
    no time atm... :( your mail looks german... so maybe mail me at tr @ raab
    dot com . de
    after removing the spaces...


    > I thought that since cA is a
    > private member of B you can just redeclare it in the derived class BDer
    > and C++ knows, depending on which class you have declared, what object
    > cA is.


    The private thing is only something that helps multiple programmers to work
    on the same program.
    Whether you use it or not, will change nothing on the generated program.

    Imagine a class Box, ProgrammerA puts in a "circumference" and calls it just
    c....
    He makes c private, to say "dont touch it", maybe in the next version i will
    remove it and calculate the circumference out of width and height.
    ProgrammerB will use the class Box to derive a ColoredBox, he does not know
    anything about the c for the circumference, so he will use a c for color...
    But for sure... The ColoredBox still got a circumference and all functions
    in Box will use c still as it.


    > But why I get the correct address using "ADer* getA(int i) {return cA +
    > i;};" but no address using "void A::func(double* value)" or even with
    > "void ADer::func(double* value)"?


    whether B::cA or BDer::cA is used only depends of the class where the
    function is in...
    B does not know anything about the new cA in BDer.
    Frank Schmidt, Feb 24, 2006
    #8
  9. ChristophK

    ChristophK Guest

    I've redesigned my application a little bit whereby I'm not facing this
    problem any longer. Thank you all very much for the help you gave me
    ChristophK, Feb 24, 2006
    #9
    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. Alex Hunsley
    Replies:
    17
    Views:
    849
  2. Pud
    Replies:
    0
    Views:
    563
  3. Joel
    Replies:
    4
    Views:
    9,242
    John Harrison
    Oct 11, 2004
  4. Jason C
    Replies:
    4
    Views:
    670
    Morty Abzug
    Jun 26, 2012
  5. Replies:
    11
    Views:
    663
Loading...

Share This Page