Function returns pointer to dynamic array 
Author Message
 Function returns pointer to dynamic array

I have a function (a dll) that returns a pointer to a dyanmic array of
structures.  This routine is called again and again.

Since the memory for the pointer is allocated inside the function, when do I
deallocate the memory.

Obviously, I can not delete before I leave the function.

Does the calling program have to deallocate it, or should I attempt to
deallocate it upon entering the function before reallocating it.

Thanks,
Rich



Sat, 28 Jun 2003 20:15:58 GMT  
 Function returns pointer to dynamic array

Quote:

> I have a function (a dll) that returns a pointer to a dyanmic array of
> structures.  This routine is called again and again.

> Since the memory for the pointer is allocated inside the function, when do I
> deallocate the memory.

> Obviously, I can not delete before I leave the function.

> Does the calling program have to deallocate it, or should I attempt to
> deallocate it upon entering the function before reallocating it.

If it is in a dll, you want to make sure that the same dll is where
the memory gets deallocated.  Otherwise you may mismatch memory
managers and cause serious problems.

So for example, if your function (in a dll) is called allocate_array(), it
would be VERY wise of you to create a matching function called
deallocate_array(Array *ptr).

That way a user can load your dll, obtain an allocation from your dll,
then can give it back to your dll to free when it's finished.  It's
important to make symmetric interfaces.

Also, you might also consider writing a class to encapsulate the
pointer, such that its destructor will automatically call back into
the dll to free the allocation for you.  This way the user is not
responsible for any memory management issues.  (It's generally not a
good idea to return raw pointers to allocated memory, because it's so
easy to leak.)

--
Chris



Sun, 29 Jun 2003 08:29:28 GMT  
 Function returns pointer to dynamic array

Quote:

> If it is in a dll, you want to make sure that the same dll is where
> the memory gets deallocated.  [...]

I think all your comments are good, but there is another approach that is
simpler: use the memory manager in a C runtime DLL. Of course, this has some
disadvantages as well, but doesn't require coding changes.


Sun, 29 Jun 2003 09:09:01 GMT  
 Function returns pointer to dynamic array
Thanks for the input,
  I will try to figure out how to create a class to encompass allocation and
deallocation of the pointer.

Thanks,
Rich

PS.  How is the destructor initiated in the calling program?  Is it called
when he deletes the pointer or the variable goes out of scope?


Quote:

> > I have a function (a dll) that returns a pointer to a dyanmic array of
> > structures.  This routine is called again and again.

> > Since the memory for the pointer is allocated inside the function, when
do I
> > deallocate the memory.

> > Obviously, I can not delete before I leave the function.

> > Does the calling program have to deallocate it, or should I attempt to
> > deallocate it upon entering the function before reallocating it.

> If it is in a dll, you want to make sure that the same dll is where
> the memory gets deallocated.  Otherwise you may mismatch memory
> managers and cause serious problems.

> So for example, if your function (in a dll) is called allocate_array(), it
> would be VERY wise of you to create a matching function called
> deallocate_array(Array *ptr).

> That way a user can load your dll, obtain an allocation from your dll,
> then can give it back to your dll to free when it's finished.  It's
> important to make symmetric interfaces.

> Also, you might also consider writing a class to encapsulate the
> pointer, such that its destructor will automatically call back into
> the dll to free the allocation for you.  This way the user is not
> responsible for any memory management issues.  (It's generally not a
> good idea to return raw pointers to allocated memory, because it's so
> easy to leak.)

> --
> Chris



Sun, 29 Jun 2003 15:01:14 GMT  
 Function returns pointer to dynamic array
Don't even know where to begin to investigate this option, where would I
start to look?

Rich


Quote:



> > If it is in a dll, you want to make sure that the same dll is where
> > the memory gets deallocated.  [...]

> I think all your comments are good, but there is another approach that is
> simpler: use the memory manager in a C runtime DLL. Of course, this has
some
> disadvantages as well, but doesn't require coding changes.



Sun, 29 Jun 2003 15:51:50 GMT  
 Function returns pointer to dynamic array

Quote:

> Don't even know where to begin to investigate this option, where would I
> start to look?

> Rich

Compiler options MD, MDd

Sergei

Quote:






> > > If it is in a dll, you want to make sure that the same dll is where
> > > the memory gets deallocated.  [...]

> > I think all your comments are good, but there is another approach that is
> > simpler: use the memory manager in a C runtime DLL. Of course, this has
> some
> > disadvantages as well, but doesn't require coding changes.



Sun, 29 Jun 2003 21:08:35 GMT  
 Function returns pointer to dynamic array

Quote:
> Don't even know where to begin to investigate this option, where would I
> start to look?

Look in Settings | C/C++ | Code Generation | Use run-time library, and select a
DLL library.

The only real disadvantage of this approach is if you're distributing your
programs. You'll have to make sure you distribute the DLL as well.



Mon, 30 Jun 2003 02:50:08 GMT  
 Function returns pointer to dynamic array

Quote:

> Thanks for the input,
>   I will try to figure out how to create a class to encompass allocation and
> deallocation of the pointer.

> Thanks,
> Rich

> PS.  How is the destructor initiated in the calling program?  Is it called
> when he deletes the pointer or the variable goes out of scope?

The way I would do it, the function returns a simple object by value
that contains the pointer.  The object's destructor will send the
pointer back to your dll's "release" function.  That way if the caller
doesn't store the return value, there is no leak.  This would be
similar to an auto_ptr, but the means it uses to clean up is
different.

--
Chris



Mon, 30 Jun 2003 21:37:04 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Pointers: return of pointer to array of pointers to main

3. Returning pointer to array of structure from function

4. Arrays, Functions, and Returning Pointers via Parameters

5. function returning pointer array

6. Pointers, Arrays, and function returns

7. Returning and manipulating array/pointer from function to main

8. Returning Int array pointers from functions

9. Question about signal()/pointers to functions that return pointers to functions

10. function pointers and function pointers array

11. Can function returning pointer return negative integers?

12. Returning Pointer To Pointer From Function

 

 
Powered by phpBB® Forum Software