Will a value live perpetually when there's no reference to it
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Suppose the following minimal codes:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %c", character);
I overwrote character within main
,
Then what happened to c
, will it be destroyed automatically or live in the memory perpetually.
The comment click me:
"variables in C are nothing but named chunks of memory. "
c
add a comment |Â
up vote
6
down vote
favorite
Suppose the following minimal codes:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %c", character);
I overwrote character within main
,
Then what happened to c
, will it be destroyed automatically or live in the memory perpetually.
The comment click me:
"variables in C are nothing but named chunks of memory. "
c
1
This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
â Eli Korvigo
1 hour ago
So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
â rider dragon
1 hour ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Suppose the following minimal codes:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %c", character);
I overwrote character within main
,
Then what happened to c
, will it be destroyed automatically or live in the memory perpetually.
The comment click me:
"variables in C are nothing but named chunks of memory. "
c
Suppose the following minimal codes:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %c", character);
I overwrote character within main
,
Then what happened to c
, will it be destroyed automatically or live in the memory perpetually.
The comment click me:
"variables in C are nothing but named chunks of memory. "
c
c
edited 1 hour ago
asked 1 hour ago
rider dragon
1916
1916
1
This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
â Eli Korvigo
1 hour ago
So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
â rider dragon
1 hour ago
add a comment |Â
1
This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
â Eli Korvigo
1 hour ago
So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
â rider dragon
1 hour ago
1
1
This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
â Eli Korvigo
1 hour ago
This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
â Eli Korvigo
1 hour ago
So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
â rider dragon
1 hour ago
So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
â rider dragon
1 hour ago
add a comment |Â
8 Answers
8
active
oldest
votes
up vote
6
down vote
accepted
By "shadowing" the global character
variable you are just hiding it from the main
function, but it will still be part of the program.
It will live on.
If the variable was declared as static
, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).
However, since the variable isn't declared as static
, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).
add a comment |Â
up vote
5
down vote
You have two separate variables named character
: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main
set to 'b', whose lifetime is that of its scope.
The definition of character
in main
masks the definition at file scope, so only the latter is accessible.
How about if declared asstatic char character = 'b
â rider dragon
1 hour ago
1
@riderdragon For the variable inmain
, thestatic
modifier gives it a lifetime of the whole program.
â dbush
1 hour ago
@riderdragon, thestatic
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
â Myst
1 hour ago
add a comment |Â
up vote
4
down vote
It will live on (till the program dies, as any static-storage variable would) and you can still get to it:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is '%c'n", character);
extern char character;
//in this scope, overrides the local `character` with
//the global (extern?) one
printf("The current value of head is '%c'n", character);
printf("The current value of head is '%c'n", character);
/*prints:
The current value of head is 'b'
The current value of head is 'c'
The current value of head is 'b'
*/
The local extern declaration doesn't work reliably/portably for static
globals, though you can still get to them through pointers or through a separate function.
(
Why isn't static char character='c'; int main() char character='b'; extern char character;
reliable with the global being static
?
6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).
6.2.2p4:
For an identifier declared with the storage-class specifier extern in
a scope in which a prior declaration of that identifier is visible,31)
if the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage.
My clang 6.0.0 is accepting it with a static char character='b';
but my gcc 7.3.0 isn't.
Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static
too.
)
1
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed tostatic char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules forextern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.
â Eric Postpischil
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
1
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
add a comment |Â
up vote
3
down vote
Complement to the other answers:
Try this and you'll understand:
#include <stdio.h>
char character = 'c';
void Check()
printf("Check: c = %cn", character);
int main(void)
char character = 'b';
printf("The current value of head is %cn", character);
Check();
1
Very elegant and usefull example.
â manoliar
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
add a comment |Â
up vote
1
down vote
Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.
But it could also depend on linker settings. Linker could optimize the unsused global variable out.
add a comment |Â
up vote
1
down vote
Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.
A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.
If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
add a comment |Â
up vote
1
down vote
After the declaration of character
in main
, any reference to character
in that function refers to that one, not the one at global scope. We call this shadowing.
As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to
#include <stdio.h>
int main()
printf("The current value of head is b");
for example.
add a comment |Â
up vote
1
down vote
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %cn", character);
printc();
void printc()
printf("%c", character);
It makes it all clear. It is not destroyed it is just shadowed.
Output:
The current value of head is b
c
add a comment |Â
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
By "shadowing" the global character
variable you are just hiding it from the main
function, but it will still be part of the program.
It will live on.
If the variable was declared as static
, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).
However, since the variable isn't declared as static
, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).
add a comment |Â
up vote
6
down vote
accepted
By "shadowing" the global character
variable you are just hiding it from the main
function, but it will still be part of the program.
It will live on.
If the variable was declared as static
, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).
However, since the variable isn't declared as static
, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
By "shadowing" the global character
variable you are just hiding it from the main
function, but it will still be part of the program.
It will live on.
If the variable was declared as static
, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).
However, since the variable isn't declared as static
, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).
By "shadowing" the global character
variable you are just hiding it from the main
function, but it will still be part of the program.
It will live on.
If the variable was declared as static
, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).
However, since the variable isn't declared as static
, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).
answered 1 hour ago
Myst
11.4k22241
11.4k22241
add a comment |Â
add a comment |Â
up vote
5
down vote
You have two separate variables named character
: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main
set to 'b', whose lifetime is that of its scope.
The definition of character
in main
masks the definition at file scope, so only the latter is accessible.
How about if declared asstatic char character = 'b
â rider dragon
1 hour ago
1
@riderdragon For the variable inmain
, thestatic
modifier gives it a lifetime of the whole program.
â dbush
1 hour ago
@riderdragon, thestatic
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
â Myst
1 hour ago
add a comment |Â
up vote
5
down vote
You have two separate variables named character
: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main
set to 'b', whose lifetime is that of its scope.
The definition of character
in main
masks the definition at file scope, so only the latter is accessible.
How about if declared asstatic char character = 'b
â rider dragon
1 hour ago
1
@riderdragon For the variable inmain
, thestatic
modifier gives it a lifetime of the whole program.
â dbush
1 hour ago
@riderdragon, thestatic
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
â Myst
1 hour ago
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You have two separate variables named character
: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main
set to 'b', whose lifetime is that of its scope.
The definition of character
in main
masks the definition at file scope, so only the latter is accessible.
You have two separate variables named character
: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main
set to 'b', whose lifetime is that of its scope.
The definition of character
in main
masks the definition at file scope, so only the latter is accessible.
answered 1 hour ago
dbush
85.2k1090122
85.2k1090122
How about if declared asstatic char character = 'b
â rider dragon
1 hour ago
1
@riderdragon For the variable inmain
, thestatic
modifier gives it a lifetime of the whole program.
â dbush
1 hour ago
@riderdragon, thestatic
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
â Myst
1 hour ago
add a comment |Â
How about if declared asstatic char character = 'b
â rider dragon
1 hour ago
1
@riderdragon For the variable inmain
, thestatic
modifier gives it a lifetime of the whole program.
â dbush
1 hour ago
@riderdragon, thestatic
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
â Myst
1 hour ago
How about if declared as
static char character = 'b
â rider dragon
1 hour ago
How about if declared as
static char character = 'b
â rider dragon
1 hour ago
1
1
@riderdragon For the variable in
main
, the static
modifier gives it a lifetime of the whole program.â dbush
1 hour ago
@riderdragon For the variable in
main
, the static
modifier gives it a lifetime of the whole program.â dbush
1 hour ago
@riderdragon, the
static
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".â Myst
1 hour ago
@riderdragon, the
static
declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".â Myst
1 hour ago
add a comment |Â
up vote
4
down vote
It will live on (till the program dies, as any static-storage variable would) and you can still get to it:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is '%c'n", character);
extern char character;
//in this scope, overrides the local `character` with
//the global (extern?) one
printf("The current value of head is '%c'n", character);
printf("The current value of head is '%c'n", character);
/*prints:
The current value of head is 'b'
The current value of head is 'c'
The current value of head is 'b'
*/
The local extern declaration doesn't work reliably/portably for static
globals, though you can still get to them through pointers or through a separate function.
(
Why isn't static char character='c'; int main() char character='b'; extern char character;
reliable with the global being static
?
6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).
6.2.2p4:
For an identifier declared with the storage-class specifier extern in
a scope in which a prior declaration of that identifier is visible,31)
if the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage.
My clang 6.0.0 is accepting it with a static char character='b';
but my gcc 7.3.0 isn't.
Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static
too.
)
1
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed tostatic char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules forextern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.
â Eric Postpischil
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
1
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
add a comment |Â
up vote
4
down vote
It will live on (till the program dies, as any static-storage variable would) and you can still get to it:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is '%c'n", character);
extern char character;
//in this scope, overrides the local `character` with
//the global (extern?) one
printf("The current value of head is '%c'n", character);
printf("The current value of head is '%c'n", character);
/*prints:
The current value of head is 'b'
The current value of head is 'c'
The current value of head is 'b'
*/
The local extern declaration doesn't work reliably/portably for static
globals, though you can still get to them through pointers or through a separate function.
(
Why isn't static char character='c'; int main() char character='b'; extern char character;
reliable with the global being static
?
6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).
6.2.2p4:
For an identifier declared with the storage-class specifier extern in
a scope in which a prior declaration of that identifier is visible,31)
if the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage.
My clang 6.0.0 is accepting it with a static char character='b';
but my gcc 7.3.0 isn't.
Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static
too.
)
1
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed tostatic char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules forextern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.
â Eric Postpischil
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
1
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
It will live on (till the program dies, as any static-storage variable would) and you can still get to it:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is '%c'n", character);
extern char character;
//in this scope, overrides the local `character` with
//the global (extern?) one
printf("The current value of head is '%c'n", character);
printf("The current value of head is '%c'n", character);
/*prints:
The current value of head is 'b'
The current value of head is 'c'
The current value of head is 'b'
*/
The local extern declaration doesn't work reliably/portably for static
globals, though you can still get to them through pointers or through a separate function.
(
Why isn't static char character='c'; int main() char character='b'; extern char character;
reliable with the global being static
?
6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).
6.2.2p4:
For an identifier declared with the storage-class specifier extern in
a scope in which a prior declaration of that identifier is visible,31)
if the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage.
My clang 6.0.0 is accepting it with a static char character='b';
but my gcc 7.3.0 isn't.
Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static
too.
)
It will live on (till the program dies, as any static-storage variable would) and you can still get to it:
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is '%c'n", character);
extern char character;
//in this scope, overrides the local `character` with
//the global (extern?) one
printf("The current value of head is '%c'n", character);
printf("The current value of head is '%c'n", character);
/*prints:
The current value of head is 'b'
The current value of head is 'c'
The current value of head is 'b'
*/
The local extern declaration doesn't work reliably/portably for static
globals, though you can still get to them through pointers or through a separate function.
(
Why isn't static char character='c'; int main() char character='b'; extern char character;
reliable with the global being static
?
6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).
6.2.2p4:
For an identifier declared with the storage-class specifier extern in
a scope in which a prior declaration of that identifier is visible,31)
if the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage.
My clang 6.0.0 is accepting it with a static char character='b';
but my gcc 7.3.0 isn't.
Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static
too.
)
edited 7 mins ago
answered 1 hour ago
PSkocik
28.6k43963
28.6k43963
1
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed tostatic char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules forextern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.
â Eric Postpischil
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
1
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
add a comment |Â
1
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed tostatic char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules forextern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.
â Eric Postpischil
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
1
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
1
1
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed to
static char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.â Eric Postpischil
1 hour ago
Re âÂÂDoesn't work for static's thoughâÂÂ: If the first declaration is changed to
static char characterâ¦
, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern
on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that âÂÂthe prior declaration specifies internal or external linkageâ and, for the other, âÂÂthe prior declaration specifies no linkage.â I do not see a resolution of that in the standard.â Eric Postpischil
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
@EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
â PSkocik
1 hour ago
1
1
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
â Eric Postpischil
54 mins ago
add a comment |Â
up vote
3
down vote
Complement to the other answers:
Try this and you'll understand:
#include <stdio.h>
char character = 'c';
void Check()
printf("Check: c = %cn", character);
int main(void)
char character = 'b';
printf("The current value of head is %cn", character);
Check();
1
Very elegant and usefull example.
â manoliar
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
add a comment |Â
up vote
3
down vote
Complement to the other answers:
Try this and you'll understand:
#include <stdio.h>
char character = 'c';
void Check()
printf("Check: c = %cn", character);
int main(void)
char character = 'b';
printf("The current value of head is %cn", character);
Check();
1
Very elegant and usefull example.
â manoliar
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Complement to the other answers:
Try this and you'll understand:
#include <stdio.h>
char character = 'c';
void Check()
printf("Check: c = %cn", character);
int main(void)
char character = 'b';
printf("The current value of head is %cn", character);
Check();
Complement to the other answers:
Try this and you'll understand:
#include <stdio.h>
char character = 'c';
void Check()
printf("Check: c = %cn", character);
int main(void)
char character = 'b';
printf("The current value of head is %cn", character);
Check();
answered 1 hour ago
Jabberwocky
25.2k93767
25.2k93767
1
Very elegant and usefull example.
â manoliar
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
add a comment |Â
1
Very elegant and usefull example.
â manoliar
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
1
1
Very elegant and usefull example.
â manoliar
1 hour ago
Very elegant and usefull example.
â manoliar
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
amazing example, ty.
â rider dragon
1 hour ago
add a comment |Â
up vote
1
down vote
Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.
But it could also depend on linker settings. Linker could optimize the unsused global variable out.
add a comment |Â
up vote
1
down vote
Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.
But it could also depend on linker settings. Linker could optimize the unsused global variable out.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.
But it could also depend on linker settings. Linker could optimize the unsused global variable out.
Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.
But it could also depend on linker settings. Linker could optimize the unsused global variable out.
answered 1 hour ago
Igor S.K.
584312
584312
add a comment |Â
add a comment |Â
up vote
1
down vote
Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.
A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.
If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
add a comment |Â
up vote
1
down vote
Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.
A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.
If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.
A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.
If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.
Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.
A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.
If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.
answered 1 hour ago
Bogdan N.
1865
1865
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
add a comment |Â
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
âÂÂAccessibleâ is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. âÂÂAccess,â as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, âÂÂglobalâ and âÂÂvariableâ are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
â Eric Postpischil
1 hour ago
add a comment |Â
up vote
1
down vote
After the declaration of character
in main
, any reference to character
in that function refers to that one, not the one at global scope. We call this shadowing.
As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to
#include <stdio.h>
int main()
printf("The current value of head is b");
for example.
add a comment |Â
up vote
1
down vote
After the declaration of character
in main
, any reference to character
in that function refers to that one, not the one at global scope. We call this shadowing.
As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to
#include <stdio.h>
int main()
printf("The current value of head is b");
for example.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
After the declaration of character
in main
, any reference to character
in that function refers to that one, not the one at global scope. We call this shadowing.
As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to
#include <stdio.h>
int main()
printf("The current value of head is b");
for example.
After the declaration of character
in main
, any reference to character
in that function refers to that one, not the one at global scope. We call this shadowing.
As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to
#include <stdio.h>
int main()
printf("The current value of head is b");
for example.
answered 1 hour ago
Bathsheba
170k26239362
170k26239362
add a comment |Â
add a comment |Â
up vote
1
down vote
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %cn", character);
printc();
void printc()
printf("%c", character);
It makes it all clear. It is not destroyed it is just shadowed.
Output:
The current value of head is b
c
add a comment |Â
up vote
1
down vote
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %cn", character);
printc();
void printc()
printf("%c", character);
It makes it all clear. It is not destroyed it is just shadowed.
Output:
The current value of head is b
c
add a comment |Â
up vote
1
down vote
up vote
1
down vote
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %cn", character);
printc();
void printc()
printf("%c", character);
It makes it all clear. It is not destroyed it is just shadowed.
Output:
The current value of head is b
c
#include <stdio.h>
char character = 'c';
int main (void)
char character = 'b';
printf("The current value of head is %cn", character);
printc();
void printc()
printf("%c", character);
It makes it all clear. It is not destroyed it is just shadowed.
Output:
The current value of head is b
c
edited 1 hour ago
answered 1 hour ago
curiousgeek
335
335
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52875241%2fwill-a-value-live-perpetually-when-theres-no-reference-to-it%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
â Eli Korvigo
1 hour ago
So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
â rider dragon
1 hour ago