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
Editors IDEsC

Avatar of undefined
Last Comment
Develprog

8/22/2022 - Mon
Infinity08

>>             //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
        }
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
Infinity08

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Develprog

ASKER

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.     }

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Infinity08

That looks good, assuming that the rest of the code is aware that currentCorner might be NULL, and checks for it as appropriate.
Develprog

ASKER
What about  line  20 :

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


is it safe ?

Develprog

ASKER


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 ?



Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Infinity08

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

That has the same problem if get_first returns NULL.
Develprog

ASKER

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

thank you