何らかの理由で「無効な読み取りサイズ4」エラーが発生しました。わかりません。私はオンラインで回答を探しましたが、それは私のコードではあまり役に立ちませんでした。私のコードが何をしているかを要約すると、Stock Paper Trading Simulatorです。ユーザーにはオプションが表示され、ユーザーは何をしたいのかを選択できます。今、私は「売る」という問題を抱えています。それはそれがどのように見えるかです。C++無効な読み取りサイズ4 valgrind
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
}
view-> 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 *)です。
==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)
だから私はそれを見つけた後、私の株式クラスからの情報にアクセスするに何か問題があることを私に言っていますということを理解:
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の言っていることですそれはDlistにありますが、なぜそれを修正するのか、私は本当に分かりません。どんな助けでも大歓迎です。ありがとうございました。
私は '証券* S = stocks-> findElement(newStock)場合は'戻っうん、私はsがnullであると私はそれが出たかどうかを確認するためにチェックを入れて – vu1p3n0x
をゼロチェックされていません注意してください。今私は問題を追跡している、と私は問題が来ているように私の演算子がオーバーロードされて==ため、アイテム - > getSymbol()とcurrNode - >データ - > getSymbol() (私がテスト用に入れたもの)しかし、currNode-> data == itemは何らかの理由でfalseを返すようです。 – Str8UpEliTe