scope in functions
-
Let me explain what happens.
int x = 6;
creates a int variable on the stack and sets it to the value 6int * ptr = &x;
creates a pointer to integer variable on the stack and sets the value to the stack address where x is located.return ptr;
retuns to the calling function lowering the stack pointer to where it was before calling the function. and returns the value of the pointer. The pointer is now addressing somewhere in the unused stack space. The values of the stack remain there until the are overwritten/needed for other variables, or other function calls. In other words this is not a good practice and should never be used. I don't know what you mean about the copy constructor, since int isn't a class so it doesn't have a copy constructor in the way you think.codito ergo sum
Hi BadKarma Thanks for that. Yes I thought it was bad practice to return an object on the stack in a function call. With regards to the copy constructor, if I have a class alpha in the function
alpha * func() { alpha anything; alpha *ptr = &anything; return ptr; }
how would it return ptr. Would it copy the pointer ptr using the copy constructor and then destroy it as the function goes out of scope? Thanks. -
Hi BadKarma Thanks for that. Yes I thought it was bad practice to return an object on the stack in a function call. With regards to the copy constructor, if I have a class alpha in the function
alpha * func() { alpha anything; alpha *ptr = &anything; return ptr; }
how would it return ptr. Would it copy the pointer ptr using the copy constructor and then destroy it as the function goes out of scope? Thanks.you're doing the exactly same mistake... your object "anything", whether it's an int or an alpha, is local to the function, so destroyed when getting out of space...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
you're doing the exactly same mistake... your object "anything", whether it's an int or an alpha, is local to the function, so destroyed when getting out of space...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi ya I know about the mistake of the object on the stack going out of scope. I was questioning how it returns the pointer. It just copies the address and does not involve using copy constructors? Is that right ? Thanks.
you're confusing about the object life, and the returning stuff. there's no copy constructors (actually, not even basic constructors at all) for pointers...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi ya I know about the mistake of the object on the stack going out of scope. I was questioning how it returns the pointer. It just copies the address and does not involve using copy constructors? Is that right ? Thanks.
minkowski wrote:
It just copies the address and does not involve using copy constructors?
Yes, no copy constructor are involved. In fact, no constructor at all will be called, because you don't construct a new object. You only delete an object (when the function goes out of scope) and then return its address. Which is of course very very bad :).
Cédric Moonen Software developer
Charting control [v1.2 - Updated] -
minkowski wrote:
It just copies the address and does not involve using copy constructors?
Yes, no copy constructor are involved. In fact, no constructor at all will be called, because you don't construct a new object. You only delete an object (when the function goes out of scope) and then return its address. Which is of course very very bad :).
Cédric Moonen Software developer
Charting control [v1.2 - Updated] -
Hi CPallini, Yes I agree with that and I was wondering why it still works. How is the pointer returned? Is it copied with a copy constructor? Thanks.
-
Hi BadKarma Thanks for that. Yes I thought it was bad practice to return an object on the stack in a function call. With regards to the copy constructor, if I have a class alpha in the function
alpha * func() { alpha anything; alpha *ptr = &anything; return ptr; }
how would it return ptr. Would it copy the pointer ptr using the copy constructor and then destroy it as the function goes out of scope? Thanks.minkowski wrote:
alpha * func()
{
alpha anything;
alpha *ptr = &anything;
return ptr;}
how would it return ptr. Would it copy the pointer ptr using the copy constructor and then destroy it as the function goes out of scope?
The type you return is pointer. In this case, pointer to alpha. And pointers do not have such things as copy constructors. So, no copy c'tor is called.
Failure is not an option - it's built right in.
-
As toxcct pointed out, it's returned by value. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
and as an idiot is roaming over here, all my posts today got voted 1 or 2 :suss:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
and as an idiot is roaming over here, all my posts today got voted 1 or 2 :suss:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Simply don't worry about. Helping people it's more rewarding than getting high scores; unfortunately, idiots can be found everywhere, even here. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.