title: Need Help Solving a Memory Leak
body: <p>I am writing an inventory management system using dynamic arrays using objects. The strategy for this is NOT to use vectors, but allocate a dynamic array that 1 increment by 1 every time the client needs to add to the inventory. I am getting a "segmentation fault" error so I believe it is a memory leak. </p>
  
  <p>I have tried rewriting it to match addresses but no luck. I think the buildItem function produces a temporary object, which is destroyed when the function ends. I don't know how to fix this though.</p>
  
  <pre><code>#include &lt;iomanip&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class InventoryObject {
  private:
    int itemNumber;
    string description;
    int qty;
    float price;
  
  public:
    int getItemNum() { return itemNumber; }
    string getDescription() { return description; }
    int getQty() { return qty; }
    float getPrice() { return price; }
  
    void storeInfo(int p, string d, int q, float pr);
    void showValues(InventoryObject &amp;item);
  };
  
  // Function Implementation
  void InventoryObject::storeInfo(int p, string d, int q, float pr) {
    itemNumber = p;
    description = d;
    qty = q;
    price = pr;
  }
  
  void InventoryObject::showValues(InventoryObject &amp;item) {
    cout &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2) &lt;&lt; endl;
    cout &lt;&lt; "Part Number  : " &lt;&lt; item.getItemNum() &lt;&lt; endl;
    cout &lt;&lt; "Description  : " &lt;&lt; item.getDescription() &lt;&lt; endl;
    cout &lt;&lt; "Quantity:    : " &lt;&lt; item.getQty() &lt;&lt; endl;
    cout &lt;&lt; "Price        : " &lt;&lt; item.getPrice() &lt;&lt; endl &lt;&lt; endl;
  }
  
  // Function Prototypes for Client Program
  InventoryObject buildItem();
  void drawMenu();
  void showValues(InventoryObject &amp;);
  void printInventory(int size);
  
  int main() {
    int size = 1;
    int choice;
    bool quit = false;
    InventoryObject part;
    InventoryObject *iArray = new InventoryObject[size];
    drawMenu();
    cin &gt;&gt; choice;
    while (quit == false) {
      if (choice == 1) {
        InventoryObject item;
        item = buildItem();
        iArray[size] = item;
      }
      if (choice == 2) {
        iArray[size].showValues(iArray[size]);
      }
      if (choice == 3) {
        quit = true;
      }
    }
  
    return 0;
  }
  
  // This function accepts the data from the client and creates a new
  // InventoryObject object. The object is then supposed to be added to the
  // dynamic array.
  InventoryObject buildItem() {
    InventoryObject *tempObject = new InventoryObject;
    int itemNum;
    string description;
    int qty;
    float price;
  
    cout &lt;&lt; "Enter data for the item you want to enter:\n\n";
    cout &lt;&lt; "Item Number: \n";
    cin &gt;&gt; itemNum;
    cout &lt;&lt; "Description: \n";
    cin.get();
    getline(cin, description);
    cout &lt;&lt; "Quantity: \n";
    cin &gt;&gt; qty;
    cout &lt;&lt; "Unit price: \n";
    cin &gt;&gt; price;
  
    tempObject-&gt;storeInfo(itemNum, description, qty, price);
    return *tempObject;
  }
  
  void drawMenu() {
    cout &lt;&lt; "1. Add Inventory\n";
    cout &lt;&lt; "2. Display Inventory\n";
    cout &lt;&lt; "3. Quit Program\n";
  }
  </code></pre>
  
  <p>I expect the object to be created, and put into the dynamic array. Then redraw the menu and interact with the client from there.</p>