Re: allocating array in function

From: modemer (me_at_privacy.net.invalid)
Date: 03/15/05


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.
>



Relevant Pages

  • Re: Referencing
    ... > by calling the 'new' operator I understand that the memory is allocated on ... > there any way to ensure that the allocated memory is actually 'inside' the ... If it's only a few elements in the array you could ... int SomeValue; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Avoid duplicating memory in mex-file?
    ... mexPutVariable to move the array to the global workspace. ... the amount of allocated memory is ... "out of memory" errors. ...
    (comp.soft-sys.matlab)
  • Re: allocate memory for pointer
    ... > I want to reallocate memory for an array. ... After this function a points to deleted memory. ... So it resolve the problem change reallocate to - ... int reallocateMemory(double *&array, int newsize) ...
    (comp.lang.cpp)
  • Re: Array valued function and memory
    ... function copies the array when the array is returned. ... > tab = 0.D0 ... > This memory is not freed after the function returned, and, apparently, not ... > It seems the function uses the already allocated memory which is enough ...
    (comp.lang.fortran)
  • Re: Intel Fortran Compiler Version 9.1 Now Available
    ... reallocate in clf in google groups shows many relevant hits. ... I missed the reallocate-on-assignment feature. ... Also, there can be a benefit of not having to copy data to enlarge an array, even if the subsequent memory is allocated, because the data can be moved in the virtual memory space by remapping the physical memory. ...
    (comp.lang.fortran)