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

C

ChristophK

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:
-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
 
F

Frank Schmidt

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.
 
V

Victor Bazarov

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

-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
 
M

Mark P

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.
 
C

ChristophK

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)"?
 
C

ChristophK

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
 
F

Frank Schmidt

ChristophK said:
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.
 
C

ChristophK

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top