Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer Manipulation 1

Status
Not open for further replies.

chris921

Technical User
Mar 9, 2004
82
GB
Hi,

I have 3 pointers on the same linked list,

named ptr, ptra, ptragain.

I'd like to be able to swap data from ptr to ptra, and from ptra to ptr, thus the need for the temporary ptragain.

Here is a slice of the code, that throws a Segmentation error.

Can you help me with this please? Cant see where I'm going wrong.
Code:
	ptra=ptr->next;
	ptragain=ptra->next;
	if(ptra->count > ptr->count){
		//ptragain=ptr;
		ptragain->count = (ptr->count);
		*ptragain->word = *ptr->word;
		//ptr=ptra;
		ptr->count = ptra->count;
		ptr->word = ptra->word;
		//ptra=ptragain;
		ptra->count = ptragain->count;
		ptra->word = ptragain->word;
	}
thanks
chris
 
You never check for the end of the list. Either at some point the ->next pointer is going to be NULL or undefined, which would cause problems when you dereference it.

Run your program in debug mode, and figure out where and when the program crashes, that will help you understand where to put more checks to handle special cases. For example: if ->next is a valid pointer, do what you do above, else handle it.

Good luck!

Vincent
 
My segmentation fault has now gone, thank you.

However, it wont allow me to print out the very first element of the Linked List, any idea why - all the others are perfect?

Thanks,

 
Further question - the segmentation error has reared its ugly head again - and the debugger doesnt tell me anything!

Code:
  else {
    ptr=freq_list;
    int word_size=strlen(word) + 1;
    char* dest=new char[word_size];
    strcpy(dest, word);
    word_freq* ptrb = freq_list;
    word_freq* ptra = freq_list;
    word_freq* ptrc = freq_list;
    word_freq* new_wf=new word_freq(dest);
    new_wf->next=freq_list;
    freq_list=new_wf;
   (new_wf->count)++;
   ptra=ptr;
   ptrb=ptr;
   ptrc=ptr;
   while (ptra != NULL) {
   if(ptra->count >= ptrb->count) {
   printf("your here");
   ptrb = ptra;
   ptra = ptra->next; 
   ptrc->count = ptra->count;
   ptrc->word = ptra->word;
   ptra->count = ptrb->count;
   ptra->word = ptrb->word;
   ptrb->count = ptrc->count;
   ptrb->word = ptrc->word;  
   }
   }

The above segment is supposed to read in a count value and compare with other counts (already in Linked List) and sort them in ascending order. This needs to happen for each value read in.
 
Debugging can be a long and hard process. And debugging partial code by hand is even worse! Here are some ideas where to look at:

How do you initially build your list? How is the very first item initialized?

Be very careful about what your pointer refer to. In your code above, at the first iteration of your while loop:

Code:
   ptra=ptr;
   ptrb=ptr;
   ptrc=ptr;                 <- all 3 pointers point to the same location
   while (ptra != NULL) {
   if(ptra->count >= ptrb->count) {
   printf("your here");
   ptrb = ptra;
   ptra = ptra->next;
   ptrc->count = ptra->count;     <- you take the "next" values and put them in c, which is the same as the first location
   ptrc->word = ptra->word;
   ptra->count = ptrb->count;    <- b is also the first location, so you put the values of the second location and put it back to the second location
   ptra->word = ptrb->word;
   ptrb->count = ptrc->count;    <- again do nothing useful
   ptrb->word = ptrc->word;

Also, you loop while ptra != NULL. What happens if it is? Well, during the previous iteration, it means that ptra was not NULL initially, but it's next was. Then ptra becomes its old next (=NULL), then you do ptra->next, which is very bad.

Here is how you should use the debugger: don't look at the variables at the point of the error. Use the "break" feature and step through your code, following the variables which cause the error, and find why they changed to what you did not expect.

Good luck!

Vincent
 
ptra ptrb and ptrc are useless names.

It's easier to figure out problems if you give them meaningful names, because simply reading the code will tell you that you're doing something dumb.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top