473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

local variables deleted by whome ?

consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .

Regards
Mangesh .

May 1 '06 #1
6 2167
mangesh wrote:
consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .

Regards
Mangesh .


In almost all machines local variables are stored in the machine's
stack. This is the same stack that stores the return address that a
function uses to return to its caller. The local variables are
"deleted" by changing the stack pointer. Both allocation and deletion
are highly efficient operations (almost zero overhead) performed by
assembly language and are closely related and part of the call/return
assembly language.

--
Scott McPhillips [VC++ MVP]

May 1 '06 #2
mangesh wrote:
consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .

Regards
Mangesh .


Answer: the CPU.

Explanation:

1. Your compiler generates code that pops the stack frame.
2. Your OS loads your program (which obviously contains the code that
pops the stack frame)
3. When the execution goes out of scope, the CPU reads the instructions
that pops the stack frame and it does what it is told.

Regards,
Ben
May 1 '06 #3
mangesh wrote:
consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .


Well, you've missed a candidate, and it's the one that does the
work. It's your program.

The compiler creates your program. The compiler is not necessarily
even loaded when your program runs. In fact, the compiler might not
be on the same machine. Hey, in theory, the compiler might have
been completely erased from all machines everywhere, not even a
backup existing, when your program runs. (Though I suppose that
does not happen all that often.)

The OS does not know about the variables in your program. Nor care.
It grabs your program out of storage and sticks it in the part of
memory that contains programs while they execute, then passes
control to your program. And that's just about it while your prog
is running. There may be some other services like disk access or
swapping and so on. But the OS does not know about variables.

For simple variables like ints, all your program does to delete the
variable is change the stack pointer so that the variable is no
longer in the part of the stack your program expects to be data.
For variables that are instances of a class with a destructor,
your program calls the dtor for that object before it changes
the stack pointer. All that goes on "behind the scenes" where
you don't have to pay attention to it. Thus such variables are
called "automatic variables." It is part of your program, in stuff
created by the compiler. But the work is done by your program.
Socks

May 1 '06 #4

"mangesh" <ma************@walla.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .


It's deleted by code created by the compiler;
this code is part of your program.

-Mike
May 1 '06 #5
mangesh wrote:
consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .

Regards
Mangesh .

I would like to say that a's lifetime ends. Delete implies
that somehow the delete operator applied. As for what is
responsible for calling the destructor and deallocating the
storage, the language just says it's the "implementation" which
is the environment that the C++ program exists in.

In most cases the compiler emits code that in conjuction with
runtime libraries handles the deallocation.
May 1 '06 #6

"mangesh" <ma************@walla.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
consider following code :

void fun()
{
int a ;
//..do something
}

when function exit , a is said to be deleted . My confusion is who
deletes a , compiler or os . And if by os which part of os .

Regards
Mangesh .


fun() has a scope, just like any other function. Any object that is created
on the stack here has local scope and a strictly defined lifetime (governed
by the function's scope). Who deletes int a is the program since it invokes
the integer destructor when the function scope ends. What destroys the
integer is its own destructor - in this case: the d~tor in a
compiler-provided primitive type.

Why not create a class that displays it all for you:

#include <iostream>
#include <ostream>

class Temp
{
public:
Temp() { std::cout << "Temp()\n"; }
~Temp() { std::cout << "~Temp()\n"; }
};

void fun()
{
Temp fun_temp;
} // fun_temp's d~tor is invoked here

int main()
{
Temp main_temp;
fun();
} // main_temp d~tor is invoked here

Think of each function as a distinct stack. Terminating each function
unwinds its own stack.

Note: you don't call the d~tor(s), these get invoked as part of the stack
unwind. This happens naturally in C++ unless you stored the object(s) on the
heap.

May 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: marc | last post by:
I want to store the link of the page that each user of my web site visited so it can help me to personalise information for each user depending on the pages visited. I don''t know what kind of...
3
by: ambar.shome | last post by:
i have a function as listed below: char* ltoa(char* chr) { char* myChr=new char; strcpy(myChr,chr); return myChr; } in the above code i am returning a reference or pointer to a local
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
dmjpro
by: dmjpro | last post by:
the web-container started by whome ???? i mean by OS or JVM ??? plz help me .... thanxxx
8
by: Samant.Trupti | last post by:
Hi All, I am facing a strange problem.. I am calling a function func2 from func1. Before calling func2 all the local variables in func1 looks fine and has their respective values. When...
7
by: pereges | last post by:
In a recursive program, do you think it is necessary to add the static keyword for every variable or do you think it is unnecessary ? Is every call associated with seperate copy of variables ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.