Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 06:01 AM
andylcx@hotmail.com
Guest
 
Posts: n/a
Default What is the return value of this function

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[i]=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;
}

  #2  
Old July 23rd, 2005, 06:01 AM
ddh
Guest
 
Posts: n/a
Default Re: What is the return value of this function


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[i] = double(i);
return data;
}
};

  #3  
Old July 23rd, 2005, 06:01 AM
Jaspreet
Guest
 
Posts: n/a
Default Re: What is the return value of this function

andylcx@hotmail.com wrote:[color=blue]
> 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;
> };
>[/color]

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.
[color=blue]
> double *Myclass::showdata()
> {[/color]

Memory has to be allocated for data. I would write:
data = new double[10];
[color=blue]
> for(int i=0;i<10;i++)
> data[i]=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[/color]

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.
[color=blue]
> delete newclass;[/color]

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[color=blue]
> }[/color]

  #4  
Old July 23rd, 2005, 06:01 AM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: What is the return value of this function

ddh wrote:
[color=blue]
>
> You just havn't allocate memory for your pointer data.
>
> The class should be:
> class MyClass
> {
> private:
> double *data;
>
> public:
> MyClass()
> {
> data = NULL;
> }[/color]

Prefer initialization over assignment:

MyClass()
: data(0)
{
}
[color=blue]
> ~MyClass()
> {
> if (data)[/color]

No need for the if. Delete[] on a null pointer is a no-op anyway.
[color=blue]
> delete []data, data = NULL;
> }
>
> double *showData()
> {
> if (data)
> delete []data;
> data = new double[10];
> if (!data)
> return NULL;[/color]

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.
[color=blue]
> for (int i = 0; i < 10; ++ i)
> data[i] = double(i);
> return data;
> }
> };[/color]

  #5  
Old July 23rd, 2005, 06:01 AM
Ian
Guest
 
Posts: n/a
Default Re: What is the return value of this function

andylcx@hotmail.com wrote:[color=blue]
> 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[i]=double (i);
> return data;
> }
>[/color]
Where are you initialising data?

Ian
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles