C: Array of automatic storage class?











up vote
5
down vote

favorite












I'm reading C How to Program and I have a question about storage classes of arrays. In the book it says:




Array and structs are "static" entities in that they remain the same size throughout the program execution (they may, of course, be of automatic storage class and hence created and destroyed each time the blocks in which they're defined are entered and exited)




I'm not sure about what blocks mean, my current understanding is that function/for/while are blocks. I've tried the following:



...
for (size_t i=1; i<=2; i++) {
printf("round %c:", i+'0');
int a[10];
show_array(a, 10); // this is a function that prints all elements of `a`.
for (size_t j=0;j<10;j++) {
a[j]=8;
}
}


I got the output:



round 1:0,0,0,0,-1160480784,22023,-1160481168,22023,1594487680,32766,
round 2:8,8,8,8,8,8,8,8,8,8,


It seems like int a[10] is static, and not automatic storage class, am I missing something? (I understand the four storage classes)



I used ideone.com to run my test, if you need this information.










share|improve this question




















  • 1




    What do you think "automatic storage class" means?
    – Fred Larson
    Nov 19 at 20:18






  • 1




    Are 32766 or -1160481168 "unitialised values"? If they are, why 8 isn't?
    – n.m.
    Nov 19 at 20:30






  • 2




    We have very good evidence that the array does not have static storage duration—anything with static storage duration is initialized to 0 at the start of the program, unless you initialize it to something else. Since your array contains garbage numbers like -1160480784, it cannot have static storage duration (unless your program has memory errors somewhere else).
    – Dietrich Epp
    Nov 19 at 20:30






  • 1




    Per C 2018 6.8.2 2, a compound statement is a block. A compound statement is a pair of braces, { and }, with declarations and/or statements inside it.
    – Eric Postpischil
    Nov 19 at 20:30






  • 1




    @user7813604: When execution of a block ends, the lifetime of each object with automatic storage duration associated with that block ends. When another execution of the block begins, each such object created in it has a new lifetime. Every iteration is different.
    – Eric Postpischil
    Nov 19 at 22:37

















up vote
5
down vote

favorite












I'm reading C How to Program and I have a question about storage classes of arrays. In the book it says:




Array and structs are "static" entities in that they remain the same size throughout the program execution (they may, of course, be of automatic storage class and hence created and destroyed each time the blocks in which they're defined are entered and exited)




I'm not sure about what blocks mean, my current understanding is that function/for/while are blocks. I've tried the following:



...
for (size_t i=1; i<=2; i++) {
printf("round %c:", i+'0');
int a[10];
show_array(a, 10); // this is a function that prints all elements of `a`.
for (size_t j=0;j<10;j++) {
a[j]=8;
}
}


I got the output:



round 1:0,0,0,0,-1160480784,22023,-1160481168,22023,1594487680,32766,
round 2:8,8,8,8,8,8,8,8,8,8,


It seems like int a[10] is static, and not automatic storage class, am I missing something? (I understand the four storage classes)



I used ideone.com to run my test, if you need this information.










share|improve this question




















  • 1




    What do you think "automatic storage class" means?
    – Fred Larson
    Nov 19 at 20:18






  • 1




    Are 32766 or -1160481168 "unitialised values"? If they are, why 8 isn't?
    – n.m.
    Nov 19 at 20:30






  • 2




    We have very good evidence that the array does not have static storage duration—anything with static storage duration is initialized to 0 at the start of the program, unless you initialize it to something else. Since your array contains garbage numbers like -1160480784, it cannot have static storage duration (unless your program has memory errors somewhere else).
    – Dietrich Epp
    Nov 19 at 20:30






  • 1




    Per C 2018 6.8.2 2, a compound statement is a block. A compound statement is a pair of braces, { and }, with declarations and/or statements inside it.
    – Eric Postpischil
    Nov 19 at 20:30






  • 1




    @user7813604: When execution of a block ends, the lifetime of each object with automatic storage duration associated with that block ends. When another execution of the block begins, each such object created in it has a new lifetime. Every iteration is different.
    – Eric Postpischil
    Nov 19 at 22:37















up vote
5
down vote

favorite









up vote
5
down vote

favorite











I'm reading C How to Program and I have a question about storage classes of arrays. In the book it says:




Array and structs are "static" entities in that they remain the same size throughout the program execution (they may, of course, be of automatic storage class and hence created and destroyed each time the blocks in which they're defined are entered and exited)




I'm not sure about what blocks mean, my current understanding is that function/for/while are blocks. I've tried the following:



...
for (size_t i=1; i<=2; i++) {
printf("round %c:", i+'0');
int a[10];
show_array(a, 10); // this is a function that prints all elements of `a`.
for (size_t j=0;j<10;j++) {
a[j]=8;
}
}


I got the output:



round 1:0,0,0,0,-1160480784,22023,-1160481168,22023,1594487680,32766,
round 2:8,8,8,8,8,8,8,8,8,8,


It seems like int a[10] is static, and not automatic storage class, am I missing something? (I understand the four storage classes)



I used ideone.com to run my test, if you need this information.










share|improve this question















I'm reading C How to Program and I have a question about storage classes of arrays. In the book it says:




Array and structs are "static" entities in that they remain the same size throughout the program execution (they may, of course, be of automatic storage class and hence created and destroyed each time the blocks in which they're defined are entered and exited)




I'm not sure about what blocks mean, my current understanding is that function/for/while are blocks. I've tried the following:



...
for (size_t i=1; i<=2; i++) {
printf("round %c:", i+'0');
int a[10];
show_array(a, 10); // this is a function that prints all elements of `a`.
for (size_t j=0;j<10;j++) {
a[j]=8;
}
}


I got the output:



round 1:0,0,0,0,-1160480784,22023,-1160481168,22023,1594487680,32766,
round 2:8,8,8,8,8,8,8,8,8,8,


It seems like int a[10] is static, and not automatic storage class, am I missing something? (I understand the four storage classes)



I used ideone.com to run my test, if you need this information.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 21:01

























asked Nov 19 at 20:17









ptr_user7813604

463118




463118








  • 1




    What do you think "automatic storage class" means?
    – Fred Larson
    Nov 19 at 20:18






  • 1




    Are 32766 or -1160481168 "unitialised values"? If they are, why 8 isn't?
    – n.m.
    Nov 19 at 20:30






  • 2




    We have very good evidence that the array does not have static storage duration—anything with static storage duration is initialized to 0 at the start of the program, unless you initialize it to something else. Since your array contains garbage numbers like -1160480784, it cannot have static storage duration (unless your program has memory errors somewhere else).
    – Dietrich Epp
    Nov 19 at 20:30






  • 1




    Per C 2018 6.8.2 2, a compound statement is a block. A compound statement is a pair of braces, { and }, with declarations and/or statements inside it.
    – Eric Postpischil
    Nov 19 at 20:30






  • 1




    @user7813604: When execution of a block ends, the lifetime of each object with automatic storage duration associated with that block ends. When another execution of the block begins, each such object created in it has a new lifetime. Every iteration is different.
    – Eric Postpischil
    Nov 19 at 22:37
















  • 1




    What do you think "automatic storage class" means?
    – Fred Larson
    Nov 19 at 20:18






  • 1




    Are 32766 or -1160481168 "unitialised values"? If they are, why 8 isn't?
    – n.m.
    Nov 19 at 20:30






  • 2




    We have very good evidence that the array does not have static storage duration—anything with static storage duration is initialized to 0 at the start of the program, unless you initialize it to something else. Since your array contains garbage numbers like -1160480784, it cannot have static storage duration (unless your program has memory errors somewhere else).
    – Dietrich Epp
    Nov 19 at 20:30






  • 1




    Per C 2018 6.8.2 2, a compound statement is a block. A compound statement is a pair of braces, { and }, with declarations and/or statements inside it.
    – Eric Postpischil
    Nov 19 at 20:30






  • 1




    @user7813604: When execution of a block ends, the lifetime of each object with automatic storage duration associated with that block ends. When another execution of the block begins, each such object created in it has a new lifetime. Every iteration is different.
    – Eric Postpischil
    Nov 19 at 22:37










1




1




What do you think "automatic storage class" means?
– Fred Larson
Nov 19 at 20:18




What do you think "automatic storage class" means?
– Fred Larson
Nov 19 at 20:18




1




1




Are 32766 or -1160481168 "unitialised values"? If they are, why 8 isn't?
– n.m.
Nov 19 at 20:30




Are 32766 or -1160481168 "unitialised values"? If they are, why 8 isn't?
– n.m.
Nov 19 at 20:30




2




2




We have very good evidence that the array does not have static storage duration—anything with static storage duration is initialized to 0 at the start of the program, unless you initialize it to something else. Since your array contains garbage numbers like -1160480784, it cannot have static storage duration (unless your program has memory errors somewhere else).
– Dietrich Epp
Nov 19 at 20:30




We have very good evidence that the array does not have static storage duration—anything with static storage duration is initialized to 0 at the start of the program, unless you initialize it to something else. Since your array contains garbage numbers like -1160480784, it cannot have static storage duration (unless your program has memory errors somewhere else).
– Dietrich Epp
Nov 19 at 20:30




1




1




Per C 2018 6.8.2 2, a compound statement is a block. A compound statement is a pair of braces, { and }, with declarations and/or statements inside it.
– Eric Postpischil
Nov 19 at 20:30




Per C 2018 6.8.2 2, a compound statement is a block. A compound statement is a pair of braces, { and }, with declarations and/or statements inside it.
– Eric Postpischil
Nov 19 at 20:30




1




1




@user7813604: When execution of a block ends, the lifetime of each object with automatic storage duration associated with that block ends. When another execution of the block begins, each such object created in it has a new lifetime. Every iteration is different.
– Eric Postpischil
Nov 19 at 22:37






@user7813604: When execution of a block ends, the lifetime of each object with automatic storage duration associated with that block ends. When another execution of the block begins, each such object created in it has a new lifetime. Every iteration is different.
– Eric Postpischil
Nov 19 at 22:37














5 Answers
5






active

oldest

votes

















up vote
5
down vote



accepted










You are indeed missing something. a does have automatic storage, and the values that are printed are a consequence of the memory being re-used, not of the storage being persistent. The values will be reset in debug mode (perhaps not by all development environments, but some will set the members to 0xCCCCCCCC on each iteration). Also, good compilers (most compilers, if you enable all warnings) will tell you that you're using uninitialized data here.



If you still don't believe me, try this example. It will show that the memory values in a are overwritten by the values stored in b. The array a ceases to exist at the end of the if statement's dependent block of code, and all of that memory is made available to the system. On the next iteration, it will probably be used for array b, which is why you'll see values of 8 printed in array b, even though they were assigned to a.



for (size_t i=1; i<=2; i++)
{
if( i&1 )
{
printf("round %c:", i+'0');
int a[10];
show_array(a, 10);
for (size_t j=0;j<10;j++) a[j]=8;
}
else
{
printf("round %c:", i+'0');
int b[10];
show_array(b, 10);
for (size_t j=0;j<10;j++) b[j]=888;
}
}


For a final confirmation that memory is being re-used, you could modify show_array to print the raw pointer passed in, not just the individual elements. You should see the same address each time.






share|improve this answer























  • Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
    – ptr_user7813604
    Nov 19 at 20:27






  • 1




    An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
    – Dietrich Epp
    Nov 19 at 20:28










  • What @DietrichEpp said. I'll add anote to the answer
    – Tim Randall
    Nov 19 at 20:28










  • @DietrichEpp: oh I got it!
    – ptr_user7813604
    Nov 19 at 20:29










  • Thank you sir I do believe you but it's just my style of asking.
    – ptr_user7813604
    Nov 19 at 20:57


















up vote
4
down vote













"Automatic storage class" refers to the scope in which the variable is defined. It means that the memory allocated to the variable will be freed upon exiting the scope in which the variable was created, not that the memory will be initialized for you. You still have to do that yourself. The reason you see the 8s in the second iteration of your loop is because the same memory that was freed in the previous iteration was reassigned to the array.






share|improve this answer




























    up vote
    3
    down vote













    The array a is an automatic variable because it is defined in a block scope, the block in this case being the body of the for loop. The fact that you see the values you set the last time doesn't mean the variable is static.



    Automatic variables are uninitialized if not explicitly initialized. This basically means they'll contain whatever happened to be in those memory locations the last time they were used.



    On the first iteration of the loop, you see seemingly random values in the array. The variable then goes out of scope at the end of the loop, and on the next iteration a new instance is created, but in this case it happened to be created at the same memory location it was created at last time, and nothing else happened to write to that memory location, so you end up seeing the same values. There's no guarantee that you'll see the same behavior if you for example make an unrelated code change or compile with different optimization settings.






    share|improve this answer






























      up vote
      2
      down vote













      When the lifetime of an object with automatic storage duration ends, that just means the memory is released (so that it may be used for other purposes). It does not guarantee that the memory is erased or that it is used for other purposes.



      When the lifetime of another instance of the same object begins, memory is allocated for it again. It may happen to be the same memory that was used for it before. There is no guarantee that it was erased or used for other purposes.






      share|improve this answer




























        up vote
        2
        down vote













        Your program has what is scientifically known as undefined behaviour. In your case it results from using (printing) a bunch of int objects before initialising them.



        Since the behaviour is undefined, program output can be anything at all, including a stream of characters that could possibly in theory lead someone into believing in an obvious nonsense (like a having static storage duration).



        In practical terms, this is what happens if you ignore the high-brow world of language standards and look at the iron:




        • At round 1 a contains garbage values, remnants of whatever has been stored at that memory location previously.

        • At round 2 a still contains garbage values, remnants of whatever has been stored at that memory location previously.


        The only difference is that the garbage looks familiar to you, because it happens to be your garbage. You can see it because the garbage people did not have a chance to collect it yet. It is still garbage however — the garbage people will remove it at some point.






        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%2f53382014%2fc-array-of-automatic-storage-class%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          You are indeed missing something. a does have automatic storage, and the values that are printed are a consequence of the memory being re-used, not of the storage being persistent. The values will be reset in debug mode (perhaps not by all development environments, but some will set the members to 0xCCCCCCCC on each iteration). Also, good compilers (most compilers, if you enable all warnings) will tell you that you're using uninitialized data here.



          If you still don't believe me, try this example. It will show that the memory values in a are overwritten by the values stored in b. The array a ceases to exist at the end of the if statement's dependent block of code, and all of that memory is made available to the system. On the next iteration, it will probably be used for array b, which is why you'll see values of 8 printed in array b, even though they were assigned to a.



          for (size_t i=1; i<=2; i++)
          {
          if( i&1 )
          {
          printf("round %c:", i+'0');
          int a[10];
          show_array(a, 10);
          for (size_t j=0;j<10;j++) a[j]=8;
          }
          else
          {
          printf("round %c:", i+'0');
          int b[10];
          show_array(b, 10);
          for (size_t j=0;j<10;j++) b[j]=888;
          }
          }


          For a final confirmation that memory is being re-used, you could modify show_array to print the raw pointer passed in, not just the individual elements. You should see the same address each time.






          share|improve this answer























          • Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
            – ptr_user7813604
            Nov 19 at 20:27






          • 1




            An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
            – Dietrich Epp
            Nov 19 at 20:28










          • What @DietrichEpp said. I'll add anote to the answer
            – Tim Randall
            Nov 19 at 20:28










          • @DietrichEpp: oh I got it!
            – ptr_user7813604
            Nov 19 at 20:29










          • Thank you sir I do believe you but it's just my style of asking.
            – ptr_user7813604
            Nov 19 at 20:57















          up vote
          5
          down vote



          accepted










          You are indeed missing something. a does have automatic storage, and the values that are printed are a consequence of the memory being re-used, not of the storage being persistent. The values will be reset in debug mode (perhaps not by all development environments, but some will set the members to 0xCCCCCCCC on each iteration). Also, good compilers (most compilers, if you enable all warnings) will tell you that you're using uninitialized data here.



          If you still don't believe me, try this example. It will show that the memory values in a are overwritten by the values stored in b. The array a ceases to exist at the end of the if statement's dependent block of code, and all of that memory is made available to the system. On the next iteration, it will probably be used for array b, which is why you'll see values of 8 printed in array b, even though they were assigned to a.



          for (size_t i=1; i<=2; i++)
          {
          if( i&1 )
          {
          printf("round %c:", i+'0');
          int a[10];
          show_array(a, 10);
          for (size_t j=0;j<10;j++) a[j]=8;
          }
          else
          {
          printf("round %c:", i+'0');
          int b[10];
          show_array(b, 10);
          for (size_t j=0;j<10;j++) b[j]=888;
          }
          }


          For a final confirmation that memory is being re-used, you could modify show_array to print the raw pointer passed in, not just the individual elements. You should see the same address each time.






          share|improve this answer























          • Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
            – ptr_user7813604
            Nov 19 at 20:27






          • 1




            An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
            – Dietrich Epp
            Nov 19 at 20:28










          • What @DietrichEpp said. I'll add anote to the answer
            – Tim Randall
            Nov 19 at 20:28










          • @DietrichEpp: oh I got it!
            – ptr_user7813604
            Nov 19 at 20:29










          • Thank you sir I do believe you but it's just my style of asking.
            – ptr_user7813604
            Nov 19 at 20:57













          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          You are indeed missing something. a does have automatic storage, and the values that are printed are a consequence of the memory being re-used, not of the storage being persistent. The values will be reset in debug mode (perhaps not by all development environments, but some will set the members to 0xCCCCCCCC on each iteration). Also, good compilers (most compilers, if you enable all warnings) will tell you that you're using uninitialized data here.



          If you still don't believe me, try this example. It will show that the memory values in a are overwritten by the values stored in b. The array a ceases to exist at the end of the if statement's dependent block of code, and all of that memory is made available to the system. On the next iteration, it will probably be used for array b, which is why you'll see values of 8 printed in array b, even though they were assigned to a.



          for (size_t i=1; i<=2; i++)
          {
          if( i&1 )
          {
          printf("round %c:", i+'0');
          int a[10];
          show_array(a, 10);
          for (size_t j=0;j<10;j++) a[j]=8;
          }
          else
          {
          printf("round %c:", i+'0');
          int b[10];
          show_array(b, 10);
          for (size_t j=0;j<10;j++) b[j]=888;
          }
          }


          For a final confirmation that memory is being re-used, you could modify show_array to print the raw pointer passed in, not just the individual elements. You should see the same address each time.






          share|improve this answer














          You are indeed missing something. a does have automatic storage, and the values that are printed are a consequence of the memory being re-used, not of the storage being persistent. The values will be reset in debug mode (perhaps not by all development environments, but some will set the members to 0xCCCCCCCC on each iteration). Also, good compilers (most compilers, if you enable all warnings) will tell you that you're using uninitialized data here.



          If you still don't believe me, try this example. It will show that the memory values in a are overwritten by the values stored in b. The array a ceases to exist at the end of the if statement's dependent block of code, and all of that memory is made available to the system. On the next iteration, it will probably be used for array b, which is why you'll see values of 8 printed in array b, even though they were assigned to a.



          for (size_t i=1; i<=2; i++)
          {
          if( i&1 )
          {
          printf("round %c:", i+'0');
          int a[10];
          show_array(a, 10);
          for (size_t j=0;j<10;j++) a[j]=8;
          }
          else
          {
          printf("round %c:", i+'0');
          int b[10];
          show_array(b, 10);
          for (size_t j=0;j<10;j++) b[j]=888;
          }
          }


          For a final confirmation that memory is being re-used, you could modify show_array to print the raw pointer passed in, not just the individual elements. You should see the same address each time.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 20:32

























          answered Nov 19 at 20:24









          Tim Randall

          1,8091118




          1,8091118












          • Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
            – ptr_user7813604
            Nov 19 at 20:27






          • 1




            An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
            – Dietrich Epp
            Nov 19 at 20:28










          • What @DietrichEpp said. I'll add anote to the answer
            – Tim Randall
            Nov 19 at 20:28










          • @DietrichEpp: oh I got it!
            – ptr_user7813604
            Nov 19 at 20:29










          • Thank you sir I do believe you but it's just my style of asking.
            – ptr_user7813604
            Nov 19 at 20:57


















          • Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
            – ptr_user7813604
            Nov 19 at 20:27






          • 1




            An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
            – Dietrich Epp
            Nov 19 at 20:28










          • What @DietrichEpp said. I'll add anote to the answer
            – Tim Randall
            Nov 19 at 20:28










          • @DietrichEpp: oh I got it!
            – ptr_user7813604
            Nov 19 at 20:29










          • Thank you sir I do believe you but it's just my style of asking.
            – ptr_user7813604
            Nov 19 at 20:57
















          Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
          – ptr_user7813604
          Nov 19 at 20:27




          Is that when the array a be automatic storage, it mean the array itself is automatic or the elements in the array be automatic?
          – ptr_user7813604
          Nov 19 at 20:27




          1




          1




          An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
          – Dietrich Epp
          Nov 19 at 20:28




          An array has the same storage class as the elements inside it (how would an element inside an array outlive the array?)
          – Dietrich Epp
          Nov 19 at 20:28












          What @DietrichEpp said. I'll add anote to the answer
          – Tim Randall
          Nov 19 at 20:28




          What @DietrichEpp said. I'll add anote to the answer
          – Tim Randall
          Nov 19 at 20:28












          @DietrichEpp: oh I got it!
          – ptr_user7813604
          Nov 19 at 20:29




          @DietrichEpp: oh I got it!
          – ptr_user7813604
          Nov 19 at 20:29












          Thank you sir I do believe you but it's just my style of asking.
          – ptr_user7813604
          Nov 19 at 20:57




          Thank you sir I do believe you but it's just my style of asking.
          – ptr_user7813604
          Nov 19 at 20:57












          up vote
          4
          down vote













          "Automatic storage class" refers to the scope in which the variable is defined. It means that the memory allocated to the variable will be freed upon exiting the scope in which the variable was created, not that the memory will be initialized for you. You still have to do that yourself. The reason you see the 8s in the second iteration of your loop is because the same memory that was freed in the previous iteration was reassigned to the array.






          share|improve this answer

























            up vote
            4
            down vote













            "Automatic storage class" refers to the scope in which the variable is defined. It means that the memory allocated to the variable will be freed upon exiting the scope in which the variable was created, not that the memory will be initialized for you. You still have to do that yourself. The reason you see the 8s in the second iteration of your loop is because the same memory that was freed in the previous iteration was reassigned to the array.






            share|improve this answer























              up vote
              4
              down vote










              up vote
              4
              down vote









              "Automatic storage class" refers to the scope in which the variable is defined. It means that the memory allocated to the variable will be freed upon exiting the scope in which the variable was created, not that the memory will be initialized for you. You still have to do that yourself. The reason you see the 8s in the second iteration of your loop is because the same memory that was freed in the previous iteration was reassigned to the array.






              share|improve this answer












              "Automatic storage class" refers to the scope in which the variable is defined. It means that the memory allocated to the variable will be freed upon exiting the scope in which the variable was created, not that the memory will be initialized for you. You still have to do that yourself. The reason you see the 8s in the second iteration of your loop is because the same memory that was freed in the previous iteration was reassigned to the array.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 19 at 20:30









              mnistic

              6,6131820




              6,6131820






















                  up vote
                  3
                  down vote













                  The array a is an automatic variable because it is defined in a block scope, the block in this case being the body of the for loop. The fact that you see the values you set the last time doesn't mean the variable is static.



                  Automatic variables are uninitialized if not explicitly initialized. This basically means they'll contain whatever happened to be in those memory locations the last time they were used.



                  On the first iteration of the loop, you see seemingly random values in the array. The variable then goes out of scope at the end of the loop, and on the next iteration a new instance is created, but in this case it happened to be created at the same memory location it was created at last time, and nothing else happened to write to that memory location, so you end up seeing the same values. There's no guarantee that you'll see the same behavior if you for example make an unrelated code change or compile with different optimization settings.






                  share|improve this answer



























                    up vote
                    3
                    down vote













                    The array a is an automatic variable because it is defined in a block scope, the block in this case being the body of the for loop. The fact that you see the values you set the last time doesn't mean the variable is static.



                    Automatic variables are uninitialized if not explicitly initialized. This basically means they'll contain whatever happened to be in those memory locations the last time they were used.



                    On the first iteration of the loop, you see seemingly random values in the array. The variable then goes out of scope at the end of the loop, and on the next iteration a new instance is created, but in this case it happened to be created at the same memory location it was created at last time, and nothing else happened to write to that memory location, so you end up seeing the same values. There's no guarantee that you'll see the same behavior if you for example make an unrelated code change or compile with different optimization settings.






                    share|improve this answer

























                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      The array a is an automatic variable because it is defined in a block scope, the block in this case being the body of the for loop. The fact that you see the values you set the last time doesn't mean the variable is static.



                      Automatic variables are uninitialized if not explicitly initialized. This basically means they'll contain whatever happened to be in those memory locations the last time they were used.



                      On the first iteration of the loop, you see seemingly random values in the array. The variable then goes out of scope at the end of the loop, and on the next iteration a new instance is created, but in this case it happened to be created at the same memory location it was created at last time, and nothing else happened to write to that memory location, so you end up seeing the same values. There's no guarantee that you'll see the same behavior if you for example make an unrelated code change or compile with different optimization settings.






                      share|improve this answer














                      The array a is an automatic variable because it is defined in a block scope, the block in this case being the body of the for loop. The fact that you see the values you set the last time doesn't mean the variable is static.



                      Automatic variables are uninitialized if not explicitly initialized. This basically means they'll contain whatever happened to be in those memory locations the last time they were used.



                      On the first iteration of the loop, you see seemingly random values in the array. The variable then goes out of scope at the end of the loop, and on the next iteration a new instance is created, but in this case it happened to be created at the same memory location it was created at last time, and nothing else happened to write to that memory location, so you end up seeing the same values. There's no guarantee that you'll see the same behavior if you for example make an unrelated code change or compile with different optimization settings.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 at 20:36

























                      answered Nov 19 at 20:27









                      dbush

                      90.7k12100131




                      90.7k12100131






















                          up vote
                          2
                          down vote













                          When the lifetime of an object with automatic storage duration ends, that just means the memory is released (so that it may be used for other purposes). It does not guarantee that the memory is erased or that it is used for other purposes.



                          When the lifetime of another instance of the same object begins, memory is allocated for it again. It may happen to be the same memory that was used for it before. There is no guarantee that it was erased or used for other purposes.






                          share|improve this answer

























                            up vote
                            2
                            down vote













                            When the lifetime of an object with automatic storage duration ends, that just means the memory is released (so that it may be used for other purposes). It does not guarantee that the memory is erased or that it is used for other purposes.



                            When the lifetime of another instance of the same object begins, memory is allocated for it again. It may happen to be the same memory that was used for it before. There is no guarantee that it was erased or used for other purposes.






                            share|improve this answer























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              When the lifetime of an object with automatic storage duration ends, that just means the memory is released (so that it may be used for other purposes). It does not guarantee that the memory is erased or that it is used for other purposes.



                              When the lifetime of another instance of the same object begins, memory is allocated for it again. It may happen to be the same memory that was used for it before. There is no guarantee that it was erased or used for other purposes.






                              share|improve this answer












                              When the lifetime of an object with automatic storage duration ends, that just means the memory is released (so that it may be used for other purposes). It does not guarantee that the memory is erased or that it is used for other purposes.



                              When the lifetime of another instance of the same object begins, memory is allocated for it again. It may happen to be the same memory that was used for it before. There is no guarantee that it was erased or used for other purposes.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 19 at 20:27









                              Eric Postpischil

                              69.6k874150




                              69.6k874150






















                                  up vote
                                  2
                                  down vote













                                  Your program has what is scientifically known as undefined behaviour. In your case it results from using (printing) a bunch of int objects before initialising them.



                                  Since the behaviour is undefined, program output can be anything at all, including a stream of characters that could possibly in theory lead someone into believing in an obvious nonsense (like a having static storage duration).



                                  In practical terms, this is what happens if you ignore the high-brow world of language standards and look at the iron:




                                  • At round 1 a contains garbage values, remnants of whatever has been stored at that memory location previously.

                                  • At round 2 a still contains garbage values, remnants of whatever has been stored at that memory location previously.


                                  The only difference is that the garbage looks familiar to you, because it happens to be your garbage. You can see it because the garbage people did not have a chance to collect it yet. It is still garbage however — the garbage people will remove it at some point.






                                  share|improve this answer



























                                    up vote
                                    2
                                    down vote













                                    Your program has what is scientifically known as undefined behaviour. In your case it results from using (printing) a bunch of int objects before initialising them.



                                    Since the behaviour is undefined, program output can be anything at all, including a stream of characters that could possibly in theory lead someone into believing in an obvious nonsense (like a having static storage duration).



                                    In practical terms, this is what happens if you ignore the high-brow world of language standards and look at the iron:




                                    • At round 1 a contains garbage values, remnants of whatever has been stored at that memory location previously.

                                    • At round 2 a still contains garbage values, remnants of whatever has been stored at that memory location previously.


                                    The only difference is that the garbage looks familiar to you, because it happens to be your garbage. You can see it because the garbage people did not have a chance to collect it yet. It is still garbage however — the garbage people will remove it at some point.






                                    share|improve this answer

























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      Your program has what is scientifically known as undefined behaviour. In your case it results from using (printing) a bunch of int objects before initialising them.



                                      Since the behaviour is undefined, program output can be anything at all, including a stream of characters that could possibly in theory lead someone into believing in an obvious nonsense (like a having static storage duration).



                                      In practical terms, this is what happens if you ignore the high-brow world of language standards and look at the iron:




                                      • At round 1 a contains garbage values, remnants of whatever has been stored at that memory location previously.

                                      • At round 2 a still contains garbage values, remnants of whatever has been stored at that memory location previously.


                                      The only difference is that the garbage looks familiar to you, because it happens to be your garbage. You can see it because the garbage people did not have a chance to collect it yet. It is still garbage however — the garbage people will remove it at some point.






                                      share|improve this answer














                                      Your program has what is scientifically known as undefined behaviour. In your case it results from using (printing) a bunch of int objects before initialising them.



                                      Since the behaviour is undefined, program output can be anything at all, including a stream of characters that could possibly in theory lead someone into believing in an obvious nonsense (like a having static storage duration).



                                      In practical terms, this is what happens if you ignore the high-brow world of language standards and look at the iron:




                                      • At round 1 a contains garbage values, remnants of whatever has been stored at that memory location previously.

                                      • At round 2 a still contains garbage values, remnants of whatever has been stored at that memory location previously.


                                      The only difference is that the garbage looks familiar to you, because it happens to be your garbage. You can see it because the garbage people did not have a chance to collect it yet. It is still garbage however — the garbage people will remove it at some point.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 19 at 21:06

























                                      answered Nov 19 at 20:48









                                      n.m.

                                      70.3k882165




                                      70.3k882165






























                                          draft saved

                                          draft discarded




















































                                          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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382014%2fc-array-of-automatic-storage-class%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

                                          404 Error Contact Form 7 ajax form submitting

                                          How to know if a Active Directory user can login interactively

                                          Refactoring coordinates for Minecraft Pi buildings written in Python