Jump to content

Pointer Comparison And Assignment


Recommended Posts

Dave / Pavel,

 

I have found another odd quirk. Nearest message I can find to this problem, which may be related, is: "Cannot Compare Pointers" posted on Feb 10 2006 in this forum.

 

I have a working fix for the problem, which I've annotated for your easy testing using the macro FIX_LEVEL , and I would appreciate your comments.

 

Problem 1. Comparison of two pointers of the same type fails. See macro BOOSTC_CAST_PTRS

Problem 2. Assignment of the pointer to another pointer of the same type fails. See macro BOOSTC_FIX_ASSIGN

 

struct stST {
 struct stST * pNext; // -> Next ST
 int x;
 int y;
 int z;
 // Other stuff i.e.
};

struct stQ {
 unsigned char Count;
 struct stST * pST;
};

struct stST   CurrST;
struct stST * pCurrST=&CurrST;

struct stQ   Qone;
struct stQ * pQone=&Qone;

struct stQ   Qtwo  ;
struct stQ * pQtwo=&Qtwo;

void MoveQs(struct stST *pThisST, struct stQ *pFromQ, struct stQ *pToQ);

// FIX_LEVEL ---- this is for easier testing of what works
//   == 0 no fix
//   == 1 cast for ptr comparison
//   == 2 working cast for ptr comparison
//   == 3 working solution
#define FIX_LEVEL  0

#pragma message "FIX_LEVEL is = " FIX_LEVEL
#if FIX_LEVEL == 0
 #define BOOSTC_CAST_PTRS 
 #define BOOSTC_FIX_ASSIGN 
#elif FIX_LEVEL == 1  
 #define BOOSTC_CAST_PTRS (struct stST *)
 #define BOOSTC_FIX_ASSIGN 
#elif FIX_LEVEL == 2  
  #define BOOSTC_CAST_PTRS (void *)
 #define BOOSTC_FIX_ASSIGN 
#elif FIX_LEVEL == 3  
 #define BOOSTC_CAST_PTRS (void *)
 #define BOOSTC_FIX_ASSIGN (struct stST*)
#else
 #pragma message "FIX_LEVEL must be in range 0 - 3, it is = " FIX_LEVEL
 #error  "Aborted compilation"
#endif  

void main() 
{
 pCurrST->pNext=pCurrST; // point to self
 pQone->pST=pCurrST;
 pQone->Count=1;
 MoveQs(pCurrST,pQone,pQtwo);
}

//---------------------------------------------------------------------------

void MoveQs(struct stST * pThisST, struct stQ * pFromQ, struct stQ * pToQ)
{
 struct stST * pChk;
 struct stST * pLast;  
 struct stST * pFirst;
 
 if(pFromQ->Count) {
   if(pFromQ->Count==1) {
     // only one ST on the Q
     pFromQ->pST=(struct stST *)0;
     pFromQ->Count=0;
   }
   else if(BOOSTC_CAST_PTRS pFromQ->pST==BOOSTC_CAST_PTRS pThisST) {
     // First ST on the Q
     pFromQ->Count--;
     pFromQ->pST=BOOSTC_FIX_ASSIGN pThisST->pNext;
   }
   else
   {  
     // Follow the Q to the ST
     pChk=pFromQ->pST;
     while(BOOSTC_CAST_PTRS pChk!=BOOSTC_CAST_PTRS pThisST) {
       pLast=pChk;
       pChk=pChk->pNext;
     }
     pLast->pNext=BOOSTC_FIX_ASSIGN pThisST->pNext;
     pFromQ->Count--;
   }
   // ST has now been removed from the FromQ
   // Now add it to the ToQ
   if(pToQ->Count) {
     // find the end of the ToQ
     pFirst=pFromQ->pST;
     pChk=pFirst;
     do {
       pLast=pChk;
       pChk=pChk->pNext;
     } while(BOOSTC_CAST_PTRS pChk!=BOOSTC_CAST_PTRS pFirst);      
     pLast->pNext=pThisST;
     pThisST->pNext=pFirst;
     pFromQ->Count++;
   }
   else {
     // ToQ is currently empty
     pToQ->pST=pThisST;
     pThisST->pNext=pThisST;
     pToQ->Count=1;
   }      
 }
}      

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...