Board index » cppbuilder » Pointers as function parameters

Pointers as function parameters


2004-10-15 07:39:05 AM
cppbuilder89
This is along the lines of my earlier post here but I wanted to clarify
something :
If I am passing a pointer (or array of pointers) to a function, Which
pointers do I need to deallocate after use?
I understand currently that if I create the pointer in one unit then I
deallocate it in the same unit (or function), but what
about when this pointer is passed on to a function(in another unit) as a
parameter. Since there is no allocation of the pointer in that function
there is no need for deallocation in that function either (??)
Does it make a difference if I pass the pointer by value or by reference
when it comes to deallocating the pointer?
 
 

Re:Pointers as function parameters

"Thomas" < XXXX@XXXXX.COM >wrote in message
Quote
If I am passing a pointer (or array of pointers) to a function, Which
pointers do I need to deallocate after use?
Can you rephrase your question? It's not clear what you mean since I can
pass pointer just for fun of it and no deallocation needed:
char *Increase( char *_pcDummy )
{
return( _pcDummy + 1 );
}
On the other hand, I can pass pointer to free and it will deallocate memory
allocated with another function malloc.
Quote
I understand currently that if I create the pointer in one unit then I
deallocate it in the same unit (or function), but what
about when this pointer is passed on to a function(in another unit) as a
parameter. Since there is no allocation of the pointer in that function
there is no need for deallocation in that function either (??)
By "unit" you mean a compilation module, i.e. .cpp file? Or a code module,
i.e. DLL/EXE?
You can allocate and free memory in different modules in both cases though.
with some care if you do it accross DLL/EXE module boundaries.
Quote
Does it make a difference if I pass the pointer by value or by reference
when it comes to deallocating the pointer?
It does not matter, but why would you do this? Pointer takes 4 bytes and
reference is essentually is a pointer to an object, so it takes 4 bytes as
well. In your case using reference to a pointer you'll pay the price of...
hmm... someone correct me if I'm wrong... One LEA at function call and one
MOV per use of reference.
Using references makes sense when you want function to look and behave like
if it takes an object while saving time for creating a temp object at
function call (when object is big, say, some class ):.
Consider:
class CBar
{
public :
unsigned int uiPar[ 8192 ];
};
void Foo1( CBar _clDummy )//by value
{
unsigned int a = _clDummy.uiPar[ 7 ];
}
void Foo2( CBar &_rclDummy )// by reference
{
unsigned int a = _rclDummy.uiPar[ 7 ];
}
CBar bar;
Foo1( bar );
Foo2( bar );
Calls look the same, but for Foo1 compiler will actually create copy of
_clDummy (which is 8193 bytes ) on Foo1's stack. For Foo2 it will create a
reference on stack ( reference is 4 bytes ). Basically, never pass anything
that is not a pre-defined type by value. Use either pointer( clearer and
better way, my opinion) or reference. Another thing with references is that
if formal types of argument and reference do not match but can be converted
one to another, compiler will create temp object of needed type, initialize
it with given object, initialize reference with temp object, pass reference
to the function and then delete temp object at function exit. It does so
quetly, so sometimes it's hard to catch and those temp object can be big. So
my advice, use pointers.
Azazello.
 

Re:Pointers as function parameters

"Thomas" < XXXX@XXXXX.COM >writes:
Quote
This is along the lines of my earlier post here but I wanted to clarify
something :

If I am passing a pointer (or array of pointers) to a function, Which
pointers do I need to deallocate after use?

I understand currently that if I create the pointer in one unit then I
deallocate it in the same unit (or function), but what
about when this pointer is passed on to a function(in another unit) as a
parameter. Since there is no allocation of the pointer in that function
there is no need for deallocation in that function either (??)

Does it make a difference if I pass the pointer by value or by reference
when it comes to deallocating the pointer?
Actually, it's not the pointer itself that determines how you need to
treat the memory. It's the object whose address the pointer holds
that matters. You can, for example, have a hundred pointers to the
same object. You still must only delete the object once, if it was
allocated with new. It does not matter which one of the hundred
pointers you use, just that you only use one, and after it's deleted,
no pointer that refers to that object is used (or else you have a
major problem.)
If you allocate an object and pass its address to another function in
another unit, then if that code deletes the pointer, it's generally
valid provided it's using the proper deallocation mechanism.
(new/delete, new[]/delete[], malloc/free, calloc/free)
The reason you want the same unit to manage the pointer is so that you
don't have to have code that "knows" how the pointer was allocated.
If you create an object that owns the pointer, and it judiciously
guards all access to it, then it can very easily know when to allocate
and delete the object that the pointer points to. Get sloppy with the
pointer, pass it around to a lot of places, and delete it in any of a
dozen places, and suddenly your code turns into a quagmire. Pointer
errors are one of the hardes things to debug. So this is a guideline
rather than a hard and fast requirement. You can make a program work
with really sloppy pointer handling, but maintaining such a program is
a nightmare.
--
Chris (TeamB);
 

{smallsort}