Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I'm losing the address of an object... segmentation fault!

Reply
Thread Tools

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

 
 
ChristophK
Guest
Posts: n/a
 
      02-23-2006
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 ADerublic 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[i].func(value);
}
}

class BDerublic 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

 
Reply With Quote
 
 
 
 
Frank Schmidt
Guest
Posts: n/a
 
      02-23-2006

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.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-23-2006
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 ADerublic 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[i].func(value);
> }
> }
>
> class BDerublic 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
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      02-24-2006
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.
 
Reply With Quote
 
ChristophK
Guest
Posts: n/a
 
      02-24-2006
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)"?

 
Reply With Quote
 
ChristophK
Guest
Posts: n/a
 
      02-24-2006
Thanks,
I just forgot the brackets in this code snippet..

 
Reply With Quote
 
ChristophK
Guest
Posts: n/a
 
      02-24-2006
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

 
Reply With Quote
 
Frank Schmidt
Guest
Posts: n/a
 
      02-24-2006

"ChristophK" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
ChristophK
Guest
Posts: n/a
 
      02-24-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex losing <br> (different from the earlier topic about losing $1) Jason C Perl Misc 4 06-26-2012 10:29 PM
Wireless adapter losing network address Bella Simona Wireless Networking 0 10-09-2009 02:20 PM
Wireless adapter losing network address even though signal is strong... Howard Woodard Wireless Networking 2 07-11-2009 07:21 PM
Losing Drives - Finding Drives - Losing Drives mel@no.spam.com Computer Support 2 09-21-2007 10:16 PM
Trouble losing old DHCP IP address libertybasic Wireless Networking 9 12-27-2005 11:07 PM



Advertisments