If "InputQueue_c InputQueue; " is outside int main ,being declared as global, the code will compile but it will trigger a memory error and crash. if "InputQueue_c InputQueue; " is inside int main() it works fine, or so it seems. What am i doing wrong?
Code:#include <iostream> #include <windows.h> using namespace std; struct Queue_str { int iFlag; void * info; int iFrom; int iTo; Queue_str * next; }; struct Data_str { int iFlag; void * info; int iFrom; int iTo; }; Data_str * CreateData(void * pArg, int iFlag, int iFrom, int iTo) {Data_str * strData; strData=new Data_str; strData->info=pArg; strData->iFlag=iFlag; strData->iFrom=iFrom; strData->iTo=iTo; return strData; }; class InputQueue_c {public: int iCount; Queue_str * pLastRead; Queue_str * pStart; Queue_str * pEnd; void Input(Data_str * strData) { pEnd->next=new Queue_str; pEnd->next->info=strData->info; pEnd->next->iFlag=strData->iFlag; pEnd->next->iFrom=strData->iFrom; pEnd->next->iTo=strData->iTo; pEnd->next->next=NULL; pEnd=pEnd->next; iCount++; } Data_str * Read() { Data_str * strData; strData=new Data_str; if (pLastRead==pEnd) {strData->info=(void*) "\0"; strData->iFlag=-1; return strData; } Queue_str *pBuffer; pBuffer=pLastRead->next; pLastRead=pBuffer; strData->iFlag=pBuffer->iFlag; strData->info=pBuffer->info; strData->iFrom=pBuffer->iFrom; strData->iTo=pBuffer->iTo; return strData; } bool Unread() {if(pEnd==pLastRead) return 0; else return 1; } InputQueue_c(){iCount=0; pLastRead=pStart; pEnd=pStart; } ~InputQueue_c(){} }; InputQueue_c InputQueue; int main() { InputQueue.Input(CreateData((void*)"test",1, 0,0 )) ; //at this point it will crash cout<<(char*)InputQueue.Read()->info; cin.get(); }



LinkBack URL
About LinkBacks



