Re: Pointer to the out of scope local variables
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Tue, 13 Nov 2007 08:48:01 -0600
"Stuart Redmann" <DerTopper@xxxxxx> wrote in message
news:fhbu1c$lm4$1@xxxxxxxxxxxxxxx
Ulrich Eckhardt wrote:
K?r?at wrote:
As far as I know local variables are deleted when they go out of
scope.
They are destroyed, not deleted.
Well, but somtimes we need to return local variables address to the
caller.
I'd rather say no to that claim.
Is it valid to do so?
In pretty much any case the answer is "no", without even looking at the
code. Anyway, let's see...
struct 3D
{
int X;
int Y;
int Z;
};
No, not okay, '3D' is not a valid identifier, at least not in any of the
languages I would expect from the rest of the syntax. But well, let's
ignore this obvious error and assume it was a valid identifier.
3D * buildPoint (int nX, int nY, int nZ)
{
3D tmp;
tmp.X = nX;
tmp.Y = nY;
tmp.Z = nZ;
return &tmp;
}
1. You can return objects from functions.
2. Your function seems to do what C++ constructors are for.
3. You could allocate storage dynamically in a function and return a
pointer
to it if you don't want to copy the struct for whatever reason. This puts
the burden of cleanup on you though and generally you don't want that.
I think Ulrich had only had a quick look at the code so that he missed the
main error of it. It may be the case that a very experienced C++
programmer develops a kind of 'black spot' for the most fundamental
errors. Thus he probably saw the signature of buildPoint, saw the return
of a pointer, and jumped to the conclusion that you allocated the memory
for the return value _dynamically_. As
AFAICT, he already responded to that earlier in the post (it is never
acceptable to return a pointer to a local variable), so he started offering
alternatives.
this would be only bad style, the code you have actually posted is plainly
broken: You return a pointer that is not valid outside the function call
(any decent compiler would at least produce a warning). As the variable
'tmp' goes out of scope at the end of buildPoint, you must no longer use
it.
There are two alternatives: 1) Return a pointer to a dynamically allocated
object (using operator new), or 2) make the return type a non-pointer.
Alternative 2) is better than alternative 1) as alternative one violates
the informal rule that whoever allocates memory is responsible for
deleting it. In this particular case the funtion buildPoint would allocate
the memory but the calling function had to free the memory. If the caller
forgets to do so, you'll get a memory leak.
Regards,
Stuart
.
- Follow-Ups:
- Re: Pointer to the out of scope local variables
- From: Stuart Redmann
- Re: Pointer to the out of scope local variables
- References:
- Pointer to the out of scope local variables
- From: Kürşat
- Re: Pointer to the out of scope local variables
- From: Ulrich Eckhardt
- Re: Pointer to the out of scope local variables
- From: Stuart Redmann
- Pointer to the out of scope local variables
- Prev by Date: Re: Pointer to the out of scope local variables
- Next by Date: Re: recognize .Net controls properly and get infos/styles/properties
- Previous by thread: Re: Pointer to the out of scope local variables
- Next by thread: Re: Pointer to the out of scope local variables
- Index(es):
Relevant Pages
|