allocating memory in one function and freeing it with another function 
Author Message
 allocating memory in one function and freeing it with another function

I am using C++ V 6.
I would like to allocate memory for image storage in one function,
manipulate the image data in the allocated memory using other functions,
then finally free the memory with a different function.  Is this possible,
and what is the best way to do it?

Thanks for the help.
Bob Linford



Tue, 05 Oct 2004 10:45:26 GMT  
 allocating memory in one function and freeing it with another function
I think you can encapsulate a Image class for the features. Provide
the image manipulate function as public method, and add a Create()
method for allocate memory and free the memory in constructor.
Quote:

> I am using C++ V 6.
> I would like to allocate memory for image storage in one function,
> manipulate the image data in the allocated memory using other functions,
> then finally free the memory with a different function.  Is this possible,
> and what is the best way to do it?

> Thanks for the help.
> Bob Linford



Tue, 05 Oct 2004 13:39:41 GMT  
 allocating memory in one function and freeing it with another function

Quote:

> I am using C++ V 6.
> I would like to allocate memory for image storage in one function,
> manipulate the image data in the allocated memory using other functions,
> then finally free the memory with a different function.  Is this possible,
> and what is the best way to do it?

It is possible, as long as the two functions don't reside in different
libraries with different heaps (they can be in different libraries as
long as both use the multithreaded DLL CRT).

The best way to do it is very carefully.  Manual memory management is
error-prone and difficult to make exception-safe.  As David suggests,
you may want to consider using a class.  I don't agree with his
suggestion to use a Create function, though, as a constructor should
be adequate.  Depending on your usage, you may be better to just use
something simple like std::auto_ptr.



Tue, 05 Oct 2004 21:21:35 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Freeing memory allocated inside a function

2. allocating memory with functions

3. Allocating memory inside a function

4. allocating memory and processing thru function.

5. Do the string functions allocate memory?

6. standard variadic functions allocate memory?

7. getline function that allocates memory on-the-fly

8. allocate memory for function

9. allocate memory in a function

10. C functions that allocate memory

11. How to access memory allocated in function

12. Allocating memory for function argument

 

 
Powered by phpBB® Forum Software