Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2009, 12:12 PM
Authorized User
 
Join Date: May 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Default autmatic storage duration

Consider the following:
int main(){
int x=4;
funcy(x);
return 0;
}
void funcy(int& y){
cout<<y;
}

now, y stores the address of x. after funcy will be executed, y will be destroyed. Wont this also clear the memory occupied by x as all changes to y are mapped to x??


Thanks
 
Old June 10th, 2009, 11:37 PM
Registered User
 
Join Date: May 2009
Posts: 5
Thanks: 0
Thanked 3 Times in 3 Posts
Default autmatic storage duration

sudhanshu631,

No. When the function call is made, only a copy of the address is passed to funcy(). When 'y' is destroyed, the information it contains (which is the address of x) may be lost but noting gets cleared as a result of this loss. You can test this fairly quickly by displaying the value of x before -- and -- after the call to to funcy(). Of course, if funcy() modifies y (which actually contains the address of x) then x will be modified. Whatever value funcy() assigns to to y will remain that way after the function completes and y is destroyed.

As a side note, depending on the compiler, there is a good chance 'y' will exist only on the stack. It may never exist in code space. Thus when main() calls funcy() the address of x is placed on the stack. Funcy() uses this stack location on every access to y. It may never get copied from the stack to a new location. When funcy() is finished, this address for x is popped off the stack but it is not used. Rather, the memory claimed by the stack is simply freeded and can be reused next time something else must be pushed onto the stack. The stack location which contained y (address of x) will remain unchanged until the next function call when something else is placed on the stack. When this memory location on the stack is later overwritten, only copy of address of x is destroyed, not the actual memory location containing x.

Ken





Similar Threads
Thread Thread Starter Forum Replies Last Post
days-from-duration Dinghus XSLT 1 November 21st, 2007 04:05 AM
seconds-from-duration outspaced XSLT 2 April 3rd, 2007 08:51 AM
date duration urgent rjkulkarni2001 ASP.NET 2.0 Professional 0 January 9th, 2006 06:11 AM
duration between dates rjkulkarni2001 Classic ASP Basics 1 January 5th, 2006 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.