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

allocating array in function



Hi

I am new to C++ and suffering from dynamic allocation.
I have a function that returns an integer pointer and
want to assign this to another pointer:
class cssReader
{
public:
cssReader();
cssReader(string, string);
~cssReader();

public:

int* GetWandRead(string, int);

public:
int *d;

.....'...

}

int* cssReader::GetWandRead(string dirname, int rows)
{
.....
d = new int[duration*(int)samprate];

// varibles duration and samprate are changing
// thats why I have to dynamically allocate my 'd'.
.....

return d;

}
using the above:
int *data;
ccReader *css = new cssReader;
void getData()
{

....
....
data = css->GetWandRead(dirname, rows);

// plot data

delete [] data;
}
first sorry for the short version of the code. It was thought
non essential.

The above sometimes works and most of the time doesn't. If I
remove the last 'delete' statement it works but having large
amount of data the memory leak is huge.
I guess my question is: Is it ok to allocate an array inside
a function, delete it outside the function and reallocate
and delete and reallocate and delete ...
Thanks in advance

H.

Jul 23 '05 #1
5 1902
What do you mean "doesn't work"?
If you mean that the allocated memory can't be freed every time in a loop, I
guess your coding has some problem to forget free the array.

Memory (including array) could be allocated(new or malloc) in one place, and
freed in other place because it's located in heap.

"Henrietta Denoue" <he*******@netcalcul.fr> wrote in message
news:d1**********@dolly.uninett.no...


Hi

I am new to C++ and suffering from dynamic allocation.
I have a function that returns an integer pointer and
want to assign this to another pointer:
class cssReader
{
public:
cssReader();
cssReader(string, string);
~cssReader();

public:

int* GetWandRead(string, int);

public:
int *d;

....'...

}

int* cssReader::GetWandRead(string dirname, int rows)
{
....
d = new int[duration*(int)samprate];

// varibles duration and samprate are changing
// thats why I have to dynamically allocate my 'd'.
....

return d;

}
using the above:
int *data;
ccReader *css = new cssReader;
void getData()
{

...
...
data = css->GetWandRead(dirname, rows);

// plot data

delete [] data;
}
first sorry for the short version of the code. It was thought
non essential.

The above sometimes works and most of the time doesn't. If I
remove the last 'delete' statement it works but having large
amount of data the memory leak is huge.
I guess my question is: Is it ok to allocate an array inside
a function, delete it outside the function and reallocate
and delete and reallocate and delete ...
Thanks in advance

H.

Jul 23 '05 #2
Henrietta Denoue wrote:


Hi

I am new to C++ and suffering from dynamic allocation.
I have a function that returns an integer pointer and
want to assign this to another pointer:
class cssReader
{
public:
cssReader();
cssReader(string, string);
~cssReader();

public:

int* GetWandRead(string, int);

public:
int *d;

....'...

}

int* cssReader::GetWandRead(string dirname, int rows)
{
....
d = new int[duration*(int)samprate];

// varibles duration and samprate are changing
// thats why I have to dynamically allocate my 'd'.
....

return d;

}
using the above:
int *data;
ccReader *css = new cssReader;
void getData()
{

...
...
data = css->GetWandRead(dirname, rows);

// plot data

delete [] data;
}
first sorry for the short version of the code. It was thought
non essential.

The above sometimes works and most of the time doesn't.
What do you mean by that? How does it not work? What does it do instead?
If I remove the last 'delete' statement it works but having large
amount of data the memory leak is huge.
I guess my question is: Is it ok to allocate an array inside
a function, delete it outside the function and reallocate
and delete and reallocate and delete ...


Yes, it is. One possible reason for problems is that you don't obey the Rule
of Three. If you need one of a copy constructor, assignment operator or
destructor, you almost certainly need all three of them. If you happen to
copy one of your cssReader objects anywhere in your program, you'll get
nasty double deletion errors.

Jul 23 '05 #3
The fact that d is a class member instead of a local variable to
GetWandRead raises a red flag to me... Is d used anywhere else? And if
not, does the problem go away if you make it a local variable?

I don't really see any reason why it shouldn't work to tell you the
truth. I'm not a C++ guru or anything, but I'm pretty sure that what
you have should work. So I suspect the problem is somewhere else.

Jul 23 '05 #4
Henrietta Denoue <he*******@netcalcul.fr> wrote in message news:<d1**********@dolly.uninett.no>...
Hi

I am new to C++ and suffering from dynamic allocation.
I have a function that returns an integer pointer and
want to assign this to another pointer:
first sorry for the short version of the code. It was thought
non essential.

The above sometimes works and most of the time doesn't. If I
remove the last 'delete' statement it works but having large
amount of data the memory leak is huge.
I guess my question is: Is it ok to allocate an array inside
a function, delete it outside the function and reallocate
and delete and reallocate and delete ...
Thanks in advance

H.


One Question.
I just want to know whether your app is multithread apps or not?
If it is, Due to the fact that your GetData function is not thread-safe
function. It could be crashed.

Regards,
Jul 23 '05 #5
Henrietta Denoue wrote:

Hi

I am new to C++ and suffering from dynamic allocation.
I have a function that returns an integer pointer and
want to assign this to another pointer:

class cssReader
{
public:
cssReader();
cssReader(string, string);
~cssReader();
What does the destructor do?
Does it delete[] the allocated memory?

int *data;
ccReader *css = new cssReader;
void getData()
{

...
...

data = css->GetWandRead(dirname, rows);

// plot data

delete [] data;


That's not a good idea.
You should get into the habit of:
"The object (or function) that allocated the data is
reponsible for freeing it."

That's a simple rule and there are exceptions to it, but as a rule
of thumb it works pretty well. In your example it means, since the
cssReader object allocated the memory, it is responsible for deleting
it. Not the code that created the cssReader object, not some obscure
function, nothing else but the cssReader object itself. If you keep
the code environment dealing with managing that memory small, it is much
easier to find bugs and code correctly as if some function allocates some
memory and 15000 lines of code later in some other function the deallocation
happens.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

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

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
3
by: Erik S. Bartul | last post by:
lets say i want to fill up a multidimentional array, but i wish to allocate memory for it on the fly. i assume i declare, char **a; but how do i allocate memory for the pointers, so i can...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
5
by: Johnathan Doe | last post by:
Why is is necessary to write a function that allocates memory for, or changes, a pointer to a char array using char** instead of char*? E.g. void modify_string( char** string ) { if( string...
10
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr =...
1
by: Peter Oliphant | last post by:
I'm using VS C++.NET 2005 Express in /clr mode. Say I have a PointF array: array<PointF>^ point_array = gcnew array<PointF>(100) ; Now I want to re-allocate point_array, and in doing so also...
4
by: Rakesh Kumar | last post by:
Hi All - In a project of mine - I was trying to scale down the actual issue to the following piece of code. I need to allocate an array of strings and reserve the individual string to a particular...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
13
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.