gpt4 book ai didi

c++ - C++ 中的安全删除

转载 作者:行者123 更新时间:2023-11-27 22:31:39 25 4
gpt4 key购买 nike

我开发了一个基于数组的 hashTable 实现,其中包含多个股票名称、符号、价格等。我需要从我的数组中删除一个股票。有人告诉我,使用 delete 运算符是糟糕的面向对象设计。什么是好的面向对象的删除设计?

bool hash::remove(char const * const symbol, stock &s,
int& symbolHash, int& hashIndex, int& usedIndex)
{
symbolHash = this->hashStr( symbol ); // hash to try to reduce our search.
hashIndex = symbolHash % maxSize;
usedIndex = hashIndex;

if ( hashTable[hashIndex].symbol != NULL &&
strcmp( hashTable[hashIndex].symbol , symbol ) == 0 )
{
delete hashTable[hashIndex].symbol;
hashTable[hashIndex].symbol = NULL;
return true;
}
for ( int myInt = 0; myInt < maxSize; myInt++ )
{
++usedIndex %= maxSize;
if ( hashTable[usedIndex].symbol != NULL &&
strcmp( hashTable[usedIndex].symbol , symbol ) == 0 )
{
delete hashTable[usedIndex].symbol;
hashTable[usedIndex].symbol = NULL;
return true;
}
}
return false;
}

注意到我有一个股票 &s 作为参数,我可以像这样使用它:

s = &hashTable[usedIndex];
delete s.symbol;
s.symbol = NULL;
hashTable[usedIndex] = &s;

这确实有效,但是会导致内存泄漏。即使那样,我也不确定它是否是良好的面向对象的设计。

这是我的标题,其中初始化和定义了库存和所有其他内容。

//哈希.h

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

};

//哈希表构造函数

hash::hash(int capacity) : isAdded(0),
hashTable(new stock[capacity]) // allocate array with a fixed size
{
if ( capacity < 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 < maxSize; index++ )
{
hashTable[index].name = NULL;
hashTable[index].sharePrice = NULL;
hashTable[index].symbol = NULL;
}
}

//股票.h

... friend 类 HashMap ;

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;
};

我的问题仍然只是,如何执行安全删除?

s = &hashTable[usedIndex];
delete s.symbol;
s.symbol = NULL;
hashTable[usedIndex] = &s;

这似乎可行,但会导致内存泄漏!这是如何安全完成的?

删除 hashTable[usedIndex].symbol;hashTable[usedIndex].symbol = NULL; <-- 不这样做。

数组中插槽的状态(空等)不应记录在库存实例中。那是糟糕的面向对象设计。相反,我需要将数组槽的状态存储在数组槽本身中。

我该怎么做?

最佳答案

NOTE: This answer is just addressing some of the things you are doing incorrectly. This is not the best way to do what you are doing. An array of stock** would make more sense.

你的股票类没有构造函数吗?如果它是一个适当的对象,你不需要知道任何关于股票类的信息:

hash::hash(int capacity) // change this to unsigned and then you can't have capacity < 0
: isAdded(0)
, hashTable(0) // don't call new here with bad parameters
{
if ( capacity < 1 ) exit(-1); // this should throw something, maybe bad_alloc

maxSize = capacity;
hashTable = new stock[capacity]; // this calls the stock() constructor

// constructor already called. All this code is useless
// We can initialize our attributes for the stock
// to NULL, and test for that when searching.
// for ( int index = 0; index < maxSize; index++ )
// {
// hashTable[index].name = NULL;
// hashTable[index].sharePrice = NULL;
// hashTable[index].symbol = NULL;
// }
}

class stock {
char* name; // these should be std::string as it will save you many headaches
char* sharePrice; // but I'll do it your way here so you can see how to
char* symbol; // avoid memory leaks
public:
stock() : name(0), sharePrice(0), symbol(0) {}
~stock() { delete[] name; delete[] sharePrice; delete[] symbol; }
setName(const char* n) { name = new char[strlen(n)+1]; strcpy(name, n); }
setPrice(const char* p) { sharePrice = new char[strlen(p)+1]; strcpy(sharePrice, p); }
setSymbol(const char* s) { symbol = new char[strlen(s)+1]; strcpy(symbol, n); }

const char* getName() const { return name; }
const char* getPrice() const { return sharePrice; }
const char* getSymbol() const { return symbol; }
}

关于c++ - C++ 中的安全删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1774133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com