debugging - Find Heap Corruption In C Program -


i've looked around, of answers here questions heap corruption obvious code, or asker has identified source.

i have c program (simulating car race) dynamically allocating memory linked list. copies values 1 or more nodes list dynamically allocated 2d array, based on value in node. each node freed after copied, , list head updated. repeats until there no more nodes in list (the end of race).

a pointer array returned main , stored in 3d array.

the whole process repeats (new linked list, new array).

at end of second iteration (second race), getting heap corruption error, , can't figure out causing it.

i tried using vld suggested here: memory allocation / heap corruption in std::string constructor

but vld included didn't error.

i tried enabling debug heap functions: https://msdn.microsoft.com/en-us/library/x98tx3cf.aspx

this told me address 0x596ebc5c, doesn't appear contain allocated, i'm not sure if that's meaningful somehow.

the best can tell, i'm getting error in block of code, i'm not sure of that, , don't know how me find source of problem.

void movefinishers(node **racehead, int **finisherlist, int racelength) {     static int numberoffinishers = 0;     node *head = *racehead;     node *temp = *racehead;     node *tempnext = null;     while (head != null && head->car.distance >= racelength)     {         finisherlist[0][numberoffinishers] = head->car.number;         numberoffinishers++;         head = head->next; //advance next finisher     }      *racehead = head; //change list head start first non-finisher      //free list elements before first non-finisher     while (temp != head)     {         tempnext = temp->next; //iterates through temp values         free(temp);         temp = tempnext;     } //end while } 

i figured out. unfortunately, still wasn't able find problem throught debugger, rather looking @ code.

anyways, issue this:

static int numberoffinishers = 0 

i declared static because needed maintain state within single race.

however, after first race, not resetting counter, starting store values in unallocated memory:

finisherlist[0][numberoffinishers] 

the fix simple adding end of function:

    if (!head)     {         numberoffinishers = 0;     } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -