Compiler Baremetal (arm-none-eabi) - Compiler Implementation Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why can templates only be implemented in the header file?Improve INSERT-per-second performance of SQLite?Can code that is valid in both C and C++ produce different behavior when compiled in each language?Cross-compile a library for arm-none-eabi-gccHow to affect Delphi XEx code generation for Android/ARM targets?Compiling Programs with arm-none-eabilinking succeeds with arm-none-eabi-g++ but not arm-none-eabi-gccCompiling an application for use in highly radioactive environmentsAre there valid reasons to declare variables static inside the main() function of a C program?Clang cross-compilation with arm-none-eabi
Marquee sign letters
Meaning of 境 in その日を境に
Does the main washing effect of soap come from foam?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
Twin's vs. Twins'
NIntegrate on a solution of a matrix ODE
Improvising over quartal voicings
Did pre-Columbian Americans know the spherical shape of the Earth?
Did any compiler fully use 80-bit floating point?
Besides transaction validation, are there any other uses of the Script language in Bitcoin
What is the proper term for etching or digging of wall to hide conduit of cables
Short story about astronauts fertilizing soil with their own bodies
Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)
Do i imagine the linear (straight line) homotopy in a correct way?
Determine whether an integer is a palindrome
How to infer difference of population proportion between two groups when proportion is small?
Flight departed from the gate 5 min before scheduled departure time. Refund options
Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?
Pointing to problems without suggesting solutions
Does the universe have a fixed centre of mass?
Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?
How can I list files in reverse time order by a command and pass them as arguments to another command?
How to resize main filesystem
Why is there so little support for joining EFTA in the British parliament?
Compiler Baremetal (arm-none-eabi) - Compiler Implementation
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why can templates only be implemented in the header file?Improve INSERT-per-second performance of SQLite?Can code that is valid in both C and C++ produce different behavior when compiled in each language?Cross-compile a library for arm-none-eabi-gccHow to affect Delphi XEx code generation for Android/ARM targets?Compiling Programs with arm-none-eabilinking succeeds with arm-none-eabi-g++ but not arm-none-eabi-gccCompiling an application for use in highly radioactive environmentsAre there valid reasons to declare variables static inside the main() function of a C program?Clang cross-compilation with arm-none-eabi
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically or statically) at the entrypoint of the function rather than mixed with instructions inside the function.
See Example 1.a and 1.b.
This helps with a better readability and was with older language even required.
With modern C/C++ it is no longer required (..but still good practice).
My Question though:
How does the compiler solve it if stack variables are inside a function rather than at the entry point.
See Examples 2.a and 2.b how i can imagine he solves it.
What is ACTUALLY HAPPENING?
1.a) Example (Common Practice / Best Practice)
void main()
int a = 3; // best practice
bool c = false; // best practice
a += 16;
if(a == 5)
c=false;
...rather than ...
1.b) Example (Uncommon)
void main()
int a = 3;
a += 16;
bool c = false; // variable after some instructions executed..
if(a == 5)
c=false;
2.a) Possible compiler solution (A)
void main()
int a = 3;
a += 16;
bool c = false; // COMPILER CUTS THIS LINE AND MOVES IT UP UNDER "int a = 3;"
if(a == 5)
c=false;
2.B) Possible compiler solution (B)
void main()
int a = 3;
a += 16;
// COMPILER ADDS SUBSECTION IN ORDER TO INTRODUCE NEW VARIABLES ON STACK
bool c = false;
if(a == 5)
c=false;
c++ c arm embedded cross-compiling
|
show 8 more comments
It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically or statically) at the entrypoint of the function rather than mixed with instructions inside the function.
See Example 1.a and 1.b.
This helps with a better readability and was with older language even required.
With modern C/C++ it is no longer required (..but still good practice).
My Question though:
How does the compiler solve it if stack variables are inside a function rather than at the entry point.
See Examples 2.a and 2.b how i can imagine he solves it.
What is ACTUALLY HAPPENING?
1.a) Example (Common Practice / Best Practice)
void main()
int a = 3; // best practice
bool c = false; // best practice
a += 16;
if(a == 5)
c=false;
...rather than ...
1.b) Example (Uncommon)
void main()
int a = 3;
a += 16;
bool c = false; // variable after some instructions executed..
if(a == 5)
c=false;
2.a) Possible compiler solution (A)
void main()
int a = 3;
a += 16;
bool c = false; // COMPILER CUTS THIS LINE AND MOVES IT UP UNDER "int a = 3;"
if(a == 5)
c=false;
2.B) Possible compiler solution (B)
void main()
int a = 3;
a += 16;
// COMPILER ADDS SUBSECTION IN ORDER TO INTRODUCE NEW VARIABLES ON STACK
bool c = false;
if(a == 5)
c=false;
c++ c arm embedded cross-compiling
6
"Common/best practice" is very subjective. And many people recommend defining variables as close to their use as possible (helps grouping code, and you don't have to jump much to find a variables definition when reading the code).
– Some programmer dude
Mar 22 at 13:03
As for how it's solved, one way is to simply reserve space at function entry for all variables, no matter their scope or grouping within the function. If you really want to know (for you specific compiler and target system), look at the generated machine code.
– Some programmer dude
Mar 22 at 13:04
1
It is not difficult for a compiler to keep track, some variable might be at sp-8 for a while then push an item now that variable is at sp-12 for a while pop something off the variable is now at sp-8 again. control the execution paths through the function which compilers also already do. in the grand scheme of things this is not in any way a difficult task for compilers they have other more complicated tasks to worry about.
– old_timer
Mar 22 at 13:14
Also note that while automatic (local non-static) variables are usually stored on the stack (even through it's not something specified in the C specification), a good compiler doesn't really usepush
orpop
instructions to handle them. Instead it just reserves a chunk of the stack for its variables, and then uses offsets from the stack-pointer to access the variables.
– Some programmer dude
Mar 22 at 13:18
1
You would do well also to remove the contentious and inaccurate pre-amble to your question and just ask the question. "With modern C/C++ it is no longer required (..but still good practice)." - no, it was never good practice - exactly the opposite - minimal scope is always preferable. Moreover it is not a "modern C/C++" thing either, it was never necessary in C++, and for C you could always declare variables at the start of any...
delimited block, not just function blocks.
– Clifford
Mar 22 at 13:50
|
show 8 more comments
It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically or statically) at the entrypoint of the function rather than mixed with instructions inside the function.
See Example 1.a and 1.b.
This helps with a better readability and was with older language even required.
With modern C/C++ it is no longer required (..but still good practice).
My Question though:
How does the compiler solve it if stack variables are inside a function rather than at the entry point.
See Examples 2.a and 2.b how i can imagine he solves it.
What is ACTUALLY HAPPENING?
1.a) Example (Common Practice / Best Practice)
void main()
int a = 3; // best practice
bool c = false; // best practice
a += 16;
if(a == 5)
c=false;
...rather than ...
1.b) Example (Uncommon)
void main()
int a = 3;
a += 16;
bool c = false; // variable after some instructions executed..
if(a == 5)
c=false;
2.a) Possible compiler solution (A)
void main()
int a = 3;
a += 16;
bool c = false; // COMPILER CUTS THIS LINE AND MOVES IT UP UNDER "int a = 3;"
if(a == 5)
c=false;
2.B) Possible compiler solution (B)
void main()
int a = 3;
a += 16;
// COMPILER ADDS SUBSECTION IN ORDER TO INTRODUCE NEW VARIABLES ON STACK
bool c = false;
if(a == 5)
c=false;
c++ c arm embedded cross-compiling
It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically or statically) at the entrypoint of the function rather than mixed with instructions inside the function.
See Example 1.a and 1.b.
This helps with a better readability and was with older language even required.
With modern C/C++ it is no longer required (..but still good practice).
My Question though:
How does the compiler solve it if stack variables are inside a function rather than at the entry point.
See Examples 2.a and 2.b how i can imagine he solves it.
What is ACTUALLY HAPPENING?
1.a) Example (Common Practice / Best Practice)
void main()
int a = 3; // best practice
bool c = false; // best practice
a += 16;
if(a == 5)
c=false;
...rather than ...
1.b) Example (Uncommon)
void main()
int a = 3;
a += 16;
bool c = false; // variable after some instructions executed..
if(a == 5)
c=false;
2.a) Possible compiler solution (A)
void main()
int a = 3;
a += 16;
bool c = false; // COMPILER CUTS THIS LINE AND MOVES IT UP UNDER "int a = 3;"
if(a == 5)
c=false;
2.B) Possible compiler solution (B)
void main()
int a = 3;
a += 16;
// COMPILER ADDS SUBSECTION IN ORDER TO INTRODUCE NEW VARIABLES ON STACK
bool c = false;
if(a == 5)
c=false;
c++ c arm embedded cross-compiling
c++ c arm embedded cross-compiling
asked Mar 22 at 13:00
aphardtaphardt
245
245
6
"Common/best practice" is very subjective. And many people recommend defining variables as close to their use as possible (helps grouping code, and you don't have to jump much to find a variables definition when reading the code).
– Some programmer dude
Mar 22 at 13:03
As for how it's solved, one way is to simply reserve space at function entry for all variables, no matter their scope or grouping within the function. If you really want to know (for you specific compiler and target system), look at the generated machine code.
– Some programmer dude
Mar 22 at 13:04
1
It is not difficult for a compiler to keep track, some variable might be at sp-8 for a while then push an item now that variable is at sp-12 for a while pop something off the variable is now at sp-8 again. control the execution paths through the function which compilers also already do. in the grand scheme of things this is not in any way a difficult task for compilers they have other more complicated tasks to worry about.
– old_timer
Mar 22 at 13:14
Also note that while automatic (local non-static) variables are usually stored on the stack (even through it's not something specified in the C specification), a good compiler doesn't really usepush
orpop
instructions to handle them. Instead it just reserves a chunk of the stack for its variables, and then uses offsets from the stack-pointer to access the variables.
– Some programmer dude
Mar 22 at 13:18
1
You would do well also to remove the contentious and inaccurate pre-amble to your question and just ask the question. "With modern C/C++ it is no longer required (..but still good practice)." - no, it was never good practice - exactly the opposite - minimal scope is always preferable. Moreover it is not a "modern C/C++" thing either, it was never necessary in C++, and for C you could always declare variables at the start of any...
delimited block, not just function blocks.
– Clifford
Mar 22 at 13:50
|
show 8 more comments
6
"Common/best practice" is very subjective. And many people recommend defining variables as close to their use as possible (helps grouping code, and you don't have to jump much to find a variables definition when reading the code).
– Some programmer dude
Mar 22 at 13:03
As for how it's solved, one way is to simply reserve space at function entry for all variables, no matter their scope or grouping within the function. If you really want to know (for you specific compiler and target system), look at the generated machine code.
– Some programmer dude
Mar 22 at 13:04
1
It is not difficult for a compiler to keep track, some variable might be at sp-8 for a while then push an item now that variable is at sp-12 for a while pop something off the variable is now at sp-8 again. control the execution paths through the function which compilers also already do. in the grand scheme of things this is not in any way a difficult task for compilers they have other more complicated tasks to worry about.
– old_timer
Mar 22 at 13:14
Also note that while automatic (local non-static) variables are usually stored on the stack (even through it's not something specified in the C specification), a good compiler doesn't really usepush
orpop
instructions to handle them. Instead it just reserves a chunk of the stack for its variables, and then uses offsets from the stack-pointer to access the variables.
– Some programmer dude
Mar 22 at 13:18
1
You would do well also to remove the contentious and inaccurate pre-amble to your question and just ask the question. "With modern C/C++ it is no longer required (..but still good practice)." - no, it was never good practice - exactly the opposite - minimal scope is always preferable. Moreover it is not a "modern C/C++" thing either, it was never necessary in C++, and for C you could always declare variables at the start of any...
delimited block, not just function blocks.
– Clifford
Mar 22 at 13:50
6
6
"Common/best practice" is very subjective. And many people recommend defining variables as close to their use as possible (helps grouping code, and you don't have to jump much to find a variables definition when reading the code).
– Some programmer dude
Mar 22 at 13:03
"Common/best practice" is very subjective. And many people recommend defining variables as close to their use as possible (helps grouping code, and you don't have to jump much to find a variables definition when reading the code).
– Some programmer dude
Mar 22 at 13:03
As for how it's solved, one way is to simply reserve space at function entry for all variables, no matter their scope or grouping within the function. If you really want to know (for you specific compiler and target system), look at the generated machine code.
– Some programmer dude
Mar 22 at 13:04
As for how it's solved, one way is to simply reserve space at function entry for all variables, no matter their scope or grouping within the function. If you really want to know (for you specific compiler and target system), look at the generated machine code.
– Some programmer dude
Mar 22 at 13:04
1
1
It is not difficult for a compiler to keep track, some variable might be at sp-8 for a while then push an item now that variable is at sp-12 for a while pop something off the variable is now at sp-8 again. control the execution paths through the function which compilers also already do. in the grand scheme of things this is not in any way a difficult task for compilers they have other more complicated tasks to worry about.
– old_timer
Mar 22 at 13:14
It is not difficult for a compiler to keep track, some variable might be at sp-8 for a while then push an item now that variable is at sp-12 for a while pop something off the variable is now at sp-8 again. control the execution paths through the function which compilers also already do. in the grand scheme of things this is not in any way a difficult task for compilers they have other more complicated tasks to worry about.
– old_timer
Mar 22 at 13:14
Also note that while automatic (local non-static) variables are usually stored on the stack (even through it's not something specified in the C specification), a good compiler doesn't really use
push
or pop
instructions to handle them. Instead it just reserves a chunk of the stack for its variables, and then uses offsets from the stack-pointer to access the variables.– Some programmer dude
Mar 22 at 13:18
Also note that while automatic (local non-static) variables are usually stored on the stack (even through it's not something specified in the C specification), a good compiler doesn't really use
push
or pop
instructions to handle them. Instead it just reserves a chunk of the stack for its variables, and then uses offsets from the stack-pointer to access the variables.– Some programmer dude
Mar 22 at 13:18
1
1
You would do well also to remove the contentious and inaccurate pre-amble to your question and just ask the question. "With modern C/C++ it is no longer required (..but still good practice)." - no, it was never good practice - exactly the opposite - minimal scope is always preferable. Moreover it is not a "modern C/C++" thing either, it was never necessary in C++, and for C you could always declare variables at the start of any
...
delimited block, not just function blocks.– Clifford
Mar 22 at 13:50
You would do well also to remove the contentious and inaccurate pre-amble to your question and just ask the question. "With modern C/C++ it is no longer required (..but still good practice)." - no, it was never good practice - exactly the opposite - minimal scope is always preferable. Moreover it is not a "modern C/C++" thing either, it was never necessary in C++, and for C you could always declare variables at the start of any
...
delimited block, not just function blocks.– Clifford
Mar 22 at 13:50
|
show 8 more comments
1 Answer
1
active
oldest
votes
When, how and where the compiler instantiates or allocates such variables is entirely implementation dependent. It may create a stack frame for all variables at the start of the function, or it may extend the stack frame at the point of declaration, or it may not use the stack at all and use register storage instead.
The language only requires that the correct semantics are implemented; that the variable is not in scope (i.e. cannot be referenced by code) before it is declared, and that the scope ends after the closing }
of the block in which it was declared.
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55300188%2fcompiler-baremetal-arm-none-eabi-compiler-implementation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When, how and where the compiler instantiates or allocates such variables is entirely implementation dependent. It may create a stack frame for all variables at the start of the function, or it may extend the stack frame at the point of declaration, or it may not use the stack at all and use register storage instead.
The language only requires that the correct semantics are implemented; that the variable is not in scope (i.e. cannot be referenced by code) before it is declared, and that the scope ends after the closing }
of the block in which it was declared.
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
add a comment |
When, how and where the compiler instantiates or allocates such variables is entirely implementation dependent. It may create a stack frame for all variables at the start of the function, or it may extend the stack frame at the point of declaration, or it may not use the stack at all and use register storage instead.
The language only requires that the correct semantics are implemented; that the variable is not in scope (i.e. cannot be referenced by code) before it is declared, and that the scope ends after the closing }
of the block in which it was declared.
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
add a comment |
When, how and where the compiler instantiates or allocates such variables is entirely implementation dependent. It may create a stack frame for all variables at the start of the function, or it may extend the stack frame at the point of declaration, or it may not use the stack at all and use register storage instead.
The language only requires that the correct semantics are implemented; that the variable is not in scope (i.e. cannot be referenced by code) before it is declared, and that the scope ends after the closing }
of the block in which it was declared.
When, how and where the compiler instantiates or allocates such variables is entirely implementation dependent. It may create a stack frame for all variables at the start of the function, or it may extend the stack frame at the point of declaration, or it may not use the stack at all and use register storage instead.
The language only requires that the correct semantics are implemented; that the variable is not in scope (i.e. cannot be referenced by code) before it is declared, and that the scope ends after the closing }
of the block in which it was declared.
answered Mar 22 at 13:58
CliffordClifford
61.1k860128
61.1k860128
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
add a comment |
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
Ok. My question is more targeting user-controlled optimization (!= compiler optimization), so what i can and should do to write fast and low-foodprint code.
– aphardt
Mar 22 at 14:23
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
@aphardt : If it were to have any optimisation at all, which is doubtful, it would be the kind of micro-optimisation that is best left to the compiler - as a developer you should concentrate on efficient algorithms, code structure and appropriate data structure selection - you will achieve far more doing those things the compiler cannot do that trying to second-guess it.
– Clifford
Mar 22 at 18:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55300188%2fcompiler-baremetal-arm-none-eabi-compiler-implementation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
"Common/best practice" is very subjective. And many people recommend defining variables as close to their use as possible (helps grouping code, and you don't have to jump much to find a variables definition when reading the code).
– Some programmer dude
Mar 22 at 13:03
As for how it's solved, one way is to simply reserve space at function entry for all variables, no matter their scope or grouping within the function. If you really want to know (for you specific compiler and target system), look at the generated machine code.
– Some programmer dude
Mar 22 at 13:04
1
It is not difficult for a compiler to keep track, some variable might be at sp-8 for a while then push an item now that variable is at sp-12 for a while pop something off the variable is now at sp-8 again. control the execution paths through the function which compilers also already do. in the grand scheme of things this is not in any way a difficult task for compilers they have other more complicated tasks to worry about.
– old_timer
Mar 22 at 13:14
Also note that while automatic (local non-static) variables are usually stored on the stack (even through it's not something specified in the C specification), a good compiler doesn't really use
push
orpop
instructions to handle them. Instead it just reserves a chunk of the stack for its variables, and then uses offsets from the stack-pointer to access the variables.– Some programmer dude
Mar 22 at 13:18
1
You would do well also to remove the contentious and inaccurate pre-amble to your question and just ask the question. "With modern C/C++ it is no longer required (..but still good practice)." - no, it was never good practice - exactly the opposite - minimal scope is always preferable. Moreover it is not a "modern C/C++" thing either, it was never necessary in C++, and for C you could always declare variables at the start of any
...
delimited block, not just function blocks.– Clifford
Mar 22 at 13:50