Link to home
Create AccountLog in
Avatar of Develprog
Develprog

asked on

best way to handle a pointer to pointer and assignment method when the pointer return NULL

Hi I have a program using pointer of pointer to access at list

this portion of code is giving me error when the return is null.

How can I manage this ?

	/*tracer_tool.cpp*/
 
void Tracertool::updateTracerSet(CCornerList_Int* newCornerList, unsigned char * currentCorner){
 
	// reset the update status of all the (simple) corners
	map<int, SimpleCorner* >::iterator itSimpleCornerList;
	for (itSimpleCornerList = SimpleCornerList.begin(); itSimpleCornerList!=SimpleCornerList.end(); itSimpleCornerList++){		
		(*itSimpleCornerList).second->isUpdated = false;	
		(*itSimpleCornerList).second->timeElapsedSinceLastUpdate++;
 
	}
 
	// for all the current corners
	//unsigned char *currentCorner = newCornerList->get_first();
	//unsigned char **currentCorner = vgt_an_rawcorner_alloc();
	//unsigned char *currentCorner = vgt_an_rawcorner_alloc();
 
//	strcpy((char*)currentCorner,(const char*) newCornerList->get_first()) ;
	printf("a. value of newCornerList->get_first() =%p\n",newCornerList->get_first());
    currentCorner=*(newCornerList->get_first());
	printf("b. value of currentCorner after assign =%p\n",currentCorner);
 
	while (currentCorner !=NULL){
 		//int trackingID = currentCorner->tracking_id;
		long trackingID =0;
 		vgt_an_RawCorner_get_long(currentCorner,TRACKING_ID_L,&trackingID);
 
 		// search corresponding simple corner
 		map<int, SimpleCorner* >::iterator itcornerTracer= this->SimpleCornerList.find(trackingID);
		// if it exists
		if (itcornerTracer!=SimpleCornerList.end()){		
			(*itcornerTracer).second->isUpdated = true;
			(*itcornerTracer).second->timeElapsedSinceLastUpdate = 0;
			// if the corner exist for a number of frame >= MAX_TRACER_SIZE 
			if ((*itcornerTracer).second->tracer.size() >= (*itcornerTracer).second->tracer_MAX_SIZE ){ 
 
				//erease first element of real tracer
				delete (*(*itcornerTracer).second->tracer.begin());
				(*itcornerTracer).second->tracer.erase( (*itcornerTracer).second->tracer.begin() );
 
			}
 
			//addPointTotracer(currentCorner,(*itcornerTracer).second );
			addPointTotracer(currentCorner,(*itcornerTracer).second );
			//addPointToSmoothtracer(currentCorner,(*itcornerTracer).second, 3);			
			addPointToSmoothtracer(currentCorner,(*itcornerTracer).second, 3);			
		}
		// else create new simple corner
		else{
			SimpleCorner* newcorner = new SimpleCorner();
			newcorner->isUpdated=true;
			newcorner->timeElapsedSinceLastUpdate=0;
			// add the corner to the intern  list
			this->SimpleCornerList.insert(pair<int, SimpleCorner*>(trackingID, newcorner));
		}


//1.
		//currentCorner=*(newCornerList->get_next()); !! ACCESS VIOLATION WHEN FONCTION RETURNS NULL
 
		//2.
		//Is this a good way to check NULL condition ? 
		unsigned char ** temp=newCornerList->get_next();
		if(temp!=0)
			currentCorner=temp;
		else
			currentCorner = NULL;
 
		//3. using strcpy ?  
		//strcpy((char*)currentCorner,(const char*)newCornerList->get_next()) ;

         }

   };

Open in new window


The problem is that when list is at end the line ..t->get_next()); return a NULL so assign it this address to pointer generate Access violation problem.

Is ther another way to do it better ?

Thank you
Avatar of Infinity08
Infinity08
Flag of Belgium image

>>             //currentCorner=*(newCornerList->get_next()); !! ACCESS VIOLATION WHEN FONCTION RETURNS NULL

Why don't you just check whether it returns NULL, and don't dereference it if it does ?

        unsigned char** ret = newCornerList->get_next();
        if (ret == NULL) {
            // oops : don't use it - handle this error !!!
        }
        else {
            // you can use ret here, and dereference it
        }
Avatar of Develprog
Develprog

ASKER


Ok,

you mean like this ?


 1.       unsigned char** ret = newCornerList->get_next();
 2.       if (ret == NULL) {
 3.           // oops : don't use it - handle this error !!!
 4.      }
 5.     else {
 6.         // you can use ret here, and dereference it
 7.            currentCorner=*(ret); //is it good?
 8.     }


is this line 7 same that doing " currentCorner=*(newCornerList->next()); " in line 1 ?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account

Ok so finally :

 1.       unsigned char** ret = newCornerList->get_next();
 2.       if (ret == NULL) {
 3.           currentCorner = NULL;
 4.      }
 5.     else {
 6.         // you can use ret here, and dereference it
 7.            currentCorner=*(ret);
 8.     }

That looks good, assuming that the rest of the code is aware that currentCorner might be NULL, and checks for it as appropriate.
What about  line  20 :

    currentBlob=*(newBlobList->get_first());


is it safe ?



Maybe no need to check according to fact that list contain alwways element at first

 currentCorner=*(newCornerList->get_first());

what if erase of first element in the list occur ?



>>     currentBlob=*(newBlobList->get_first());

That has the same problem if get_first returns NULL.

Ok so I did same than get_next() for this line.

thank you