Note that there are some explanatory texts on larger screens.

plurals
  1. POC program runs in Cygwin but not Linux (Malloc)
    text
    copied!<p>I have a heap allocation error that I cant spot in my code that is picked up on vanguard/gdb on Linux but runs perfectly on a Windows cygwin environment. I understand that Linux could be tighter with its heap allocation than Windows but I would really like to have a response that discovers the issue/possible fix. I'm also aware that I shouldn't typecast malloc in C but it's a force of habit and doesn't change my problem from happening. My program actually compiles without error on both Linux &amp; Windows but when I run it in Linux I get a scary looking result:</p> <blockquote> <p>malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &amp;((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) &amp; ~((2 * (sizeof(size_t))) - 1))) &amp;&amp; ((old_top)->size &amp; 0x1) &amp;&amp; ((unsigned long)old_end &amp; pagemask) == 0)' failed. Aborted</p> </blockquote> <p>Attached snippet from my code that is being pointed to as the error for review:</p> <pre><code>/* Main */ int main(int argc, char * argv[]) { FILE *pFile; unsigned char *buffer; long int lSize; pFile = fopen ( argv[1] , "r" ); if (pFile==NULL) {fputs ("File error on arg[1]",stderr); return 1;} fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); buffer = (char*) malloc(sizeof(char) * lSize+1); if (buffer == NULL) {fputs ("Memory error",stderr); return 2;} bitpair * ppairs = (bitpair *) malloc(sizeof(bitpair) * (lSize+1)); //line 51 below calcpair(ppairs, (lSize+1)); /* irrelevant stuff */ fclose(pFile); free(buffer); free(ppairs); } typedef struct { long unsigned int a; //not actual variable names... Yes I need them to be long unsigned long unsigned int b; long unsigned int c; long unsigned int d; long unsigned int e; } bitpair; void calcpair(bitpair * ppairs, long int bits); void calcPairs(bitpair * ppairs, long int bits) { long int i, top, bot, var_1, var_2; int count = 0; for(i = 0; i &lt; bits; i++) { top = 0; ppairs[top].e = 1; do { bot = count; count++; } while(ppairs[bot].e != 0); ppairs[bot].e = 1; var_1 = bot; var_2 = top; bitpair * bp = &amp;ppairs[var_2]; bp-&gt;a = var_2; bp-&gt;b = var_1; bp-&gt;c = i; bp = &amp;ppairs[var_1]; bp-&gt;a = var_2; bp-&gt;b = var_1; bp-&gt;c = i; } return; } </code></pre> <p>gdb reports: free(): invalid pointer: 0x0000000000603290 * </p> <p>valgrind reports the following message 5 times before exiting due to "VALGRIND INTERNAL ERROR" signal 11 (SIGSEGV):<br> Invalid read of size 8<br> ==2727== at 0x401043: calcPairs (in /home/user/Documents/5-3/ubuntu test/main)<br> ==2727== by 0x400C9A: main (main.c:51)<br> ==2727== Address 0x5a607a0 is not stack'd, malloc'd or (recently) free'd </p>
 

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