Re: abt variable storage like global static,auto ....
- From: "dragoncoder" <pktiwary@xxxxxxxxx>
- Date: 26 May 2005 10:04:43 -0700
I don't think there are any rules about the storage of a variable
specified by the standard. What it says is that global and static
variables remain alive for the life of the program.They are initialised
before main() is called and get destroyed at the end. Implementations
use something called as the static memory to implement that.
Local variables are created where they are defined and gets destroyed
when they go out of scope. An implementation may use stack to realise
this. Talking about your code:
>#include <string.h>
>int global;
Global variable created at start and automatically initialized with 0
> main()
main() always has a return type int and nothing else.
int main()
>{
> int a ;
> int b;
2 local variables defined, created after main in called, uninitialized.
DANGER!!!
> increment(a);
You are trying to pass the value of a which is not known to a function,
Invoking undefined behaviour. Anyhing can happen from "looks like
working" to "formatting your hard disk".
>}
a and b get out of scope, destroyed.
>
>increment(int a)
A function taking argument by value. A copy of a will be passed.
functions missing return type anything and not declared, dafaults int.
You should always provide declaration for function if you don't want
your life to be miserable.
argument a is local to the function.
>{
> a++;
Value of a is not known, any operation on it leads to UB.
something happened with a, don't know.
>}
a goes out of scope, so destroyed, any changes to a are gone.
/PT
.
- References:
- Prev by Date: Re: Undefined reference to '...' error under gcc
- Next by Date: Re: using "purify" to check memory leaks
- Previous by thread: abt variable storage like global static,auto ....
- Next by thread: Re: abt variable storage like global static,auto ....
- Index(es):
Relevant Pages
|