Note that there are some explanatory texts on larger screens.

plurals
  1. POMalloc corrupting already malloc'd memory in C
    primarykey
    data
    text
    <p>I'm currently helping a friend debug a program of his, which includes linked lists. His list structure is pretty simple:</p> <pre><code>typedef struct nodo{ int cantUnos; char* numBin; struct nodo* sig; }Nodo; </code></pre> <p>We've got the following code snippet:</p> <pre><code>void insNodo(Nodo** lista, char* auxBin, int auxCantUnos){ printf("*******Insertando\n"); int i; if (*lista) printf("DecInt*%p-&gt;%p\n", *lista, (*lista)-&gt;sig); Nodo* insert = (Nodo*)malloc(sizeof(Nodo*)); if (*lista) printf("Malloc*%p-&gt;%p\n", *lista, (*lista)-&gt;sig); insert-&gt;cantUnos = auxCantUnos; insert-&gt;numBin = (char*)malloc(strlen(auxBin)*sizeof(char)); for(i=0 ; i&lt;strlen(auxBin) ; i++) insert-&gt;numBin[i] = auxBin[i]; insert-&gt;numBin[i] = '\0'; insert-&gt;sig = NULL; Nodo* aux; /* [etc] */ </code></pre> <p>(The lines with extra indentation were my addition for debug purposes)</p> <p>This yields me the following:</p> <pre><code>*******Insertando DecInt*00341098-&gt;00000000 Malloc*00341098-&gt;2832B6EE </code></pre> <p><code>(*lista)-&gt;sig</code> is previously and deliberately set as <code>NULL</code>, which checks out until here, and fixed a potential buffer overflow (he'd forgotten to copy the NULL-terminator in insert->numBin).</p> <p>I can't think of a single reason why'd that happen, nor I've got any idea on what else should I provide as further info. (Compiling on latest stable MinGW under fully-patched Windows 7, friend's using MinGW under Windows XP. On my machine, at least, in <em>only</em> happens when GDB's not attached.)</p> <p>Any ideas? Suggestions? Possible exorcism techniques? (Current hack is copying the sig pointer to a temp variable and restore it after malloc. It breaks anyways. Turns out the 2nd malloc corrupts it too. Interestingly enough, it resets sig to the exact same value as the first one).</p> <p><strong>UPDATE:</strong> Thanks for the answers. Regarding the <code>Node*</code> thing, it's fixed, but no change. At least prevents potential problems afterwards. String copying isn't the issue, as I already fixed all missing \0s myself. (Note the <code>insertBin[i] = '\0'</code> after the for)</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