title: Object created in for-loop has same address
body: <p>I'm trying to make an own list-class which is of a fixed size and can store integers. Just for learning purposes.</p>
  
  <p>This is how I do it:
  I have a struct called Item which holds the data (the integer to store), and a pointer to the next item in the list.
  When the list gets initialized I first add x amount of empty Items to the list.</p>
  
  <p>The address of item declared in iteration (n-1) is stored in a buffer, so that the address of the item in iteration n, can be set as next for the item in the previous iteration (n-1).
  This to chain all items to each other.</p>
  
  <p>And the address of the very first item is also saved, as the access point for the whole list later in the code.</p>
  
  <p>But it's not really working; this is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class List {
      public:
      //Item-object needed to store
      //elements
      struct Item {
          int data = 0;
          Item* next = nullptr;
      };
  
      //Constructor
      //creates a list of fixed size
      List(int size) {
          //Filling up with empty items
          for (int i = 0; i &lt; size; i++) {
              Item item;
  
              //Storing iterator in item (just for testing purposes)
              item.data = i;
  
              //If first item,
              //store start-address
              //set buffer to start address
              if (i == 0) 
                  this-&gt;start = &amp;item;
  
              //Setting current item as nextptr
              //for previous item in buffer 
              if (i &gt; 0)
                  this-&gt;buffer-&gt;next = &amp;item;
  
              //Storing current address in buffer
              this-&gt;buffer = &amp;item;
  
              //Outputting address and value (just for testing purposes)
              //std::cout &lt;&lt; "Address: " &lt;&lt; &amp;item &lt;&lt; " -&gt; " &lt;&lt; item.data &lt;&lt; std::endl;
          }
      }
  
      Item* getFirstItemAddress() {
          return this-&gt;start;
      }
  
      private:
      //Holding address of first item
      Item* start = nullptr;
  
      //Buffer holding temporary address
      Item* buffer = nullptr;
  
  };
  
  int main() {
      List list(5);
  
      //Printing out
      List::Item* current = list.getFirstItemAddress();
      while (current-&gt;next) {
          std::cout &lt;&lt; current-&gt;data &lt;&lt; std::endl;
          current = current-&gt;next;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>Testing output:
  1168769696
  -1064971727
  Segmentation fault
  </code></pre>
  
  <p>However, when I uncomment testing-line 37 this is the output:</p>
  
  <pre><code>Address: 0x7ffe54015cf0 -&gt; 0
  Address: 0x7ffe54015cf0 -&gt; 1
  Address: 0x7ffe54015cf0 -&gt; 2
  Address: 0x7ffe54015cf0 -&gt; 3
  Address: 0x7ffe54015cf0 -&gt; 4
  
  Testing output:
  1648675776
  1648572376
  1646105840
  1226279756
  Segmentation fault
  </code></pre>
  
  <p>I don't understand in the first place how outputting can change the output of 'Testing output' this drastically...</p>
  
  <p>Anyway, segmentation fault info:</p>
  
  <pre><code>(gdb) run
  Starting program: /home/niel/Desktop/listTest/main 
  Address: 0x7fffffffe0a0 -&gt; 0
  Address: 0x7fffffffe0a0 -&gt; 1
  Address: 0x7fffffffe0a0 -&gt; 2
  Address: 0x7fffffffe0a0 -&gt; 3
  Address: 0x7fffffffe0a0 -&gt; 4
  
  Testing output:
  -136467520
  -136570920
  -139037456
  1226279756
  
  Program received signal SIGSEGV, Segmentation fault.
  0x000000000040092e in main () at main.cpp:62
  62              std::cout &lt;&lt; current-&gt;data &lt;&lt; std::endl;
  </code></pre>
  
  <p>No idea why current->data gives the segmentation fault, as I gave data to each element!</p>
  
  <p>But what bothers me most, is this:</p>
  
  <pre><code> Address: 0x7fffffffe0a0 -&gt; 0
      Address: 0x7fffffffe0a0 -&gt; 1
      Address: 0x7fffffffe0a0 -&gt; 2
      Address: 0x7fffffffe0a0 -&gt; 3
      Address: 0x7fffffffe0a0 -&gt; 4
  </code></pre>
  
  <p>Each item has the same address, of course, this isn't what I meant. Each item created in the iterations should be another item with another address.
  Has this something to do with Item being declared local, and the object for that reason is stored on the stack and not in free space? I tried using the 'new' keyword, but that didn't work!</p>
  
  <p><strong>TL;DR</strong>
  1) Why am I getting the segmentation fault
  2) How to fix that each Item created in List() has a separate address?</p>