Writing declarations in a loop









up vote
1
down vote

favorite












The following question came to my mind:

Is there any difference between these two ways of running code?

Perhaps in memory management?



int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);

if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);

if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);

if(counter >= 4) counter = 1; else counter++;

return 0;



//



int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);


if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);


if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);


if(counter >= 4) counter = 1; else counter++;

return 0;



//
This is a simplified version of code, which I am using.

Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!










share|improve this question























  • I think we need a real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
    – Sean Bright
    Nov 8 at 17:16











  • @SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
    – chux
    Nov 8 at 20:05










  • The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
    – Sean Bright
    Nov 8 at 20:08















up vote
1
down vote

favorite












The following question came to my mind:

Is there any difference between these two ways of running code?

Perhaps in memory management?



int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);

if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);

if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);

if(counter >= 4) counter = 1; else counter++;

return 0;



//



int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);


if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);


if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);


if(counter >= 4) counter = 1; else counter++;

return 0;



//
This is a simplified version of code, which I am using.

Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!










share|improve this question























  • I think we need a real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
    – Sean Bright
    Nov 8 at 17:16











  • @SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
    – chux
    Nov 8 at 20:05










  • The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
    – Sean Bright
    Nov 8 at 20:08













up vote
1
down vote

favorite









up vote
1
down vote

favorite











The following question came to my mind:

Is there any difference between these two ways of running code?

Perhaps in memory management?



int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);

if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);

if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);

if(counter >= 4) counter = 1; else counter++;

return 0;



//



int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);


if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);


if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);


if(counter >= 4) counter = 1; else counter++;

return 0;



//
This is a simplified version of code, which I am using.

Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!










share|improve this question















The following question came to my mind:

Is there any difference between these two ways of running code?

Perhaps in memory management?



int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);

if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);

if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);

if(counter >= 4) counter = 1; else counter++;

return 0;



//



int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);


if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);


if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);


if(counter >= 4) counter = 1; else counter++;

return 0;



//
This is a simplified version of code, which I am using.

Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 17:04

























asked Nov 8 at 16:50









itzFlubby

10515




10515











  • I think we need a real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
    – Sean Bright
    Nov 8 at 17:16











  • @SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
    – chux
    Nov 8 at 20:05










  • The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
    – Sean Bright
    Nov 8 at 20:08

















  • I think we need a real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
    – Sean Bright
    Nov 8 at 17:16











  • @SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
    – chux
    Nov 8 at 20:05










  • The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
    – Sean Bright
    Nov 8 at 20:08
















I think we need a real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
– Sean Bright
Nov 8 at 17:16





I think we need a real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
– Sean Bright
Nov 8 at 17:16













@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05




@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05












The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08





The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08













3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.



That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.



So your second approach is preferable.






share|improve this answer



























    up vote
    1
    down vote













    The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...



    So your question can't be answered in general. Instead it depends on the platform used and compiler used.



    Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.



    The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.






    share|improve this answer



























      up vote
      0
      down vote













      On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.



      Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).



      int main() 
      int counter = 0;
      int arr[3] = 0, 1, 2;
      if (counter < 3)
      for (int i = 0; i < 2; i++)
      printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
      counter++;
      else
      counter = 1;






      share|improve this answer




















        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',
        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
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212458%2fwriting-declarations-in-a-loop%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.



        That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.



        So your second approach is preferable.






        share|improve this answer
























          up vote
          4
          down vote



          accepted










          Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.



          That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.



          So your second approach is preferable.






          share|improve this answer






















            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.



            That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.



            So your second approach is preferable.






            share|improve this answer












            Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.



            That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.



            So your second approach is preferable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 16:54









            dbush

            89.4k1298131




            89.4k1298131






















                up vote
                1
                down vote













                The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...



                So your question can't be answered in general. Instead it depends on the platform used and compiler used.



                Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.



                The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.






                share|improve this answer
























                  up vote
                  1
                  down vote













                  The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...



                  So your question can't be answered in general. Instead it depends on the platform used and compiler used.



                  Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.



                  The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...



                    So your question can't be answered in general. Instead it depends on the platform used and compiler used.



                    Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.



                    The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.






                    share|improve this answer












                    The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...



                    So your question can't be answered in general. Instead it depends on the platform used and compiler used.



                    Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.



                    The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 at 17:08









                    4386427

                    19.6k21745




                    19.6k21745




















                        up vote
                        0
                        down vote













                        On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.



                        Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).



                        int main() 
                        int counter = 0;
                        int arr[3] = 0, 1, 2;
                        if (counter < 3)
                        for (int i = 0; i < 2; i++)
                        printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
                        counter++;
                        else
                        counter = 1;






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.



                          Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).



                          int main() 
                          int counter = 0;
                          int arr[3] = 0, 1, 2;
                          if (counter < 3)
                          for (int i = 0; i < 2; i++)
                          printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
                          counter++;
                          else
                          counter = 1;






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.



                            Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).



                            int main() 
                            int counter = 0;
                            int arr[3] = 0, 1, 2;
                            if (counter < 3)
                            for (int i = 0; i < 2; i++)
                            printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
                            counter++;
                            else
                            counter = 1;






                            share|improve this answer












                            On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.



                            Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).



                            int main() 
                            int counter = 0;
                            int arr[3] = 0, 1, 2;
                            if (counter < 3)
                            for (int i = 0; i < 2; i++)
                            printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
                            counter++;
                            else
                            counter = 1;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 8 at 17:00









                            Jean-Marc Zimmer

                            32514




                            32514



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212458%2fwriting-declarations-in-a-loop%23new-answer', 'question_page');

                                );

                                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







                                Popular posts from this blog

                                𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                How do I collapse sections of code in Visual Studio Code for Windows?

                                ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ