由于某种原因,我收到“读取大小4无效”错误,我无法弄清楚。我在网上搜索了答案,但这对我的代码没有帮助。总结我的代码的作用,它是一个库存交易模拟器。将显示给用户选项,然后他们可以选择自己想做的事情。目前,我在“出售股票”时遇到问题。这是它的样子。
Stock* newStock = new Stock();
view->getStockData(newStock);
Stock* s = stocks->findElement(newStock);
if(s->getAOS()-newStock->getAOS() > 0) { // There is still some shares remaining
// Haven't done this yet
} else if (s->getAOS()-newStock->getAOS() == 0) { // There is no shares remaining
double result = s->calculate(newStock->getPrice(), s->getPrice(), s->getAOS());
view->printResults(result, 0);
stocks->remove(newStock);
} else { // Resulted with a negative value
// Haven't done this yet
}
查看-> getStockData(newStock);看起来像这样:
void UImanager::getStockData(Stock* stock) {
// Initializing all the stock data
string str = "";
string symbol, companyName;
double price;
int amountOfShares;
cout << endl << "Enter the stock's symbol (e.g. AAPL): ";
getline(cin, symbol);
cout << endl << "Enter the companies name: ";
getline(cin, companyName);
cout << endl << "Enter the price of the stock: ";
getline(cin, str);
stringstream ss(str);
ss >> price;
str = "";
cout << endl << "Enter the amount of shares: ";
getline(cin, str);
stringstream ss1(str);
ss1 >> amountOfShares;
str = "";
Stock* tmpStock = new Stock(symbol, companyName, price, amountOfShares);
*stock = *tmpStock;
delete tmpStock;
}
股票类如下所示:
Stock::Stock(string s, string c, double p, int aOS) {
symbol = s;
companyName = c;
price = p;
amountOfShares = aOS;
fee = 10;
}
string Stock::getSymbol() { return symbol; }
string Stock::getCompanyName() { return companyName; }
double Stock::getPrice() { return price; }
int Stock::getAOS() { return amountOfShares; }
int Stock::getFee() { return fee; }
bool Stock::operator==(Stock& s) {
if (this->getSymbol() == s.getSymbol()) {
return true;
}
return false;
}
// More below this, but that code doesn't matter for this problem
我将股票存储在我制作的模板化Dlist中。这是findElement(T *):
template <class T>
T* Dlist<T>::findElement(T* item) {
Node<T>* currNode = head;
while (currNode != 0) { // iterate through the Dlist
if (currNode->data == item) { // uses the operator overloaded == from Stock
return currNode->data;
}
currNode = currNode->next;
}
// gets to this point if nothing was found
return 0;
}
这就是valgrind所说的:
==2459== Invalid read of size 4
==2459== at 0x804A4EC: Stock::getAOS() (in /home/student/Desktop/Stock Paper Trading/spt)
==2459== by 0x8049356: SPTcontrol::launch() (in /home/student/Desktop/Stock Paper Trading/spt)
==2459== by 0x8048F8D: main (in /home/student/Desktop/Stock Paper Trading/spt)
==2459== Address 0x10 is not stack'd, malloc'd or (recently) free'd
==2459==
==2459==
==2459== Process terminating with default action of signal 11 (SIGSEGV)
==2459== Access not within mapped region at address 0x10
==2459== at 0x804A4EC: Stock::getAOS() (in /home/student/Desktop/Stock Paper Trading/spt)
==2459== by 0x8049356: SPTcontrol::launch() (in /home/student/Desktop/Stock Paper Trading/spt)
==2459== by 0x8048F8D: main (in /home/student/Desktop/Stock Paper Trading/spt)
因此,我了解到这是在告诉我在Dlist中找到我的股票类信息后出现问题,但是我真的不明白为什么或如何解决它?任何帮助将不胜感激。谢谢。
最佳答案
在我看来,s
是空指针,因为Dlist::findElement
返回空指针。
最大的线索是valgrind抱怨的地址。您是否看到它说“地址0x10没有被堆栈,malloc或最近释放”?真正的地址如此接近零是非常不寻常的(读:几乎完全闻所未闻)。这几乎总是意味着您的代码遇到一个空指针,该指针在大多数系统上恰好由零地址表示,然后对其进行了一些算术运算(例如,如果Stock
位于地址0,那么其amountOfShares
在哪里? ?在地址16 = 0x10,也许)。
您可以通过添加一些显式检查空指针的代码,或在调试器中运行代码并逐步调试来检查此问题。
如果我的猜想是正确的(或者是错误的,但某些类似的猜想是正确的),这就是为什么要从Dlist::findElement
获取空指针。但我会让你自己解决这个问题。
关于c++ - C++无效的读取大小4 valgrind,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36553112/