我得到訪問衝突讀取位置0xCDCDCDD1錯誤。我正在使用vc9編譯器。new_srdProg_d.exe中的0x0040224d未處理的異常:0xC0000005:訪問衝突讀取位置0xcdcdcdd1
我的代碼:
#define SRD_EXPORTS
#include "SRD.h"
#include "ISRDPresenter.h"
#include "ISRDFactory.h"
#include "ISRDevice.h"
#include "AString.h"
int main(int argc, char** argv)
{
ISRDPresenter* dev = (ISRDPresenter*)malloc(sizeof(ISRDPresenter)); // = NewSRDPresenter();
char act[ 128 ];
int commands = argc-1;
do
{
if(!commands)
{
act[ 0 ] = 0;
printf("SRD> ");
#if defined(__IBMC__) || defined (__IBMCPP__)
fflush(stdout);
#endif
fgets(act, 128, stdin);
}
else
{
strncpy(act, argv[ argc - commands-- ], 128);
printf("SRD> %s\n", act);
}
if(!strncmp(act, "help", 4))
{
dev->showHelp(act);
}
...
}
的錯誤是在該行的dev-> showHelp(行爲); 問題應該是在該行
ISRDPresenter* dev = (ISRDPresenter*)malloc(sizeof(ISRDPresenter));
但我不能修復它。 如果我使用來代替:
ISRDPresenter* dev = NewSRDPresenter();
它說標識符無法找到,如果我使用:
ISRDPresenter* dev = new ISRDPresenter();
說ISRDPresenter不能實例化抽象類...
ISRDPresenter.h:
#ifndef __ISRDPRESENTER_H
#define __ISRDPRESENTER_H
#define SRD_EXPORTS
#include "SRD.h"
#include "ISRDDataTypes.h"
#include "AIPClass.h"
class ISRDFactory;
class ISRDPresenter
{
public:
// -----------------------------------------------------------------------
// Destructor
// -----------------------------------------------------------------------
virtual ~ISRDPresenter() {};
// -----------------------------------------------------------------------
// Function prototypes
// -----------------------------------------------------------------------
virtual void showHelp(char* attr) const = 0;
virtual void showList(void) const = 0;
virtual void showInfo(size_t i) const = 0;
virtual void showFirmware(size_t i) const = 0;
};
#endif /* __ISRDPRESENTER_H */
SRDPresenter.h:
#ifndef __SRDPRESENTER_H
#define __SRDPRESENTER_H
#define SRD_EXPORTS
#include "SRD.h"
#include "ISRDPresenter.h"
#include "AString.h"
#include "ISRDFactory.h"
//-----------------------------------------------------------------------------
// ASRDPresenter
//-----------------------------------------------------------------------------
class ASRDPresenter : public ISRDPresenter
{
public:
//-------------------------------------------------------------------------
// ASRDPresenter constructor & destructor
//-------------------------------------------------------------------------
ASRDPresenter();
virtual ~ASRDPresenter(){};
//-------------------------------------------------------------------------
// Function prototypes
//-------------------------------------------------------------------------
void showHelp(char* attr ) const;
void showList(void) const;
void showInfo(size_t i) const;
void showFirmware(size_t i) const;
private:
ISRDFactory* f_srds;
};
SRD_EXPORT_PREF ISRDPresenter* SRD_EXPORT_POST NewSRDPresenter()
{
return new ASRDPresenter();
}
#endif /* __SRDPRESENTER_H */
SRDPresenter.cpp:
#define SRD_EXPORTS
#include "SRDPresenter.h"
#if defined(_MSC_VER)
#define WIN32_LEAN_AND_MEAN
#endif
// ---------------------------------------------------------------------------
// ASRDPresenter
// ---------------------------------------------------------------------------
ASRDPresenter::ASRDPresenter()
:f_srds()
{
// empty
}
void ASRDPresenter::showHelp(char* attr) const
{
if(!strcmp(attr, "help"))
{
printf("\nHelp to the ANOVIS SRD command.\n\n");
printf(" help : Print this help.\n");
printf(" list : List all ANOVIS SRD found.\n");
printf(" info
}
...
}
我怎樣才能解決呢?光! Thanx提前!
爲什麼malloc的內C++? – user1810087
你爲什麼要嘗試實例化ISRDPresenter,它是一個抽象類! –
不要發送垃圾郵件標籤。 C不是C++不是C! – Olaf