S
sajidazmi
Hi, I'm coding an Stock Order Entry System. The problem I'm facing is
when I'm passing a pointer to one class, the pointer gets corrupted.
I'm not able to see why? Here is the code, please tell me what I'm
doing wrong.
Problem are marked as <== in the code.
/** Class Header **/
/*
* Symbol
*/
class CSymbol
{
private:
char symbol [15] ;
char group_ [3] ;
char state [2] ;
char description [41] ;
float price ;
long boardlot ;
char flag_ [9];
CSymbol *next ;
CBuyOrder *firstbuyOrder ;
CSellOrder *firstsellOrder ;
public:
CSymbol (char *p_symbol, char *p_group_, char *p_state,
char *p_description, long long p_price, long p_boardlot,
char *p_flag_ ) ;
~CSymbol () ;
void SetBuyOrder ( CBuyOrder *buyOrder ) ;
void SetSellOrder ( CSellOrder *sellOrder ) ;
friend class CSymbolList ;
} ;
class CSymbolList
{
private:
int count ;
int error ;
CSymbol *head ;
void Insert ( char *p_symbol, char *p_group_, char *p_state,
char *p_description, long long p_price, long p_boardlot,
char *p_flag_ ) ;
public:
CSymbolList () ;
~CSymbolList () ;
int Load () ;
CSymbol *FindSymbol ( char *symbol ) ;
} ;
/*
* Buy Order
*/
class CBuyOrder
{
private:
char symbol [15] ;
float price ;
char broker [9] ;
char time [27] ;
long volume ;
CBuyOrder *next ;
public:
CBuyOrder (CSymbolList *symbollist,
char *symbol, float price, char *broker, char *time, long
volume ) ;
~CBuyOrder () ;
friend class CBuyOrderList ;
} ;
/*
* Buy Order List
*/
class CBuyOrderList
{
private:
int count ;
int error ;
CBuyOrder *head ;
CSymbolList *symbollist ;
void Insert ( char *symbol, float price, char *broker, char *time,
long volume ) ;
public:
CBuyOrderList (CSymbolList *symbollist ) ;
~CBuyOrderList () ;
int Load (void) ;
} ;
/*
***************************************************************************
* Symbols
***************************************************************************
*/
CSymbol::CSymbol ( char *p_symbol, char *p_group_, char *p_state,
char *p_description, long long p_price,
long p_boardlot, char *p_flag_ )
{
strcpy ( symbol, p_symbol ) ;
strcpy ( group_, p_group_ ) ;
strcpy (state, p_state ) ;
strcpy ( description, p_description ) ;
price = ((float) p_price ) / 1000 ;
boardlot = p_boardlot ;
strcpy ( flag_, p_flag_ ) ;
next = NULL ;
firstbuyOrder = NULL ;
firstsellOrder = NULL ;
}
void CSymbol::SetBuyOrder ( CBuyOrder *buyOrder )
{
firstbuyOrder = buyOrder ;
}
void CSymbol::SetSellOrder ( CSellOrder *sellOrder )
{
firstsellOrder = sellOrder ;
}
/*
***************************************************************************
* Symbols List
***************************************************************************
*/
CSymbolList::CSymbolList ()
{
OpenSymbol () ;
head = NULL ;
}
CSymbolList::~CSymbolList ()
{
CloseSymbol () ;
}
int CSymbolList::Load ()
{
char l_symbol [15] ;
char l_group_ [3] ;
char l_state [2] ;
char l_description [41] ;
long long l_price ;
long l_boardlot ;
char l_flag_ [9] ;
while ( ( error = FetchSymbol () ) == 0 )
{
getSymbolFields (
l_symbol,
l_group_,
l_state,
l_description,
&l_price,
&l_boardlot,
l_flag_ ) ;
Insert ( l_symbol, l_group_, l_state, l_description, l_price,
l_boardlot, l_flag_ ) ;
count++ ;
}
if ( error == NO_ROWS )
error = 0 ;
return error ;
}
void CSymbolList::Insert ( char *p_symbol, char *p_group_, char
*p_state,
char *p_description, long long p_price, long p_boardlot,
char *p_flag_ )
{
CSymbol *current, *previous, *newSymbol ;
newSymbol = new CSymbol ( p_symbol, p_group_, p_state,
p_description,
p_price, p_boardlot, p_flag_ ) ;
if ( head == NULL )
head = newSymbol;
else
{
current = head ;
previous = NULL ;
while ( current != NULL && strncmp ( p_symbol, current->symbol,
LEN_SYMBOL ) > 0 )
{
previous = current ;
current = current->next ;
}
if ( current == head )
{
head = newSymbol ;
newSymbol->next = current ;
} else {
newSymbol->next = current ;
previous->next = newSymbol ;
}
}
}
CSymbol * CSymbolList::FindSymbol ( char *p_symbol )
{
CSymbol *current ;
current = this->head ;
while ( current != NULL && strncmp ( p_symbol, current->symbol,
LEN_SYMBOL) <= 0 )
{
cout << "S: " << current->symbol << "\n" ;
current = current->next ;
}
if ( strncmp ( p_symbol, current->symbol, LEN_SYMBOL) == 0 )
return current ;
else
return NULL ;
}
/*
****************************************************************************
* BuyOrders
****************************************************************************
*/
CBuyOrder::CBuyOrder ( CSymbolList *symbollist,
char *psymbol, float pprice, char *pbroker, char *ptime, long
pvolume )
{
<== PROBLEM // *symbollist is getting corrupted here
CSymbol *thisSymbol ;
strcpy ( symbol, psymbol ) ;
price = pprice ;
strcpy ( broker, pbroker ) ;
strcpy (time, ptime ) ;
volume = pvolume ;
next = NULL ;
cout << "Sl Cnt" << symbollist->count << "\n" ;
thisSymbol = symbollist->FindSymbol ( psymbol ) ; <== / getting
garbage
cout << "TSym " << thisSymbol->CSymbol << "\n" ;
if ( thisSymbol != NULL )
thisSymbol->SetBuyOrder ( this ) ;
else
cout << "BuyOrder without a Stock Symbol " << psymbol << "\n" ;
}
/*
***************************************************************************
* Buy Orders List
***************************************************************************
*/
CBuyOrderList::CBuyOrderList ( CSymbolList *p_symbollist )
{
OpenBuyOrder () ;
head = NULL ;
symbollist = p_symbollist ;
}
CBuyOrderList::~CBuyOrderList ()
{
CloseBuyOrder () ;
}
void CBuyOrderList::Insert ( char *p_symbol, float p_price, char
*p_broker,
char *p_time, long p_volume )
{
CBuyOrder *current, *previous, *newOrder ;
cout << "Sl Cnt" << symbollist->count << "\n" ;
<== PROBLEM // at this point *symbollist appears okay
newOrder = new CBuyOrder ( symbollist, p_symbol, p_price, p_broker,
p_time, p_volume ) ;
if ( head == NULL )
head = newOrder ;
else
{
current = head ;
previous = NULL ;
while ( current != NULL )
{
previous = current ;
current = current->next ;
}
if ( current == head )
{
head = newOrder ;
newOrder->next = current ;
} else {
newOrder->next = current ;
previous->next = newOrder ;
}
}
}
int CBuyOrderList::Load ( void )
{
char l_symbol [15] ;
float l_price ;
char l_broker [9] ;
char l_time [27] ;
long l_volume ;
while ( ( error = FetchBuyOrder () ) == 0 )
{
getBuyOrderFields (
l_symbol,
&l_price,
l_time,
l_broker,
&l_volume ) ;
Insert ( l_symbol, l_price, l_broker, l_time, l_volume ) ;
count++ ;
}
if ( error == NO_ROWS )
error = 0 ;
return error ;
}
void main ( void )
{
int c ;
CSymbolList symbollist ;
CBuyOrderList buyorderlist ( &symbollist ) ;
c = symbollist.Load () ;
c = buyorderlist.Load () ;
}
when I'm passing a pointer to one class, the pointer gets corrupted.
I'm not able to see why? Here is the code, please tell me what I'm
doing wrong.
Problem are marked as <== in the code.
/** Class Header **/
/*
* Symbol
*/
class CSymbol
{
private:
char symbol [15] ;
char group_ [3] ;
char state [2] ;
char description [41] ;
float price ;
long boardlot ;
char flag_ [9];
CSymbol *next ;
CBuyOrder *firstbuyOrder ;
CSellOrder *firstsellOrder ;
public:
CSymbol (char *p_symbol, char *p_group_, char *p_state,
char *p_description, long long p_price, long p_boardlot,
char *p_flag_ ) ;
~CSymbol () ;
void SetBuyOrder ( CBuyOrder *buyOrder ) ;
void SetSellOrder ( CSellOrder *sellOrder ) ;
friend class CSymbolList ;
} ;
class CSymbolList
{
private:
int count ;
int error ;
CSymbol *head ;
void Insert ( char *p_symbol, char *p_group_, char *p_state,
char *p_description, long long p_price, long p_boardlot,
char *p_flag_ ) ;
public:
CSymbolList () ;
~CSymbolList () ;
int Load () ;
CSymbol *FindSymbol ( char *symbol ) ;
} ;
/*
* Buy Order
*/
class CBuyOrder
{
private:
char symbol [15] ;
float price ;
char broker [9] ;
char time [27] ;
long volume ;
CBuyOrder *next ;
public:
CBuyOrder (CSymbolList *symbollist,
char *symbol, float price, char *broker, char *time, long
volume ) ;
~CBuyOrder () ;
friend class CBuyOrderList ;
} ;
/*
* Buy Order List
*/
class CBuyOrderList
{
private:
int count ;
int error ;
CBuyOrder *head ;
CSymbolList *symbollist ;
void Insert ( char *symbol, float price, char *broker, char *time,
long volume ) ;
public:
CBuyOrderList (CSymbolList *symbollist ) ;
~CBuyOrderList () ;
int Load (void) ;
} ;
/*
***************************************************************************
* Symbols
***************************************************************************
*/
CSymbol::CSymbol ( char *p_symbol, char *p_group_, char *p_state,
char *p_description, long long p_price,
long p_boardlot, char *p_flag_ )
{
strcpy ( symbol, p_symbol ) ;
strcpy ( group_, p_group_ ) ;
strcpy (state, p_state ) ;
strcpy ( description, p_description ) ;
price = ((float) p_price ) / 1000 ;
boardlot = p_boardlot ;
strcpy ( flag_, p_flag_ ) ;
next = NULL ;
firstbuyOrder = NULL ;
firstsellOrder = NULL ;
}
void CSymbol::SetBuyOrder ( CBuyOrder *buyOrder )
{
firstbuyOrder = buyOrder ;
}
void CSymbol::SetSellOrder ( CSellOrder *sellOrder )
{
firstsellOrder = sellOrder ;
}
/*
***************************************************************************
* Symbols List
***************************************************************************
*/
CSymbolList::CSymbolList ()
{
OpenSymbol () ;
head = NULL ;
}
CSymbolList::~CSymbolList ()
{
CloseSymbol () ;
}
int CSymbolList::Load ()
{
char l_symbol [15] ;
char l_group_ [3] ;
char l_state [2] ;
char l_description [41] ;
long long l_price ;
long l_boardlot ;
char l_flag_ [9] ;
while ( ( error = FetchSymbol () ) == 0 )
{
getSymbolFields (
l_symbol,
l_group_,
l_state,
l_description,
&l_price,
&l_boardlot,
l_flag_ ) ;
Insert ( l_symbol, l_group_, l_state, l_description, l_price,
l_boardlot, l_flag_ ) ;
count++ ;
}
if ( error == NO_ROWS )
error = 0 ;
return error ;
}
void CSymbolList::Insert ( char *p_symbol, char *p_group_, char
*p_state,
char *p_description, long long p_price, long p_boardlot,
char *p_flag_ )
{
CSymbol *current, *previous, *newSymbol ;
newSymbol = new CSymbol ( p_symbol, p_group_, p_state,
p_description,
p_price, p_boardlot, p_flag_ ) ;
if ( head == NULL )
head = newSymbol;
else
{
current = head ;
previous = NULL ;
while ( current != NULL && strncmp ( p_symbol, current->symbol,
LEN_SYMBOL ) > 0 )
{
previous = current ;
current = current->next ;
}
if ( current == head )
{
head = newSymbol ;
newSymbol->next = current ;
} else {
newSymbol->next = current ;
previous->next = newSymbol ;
}
}
}
CSymbol * CSymbolList::FindSymbol ( char *p_symbol )
{
CSymbol *current ;
current = this->head ;
while ( current != NULL && strncmp ( p_symbol, current->symbol,
LEN_SYMBOL) <= 0 )
{
cout << "S: " << current->symbol << "\n" ;
current = current->next ;
}
if ( strncmp ( p_symbol, current->symbol, LEN_SYMBOL) == 0 )
return current ;
else
return NULL ;
}
/*
****************************************************************************
* BuyOrders
****************************************************************************
*/
CBuyOrder::CBuyOrder ( CSymbolList *symbollist,
char *psymbol, float pprice, char *pbroker, char *ptime, long
pvolume )
{
<== PROBLEM // *symbollist is getting corrupted here
CSymbol *thisSymbol ;
strcpy ( symbol, psymbol ) ;
price = pprice ;
strcpy ( broker, pbroker ) ;
strcpy (time, ptime ) ;
volume = pvolume ;
next = NULL ;
cout << "Sl Cnt" << symbollist->count << "\n" ;
thisSymbol = symbollist->FindSymbol ( psymbol ) ; <== / getting
garbage
cout << "TSym " << thisSymbol->CSymbol << "\n" ;
if ( thisSymbol != NULL )
thisSymbol->SetBuyOrder ( this ) ;
else
cout << "BuyOrder without a Stock Symbol " << psymbol << "\n" ;
}
/*
***************************************************************************
* Buy Orders List
***************************************************************************
*/
CBuyOrderList::CBuyOrderList ( CSymbolList *p_symbollist )
{
OpenBuyOrder () ;
head = NULL ;
symbollist = p_symbollist ;
}
CBuyOrderList::~CBuyOrderList ()
{
CloseBuyOrder () ;
}
void CBuyOrderList::Insert ( char *p_symbol, float p_price, char
*p_broker,
char *p_time, long p_volume )
{
CBuyOrder *current, *previous, *newOrder ;
cout << "Sl Cnt" << symbollist->count << "\n" ;
<== PROBLEM // at this point *symbollist appears okay
newOrder = new CBuyOrder ( symbollist, p_symbol, p_price, p_broker,
p_time, p_volume ) ;
if ( head == NULL )
head = newOrder ;
else
{
current = head ;
previous = NULL ;
while ( current != NULL )
{
previous = current ;
current = current->next ;
}
if ( current == head )
{
head = newOrder ;
newOrder->next = current ;
} else {
newOrder->next = current ;
previous->next = newOrder ;
}
}
}
int CBuyOrderList::Load ( void )
{
char l_symbol [15] ;
float l_price ;
char l_broker [9] ;
char l_time [27] ;
long l_volume ;
while ( ( error = FetchBuyOrder () ) == 0 )
{
getBuyOrderFields (
l_symbol,
&l_price,
l_time,
l_broker,
&l_volume ) ;
Insert ( l_symbol, l_price, l_broker, l_time, l_volume ) ;
count++ ;
}
if ( error == NO_ROWS )
error = 0 ;
return error ;
}
void main ( void )
{
int c ;
CSymbolList symbollist ;
CBuyOrderList buyorderlist ( &symbollist ) ;
c = symbollist.Load () ;
c = buyorderlist.Load () ;
}