473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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[i].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

Feb 23 '06 #1
8 1607

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.
Feb 23 '06 #2
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[i].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
Feb 23 '06 #3
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.
Feb 24 '06 #4
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)"?

Feb 24 '06 #5
Thanks,
I just forgot the brackets in this code snippet..

Feb 24 '06 #6
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

Feb 24 '06 #7

"ChristophK" <ck***@gmx.de> schrieb im Newsbeitrag
news:11**********************@j33g2000cwa.googlegr 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.
Feb 24 '06 #8
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

Feb 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
4
by: Joel | last post by:
I have this bug that quite puzzled me. Basically I am having a segmentation fault on deleting an object, which belongs to a class which is the result of multiple inheritance from two other classes....
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
36
by: Ajay | last post by:
Hi All, I got a segmentation fault while accessing a structure element. I pasted the output from gdb and the backtrace. However if I am able successfully access the structure using the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.