Note that there are some explanatory texts on larger screens.

plurals
  1. POSafe Delete in C++
    primarykey
    data
    text
    <p>I have developed an array based implementation of a hashTable with several stock names, symbols, prices, and the like. I need to remove a stock from my array. I am told that using the delete operator is bad object oriented design. What is the good object oriented design for deletion?</p> <pre><code>bool hash::remove(char const * const symbol, stock &amp;s, int&amp; symbolHash, int&amp; hashIndex, int&amp; usedIndex) { symbolHash = this-&gt;hashStr( symbol ); // hash to try to reduce our search. hashIndex = symbolHash % maxSize; usedIndex = hashIndex; if ( hashTable[hashIndex].symbol != NULL &amp;&amp; strcmp( hashTable[hashIndex].symbol , symbol ) == 0 ) { delete hashTable[hashIndex].symbol; hashTable[hashIndex].symbol = NULL; return true; } for ( int myInt = 0; myInt &lt; maxSize; myInt++ ) { ++usedIndex %= maxSize; if ( hashTable[usedIndex].symbol != NULL &amp;&amp; strcmp( hashTable[usedIndex].symbol , symbol ) == 0 ) { delete hashTable[usedIndex].symbol; hashTable[usedIndex].symbol = NULL; return true; } } return false; } </code></pre> <p>Noticing that i have a stock &amp;s as a parameter, i can use it like this:</p> <pre><code>s = &amp;hashTable[usedIndex]; delete s.symbol; s.symbol = NULL; hashTable[usedIndex] = &amp;s; </code></pre> <p>This does work however, it results in a memory leaks. Even then, i am not sure if it is good object orinted design.</p> <p>here is my header, where stock and all that stuff is initialized and defined.</p> <p>//hash.h</p> <pre><code>private: friend class stock; int isAdded; // Will contain the added hash index. // Test for empty tables. // Can possibly make searches efficient. stock *hashTable; // the hashtable will hold all the stocks in an array }; </code></pre> <p>// hashtable ctor</p> <pre><code>hash::hash(int capacity) : isAdded(0), hashTable(new stock[capacity]) // allocate array with a fixed size { if ( capacity &lt; 1 ) exit(-1); maxSize = capacity; // We can initialize our attributes for the stock // to NULL, and test for that when searching. for ( int index = 0; index &lt; maxSize; index++ ) { hashTable[index].name = NULL; hashTable[index].sharePrice = NULL; hashTable[index].symbol = NULL; } } </code></pre> <p>// stock.h</p> <p>... friend class hashmap; </p> <pre><code>private: const static int maxSize; // holds the capacity of the hash table minus one date priceDate; // Object for the date class. Holds its attributes. char *symbol; char *name; int sharePrice; }; </code></pre> <p>My question is still just, how do i preform a safe remove?</p> <pre><code>s = &amp;hashTable[usedIndex]; delete s.symbol; s.symbol = NULL; hashTable[usedIndex] = &amp;s; </code></pre> <p>That seems to work, but results in memory leaks! How is this done safely?</p> <p>delete hashTable[usedIndex].symbol; hashTable[usedIndex].symbol = NULL; &lt;-- without doing this.</p> <p>The status of the slot in the array (empty, etc) should not be recorded in the stock instance. That's bad object oriented design. Instead, I need to store the status of an array slot in the array slot itself. </p> <p>How would i do that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload