Re: allocating array in function
From: modemer (me_at_privacy.net.invalid)
Date: 03/15/05
- Next message: modemer: "Re: What's difference between f(const MyClass & in) and f(MyClass in)"
- Previous message: Shezan Baig: "Re: What's difference between f(const MyClass & in) and f(MyClass in)"
- In reply to: Henrietta Denoue: "allocating array in function"
- Next in thread: Rolf Magnus: "Re: allocating array in function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 15 Mar 2005 14:28:49 -0500
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" <henrietta@netcalcul.fr> wrote in message
news:d17bd8$scj$1@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.
>
- Next message: modemer: "Re: What's difference between f(const MyClass & in) and f(MyClass in)"
- Previous message: Shezan Baig: "Re: What's difference between f(const MyClass & in) and f(MyClass in)"
- In reply to: Henrietta Denoue: "allocating array in function"
- Next in thread: Rolf Magnus: "Re: allocating array in function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|