473,396 Members | 1,924 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,396 software developers and data experts.

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;
}

Jul 23 '05 #1
4 1170
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[i] = double(i);
return data;
}
};

Jul 23 '05 #2
an*****@hotmail.com wrote:
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[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
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 }


Jul 23 '05 #3
ddh wrote:

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


Jul 23 '05 #4
Ian
an*****@hotmail.com wrote:
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;
}

Where are you initialising data?

Ian
Jul 23 '05 #5

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

Similar topics

26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
9
by: datastructure | last post by:
Copyright (c) 2003 by James J. Perry. All Rights Reserved. char Square::validList = {'r', 'g', 'b'}; //missing an element, is 0 int Square::numValues = 3; Square::Square() { value =...
7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
2
by: Sam | last post by:
In our C++ program, we are using the system call to execute another C++ program synchronously. The program executed by system runs without error and returns back a 0. Under conditions we cannot...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
4
by: Jarod_24 | last post by:
What is the point with Delegates in VB.Net What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on... I'd like a discussion on this,...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
4
by: brian | last post by:
i broke down where i think the problems areas would be. any help would be greatly appreciated. where the file is called <script type="text/javascript" src="k.js"> </script> the beginning...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.