class SimpleVariant
{
public:
SimpleVariant() { /*...*/ };
// ...
};
struct VariantBlock
{
int nRows, nCols;
vector<SimpleVariant> theData;
};
void dumbFunction(VariantBlock& theBlock, int nRows, int nCols)
{
// ...
cout << "theBlock.nRows= " << theBlock.nRows
<< ", theBlock.nCols= " << theBlock.nCols
<< ", theBlock.theData.size() " << theBlock.theData.size();
theBlock.theData.resize(nRows * nCols);
// throws Access Violation Exception
// ...
}
輸出返回nRows = 61,nCols = 5,size()= 0,這正是它應該在那一點上,在引發訪問衝突異常之前。Soul-crushing C++ std :: vector :: resize()訪問衝突錯誤
我正在使用MSVC6,這顯然不是最佳的,但在這一點上沒有選擇。
你在'SimpleVariant'中有什麼類型的成員?你是否爲'SimpleVariant'聲明瞭複製構造函數和/或複製賦值運算符?如果是這樣,他們做什麼?您發佈的代碼沒有任何明顯的錯誤,儘管我最近沒有使用VC6的經驗。 – 2011-03-26 05:18:21
我沒有定義它們。 – Doug 2011-03-26 05:19:36
這可能是壞的複製行爲,正如詹姆斯指出的那樣,但它也可能是程序中其他地方的內存破壞。 – templatetypedef 2011-03-26 05:20:46