What is the return value of this function

A

andylcx

Hi all, I wrote a class, but met some exception when I call its member
function. How to fix it? Thanks. My code is shown below:

class Myclass
{
public:
Myclass();
~Myclass();
double *showdata();

private:
double *data;
};

double *Myclass::showdata()
{
for(int i=0;i<10;i++)
data=double (i);
return data;
}

My main function
#include <...>
int main()
{
Myclass *newclass = new Myclass;
newclass->showdata();//Here is the problem. what is the return value
delete newclass;
}
 
D

ddh

You just havn't allocate memory for your pointer data.

The class should be:
class MyClass
{
private:
double *data;

public:
MyClass()
{
data = NULL;
}

~MyClass()
{
if (data)
delete []data, data = NULL;
}

double *showData()
{
if (data)
delete []data;
data = new double[10];
if (!data)
return NULL;

for (int i = 0; i < 10; ++ i)
data = double(i);
return data;
}
};
 
J

Jaspreet

Hi all, I wrote a class, but met some exception when I call its member
function. How to fix it? Thanks. My code is shown below:

class Myclass
{
public:
Myclass();
~Myclass();
double *showdata();

private:
double *data;
};

You seem to have forgotten to give the definition of the ctor and the
dtor. Assuming you have a blank ctor and dtor. Lets move ahead.
double *Myclass::showdata()
{

Memory has to be allocated for data. I would write:
data = new double[10];
for(int i=0;i<10;i++)
data=double (i);
return data;
}

My main function
#include <...>
int main()
{
Myclass *newclass = new Myclass;
newclass->showdata();//Here is the problem. what is the return value


The above statement causes seg fault because you forgot to allocate
memory to a pointer in showdata(). BTW, why do you need to return the
doubel array.

You could write another accessor function or use the same function to
display the values.
delete newclass;

Similar to this statement yo also need to de-allocate memory for data
by writing
delete []data;
in your showdata() function.

thanks!! and have a nice day!!
jaspreet
 
R

Rolf Magnus

ddh said:
You just havn't allocate memory for your pointer data.

The class should be:
class MyClass
{
private:
double *data;

public:
MyClass()
{
data = NULL;
}

Prefer initialization over assignment:

MyClass()
: data(0)
{
}
~MyClass()
{
if (data)

No need for the if. Delete[] on a null pointer is a no-op anyway.
delete []data, data = NULL;
}

double *showData()
{
if (data)
delete []data;
data = new double[10];
if (!data)
return NULL;

No need for that check. new[] never returns a null pointer. When out of
memory, new[] throws an exception, which you actually don't handle
correctly.
for (int i = 0; i < 10; ++ i)
data = double(i);
return data;
}
};
 
I

Ian

Hi all, I wrote a class, but met some exception when I call its member
function. How to fix it? Thanks. My code is shown below:

class Myclass
{
public:
Myclass();
~Myclass();
double *showdata();

private:
double *data;
};

double *Myclass::showdata()
{
for(int i=0;i<10;i++)
data=double (i);
return data;
}

Where are you initialising data?

Ian
 

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

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top