Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
struct records t_head=0;

struct records *find_id(Rect a)
{

        struct records *tmp;
        struct records *prev;

	// Add first node
        if(t_head==NULL) {
        tmp=new records;
        tmp->b=a;
        tmp->id=trecord_count+1;
        tmp->tally.resize(labelsInfo.size());
        tmp->frames=0;
        t_head=tmp;
        t_head->next=NULL;
        trecord_count++;
        }

 	else {
	// Check if there is any node that has delete_flag set first
        tmp=t_head;
        while(tmp) {

        if(delete_flag) { 
      
        // Delete here
        if(tmp==t_head) {
        t_head=tmp->next;
        delete(tmp);
        trecord_count--;
	}
        else { 
        prev->next=tmp->next;
        delete(tmp);
        trecord_count--;
        }
       
        }

        prev=tmp;
        tmp=tmp->next;
        }

	
        tmp=new records
        tmp->b=a;
        tmp->id=trecord_count+1;
        tmp->tally.resize(labelsInfo.size());
        tmp->frames=0;
        tmp->next=t_head;
        t_head=tmp;
        trecord_count++;
        }

        return tmp;
}


What I have tried:

I implemented this based of a an article somewhere, but it crashes eventually.
Is there anything inherently wrong with this code ?

Thanks for taking a look,
Posted
Updated 7-Dec-16 17:08pm
Comments
CHill60 7-Dec-16 18:27pm    
"it crashes eventually" ... this is not a helpful statement. Try debugging it. What is the error that is reported? What conditions make it crash?
Member 12887991 7-Dec-16 18:29pm    
Sorry. It either says that i'm freeing a pointer that was not previously allocated or it crashes when i dereference the tmp pointer.

1 solution

First: your code look like C++ more than C.
Second: use proper indentation, it help reading the code.
Third: this code is not autonomous, we can't compile it to see what it does.

you have hard time understanding what the code does and why it crashes.
Use t(he debugger, it will allow you to see the code running and see variables changing. For every step look at variables changes and ask yourself if it match your expectations.
-----
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900