Note that there are some explanatory texts on larger screens.

plurals
  1. POInfinite loop because of 'sudden' change of pointer
    primarykey
    data
    text
    <p>I understand, that in programming world, nothing happens just out of blue, but I'm really stuck here... At the end of my while loop, pointer inside the condition suddenly changes from NULL to "something"</p> <p>I've got following function:</p> <pre><code>tListInstr *copyList (tListInstr *sourceList) { tListInstr *newList; listInit(&amp;newList); listFirst(sourceList); while(sourceList-&gt;active != NULL){ InstructionGenerate(newList, sourceList-&gt;active-&gt;Instruction.instType, sourceList-&gt;active-&gt;Instruction.addr1, sourceList-&gt;active-&gt;Instruction.addr2, sourceList-&gt;active-&gt;Instruction.addr3); listNext(sourceList); if(sourceList-&gt;active == NULL) printf("wtf????\n"); } return newList; } </code></pre> <p>To exaplain the code, it's part of interpreter, this function copies 3 address code instruction list of called function in source language and returns the copy, <code>sourceList</code> is list to be copied (it's never NULL) <code>newList</code> is obviousle new list. <code>listInit</code> allocates memory and initialises new list, <code>listFirst</code> sets activity of <code>sourceList</code> to it's first item. <code>listNext</code> shifts the activity to next item, right behind current active. <code>InstructionGenerate</code> appends new instruction to <code>newList</code>.</p> <p>Well my problem is, that at the end of the loop <code>sourceList-&gt;active</code> is clearly NULL, because I get infinite <code>wtf????</code>'s on terminal, but after it's printed and the condition of <code>while</code> get's tested, it has non-NULL value (I checked) and the while loops infinitely.</p> <p>The funny part is, that when I remove the <code>InstructionGenerate</code> call, it runs allright. But <code>InstructionGenerate</code> doesn't/shouldn't affect <code>sourceList</code> pointer in any way as it is working with <code>newList</code> and if it in some strange way I wouldn't understand do something with <code>sourceList</code>, I change the activity AFTER it is called and test the condition BEFORE it is called.</p> <p>I don't think this will be of any help, but here is code of <code>InstructionGenerate</code> and function it calls:</p> <pre><code>void InstructionGenerate(tListInstr *l, int varType,void *addr1, void *addr2,void *addr3){ tInstr I; I.addr1 = addr1; I.addr2 = addr2; I.addr3 = addr3; I.instType = varType; listInstInsert(l,I); } void listInstInsert(tListInstr *L,tInstr I) { tListItem ptr = malloc(sizeof(struct listItem)); if(ptr == NULL) return; else { ptr-&gt;Instruction = I; if (L-&gt;first == NULL){ ptr-&gt;nextItem = NULL; L-&gt;first = ptr; L-&gt;end = ptr; } else{ ptr-&gt;nextItem = NULL; L-&gt;end-&gt;nextItem = ptr; L-&gt;end = ptr; } } } </code></pre> <p>And finally <code>struct</code>'s</p> <pre><code>typedef struct tInstr { int instType; void *addr1; void *addr2; void *addr3; } tInstr; typedef struct listItem { tInstr Instruction; struct listItem *nextItem; } *tListItem; typedef struct tListInstr { struct listItem *first; struct listItem *end; struct listItem *active; } tListInstr; </code></pre> <p>Compiled by:<br> gcc version 4.4.5 (Debian 4.4.5-8)<br> gcc version 4.5.4 (GCC) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload