0
在一個大的,單線程,C++應用程序(編譯GCC 4.4.7 20120313),則簡單的指針分配不等於原來的指針值:C++指針分配不工作(斷開由34個字節)
class DvComVectorStreamBase : virtual public std::ios
{
// some stuff here
};
class DvComVectorOStream : public DvComVectorStreamBase, public std::ostream
{
public:
DvComVectorOStream(int which = std::ios::out, size_t capacity = 0) :
DvComVectorStreamBase(which, capacity)
{}
};
class Formatter : public aStandAloneClass
{
// cruft removed
protected:
void initFunction();
ostream* mpStream;
DvComVectorOStream* mpVectorOStream;
}
Formatter:initFunction()
{
printf("mpStream %p mpVectorOStream %p\n", (void *)mpStream, (void *)mpVectorOStream);
if (mpVectorOStream == 0)
mpVectorOStream = new DvComVectorOStream(ios::out, 32768);
else
mpVectorOStream->clear();
printf("mpStream %p mpVectorOStream %p\n", (void *)mpStream, (void *)mpVectorOStream);
mpStream = mpVectorOStream;
printf("mpStream %p mpVectorOStream %p\n", (void *)mpStream, (void *)mpVectorOStream);
}
輸出
mpStream (nil) mpVectorOStream (nil)
mpStream (nil) mpVectorOStream 0x90ccc98
mpStream 0x90ccccc mpVectorOStream 0x90ccc98
試圖重新創建一個平凡的測試情況下,問題能正常工作。
mpStream和mpVectorOStream在賦值後不應該有相同的值嗎?
即。如果兩個指針都指向同一個對象,那麼每個指針應該包含相同的值,不是? (是不是分配運算符的整個點?)
nil?那是什麼編譯器? –
看起來像叮噹對我。 – mostruash
您可能正在做多重繼承,在這種情況下,基類指針通常與派生類不同。 – interjay