Note that there are some explanatory texts on larger screens.

plurals
  1. POmalloc.c:3074 + Valgrind output
    primarykey
    data
    text
    <p>I am getting the infamous malloc.c:3074 error when running my code (compiles without issue). I compiled using the -g option. I used Valgrind to determine where the memory allocation issue is happening but the results aren't helping a whole lot. Here is the Valgrind output:</p> <pre><code>==2710== Invalid write of size 8 ==2710== at 0x400FC8: generatePairs (ldbalpha.c:42) ==2710== by 0x400BFA: main (ldb-stegoencoderalpha.c:53) ==2710== Address 0x51997c8 is not stack'd, malloc'd or (recently) free'd ==2710== ==2710== Invalid write of size 8 ==2710== at 0x401047: generatePairs (ldbalpha.c:54) ==2710== by 0x400BFA: main (ldb-stegoencoderalpha.c:53) ==2710== Address 0x51997a8 is 0 bytes after a block of size 1,160 alloc'd ==2710== at 0x4C25153: malloc (vg_replace_malloc.c:195) ==2710== by 0x400BA6: main (ldb-stegoencoderalpha.c:49) ==2710== ==2710== Invalid write of size 8 ==2710== at 0x401054: generatePairs (ldbalpha.c:55) ==2710== by 0x400BFA: main (ldb-stegoencoderalpha.c:53) ==2710== Address 0x51997b0 is 8 bytes after a block of size 1,160 alloc'd ==2710== at 0x4C25153: malloc (vg_replace_malloc.c:195) ==2710== by 0x400BA6: main (ldb-stegoencoderalpha.c:49) ==2710== ==2710== Invalid write of size 8 ==2710== at 0x401062: generatePairs (ldbalpha.c:56) ==2710== by 0x400BFA: main (ldb-stegoencoderalpha.c:53) ==2710== Address 0x51997c0 is not stack'd, malloc'd or (recently) free'd </code></pre> <p>Here is the main function followed by the called function generatePairs. I listed the line numbers as comments to correspond with the valgrind output:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;malloc.h&gt; #include "ldb.h" int main(int argc, char * argv[]) { FILE *pFile; unsigned char *buffer; int i, j; char ch = '\0'; long unsigned int lSize; if (argc != 3) { fprintf(stderr, "Usage: ./ldb-stegoencoderalpha [Stegofile] [messages.txt] &gt; [messages-encoded.txt]\n"); return 1; } // end if pFile = fopen ( argv[1] , "r" ); if (pFile==NULL) {fputs ("File error on arg[1]",stderr); exit (1);} // obtain file size: fseek (pFile , 0 , SEEK_END); // Go to end of File lSize = ftell (pFile); // Return # of Bytes in the file rewind (pFile); // Rewind to start of file // allocate memory to contain the whole file: buffer = malloc(sizeof(char) * lSize+1); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} bitpair * ppairs = malloc(sizeof(bitpair) * (lSize+1)); // Line 49,Structure setup memset (ppairs, 0, sizeof(bitpair) * (lSize+1)); //zeroize it first generatePairs(ppairs, lSize+1); //Line 53 in Valgrind error </code></pre> <p>After that I do some calculations with those pairs but the Valgrind errors are coming from the generatePairs and malloc functions. Here is the generatePairs function:</p> <pre><code>#include &lt;string.h&gt; #include &lt;assert.h&gt; #include "ldb.h" void generatePairs(bitpair * ppairs, long unsigned int bits) { unsigned int i, rand1, rand2, high, low; unsigned int count = 1; // initialize the array of pairs for(i = 1; i &lt;= bits; i++) { bitpair * bp = &amp;ppairs[i]; bp-&gt;ref = -1; bp-&gt;enc = -1; bp-&gt;len = -1; bp-&gt;bit = -1; bp-&gt;used = 0; } for(i = 1; i &lt;= bits; i++) { rand1 = 0; ppairs[rand1].used = 1; rand2 = count; count++; assert(rand2 &lt;= bits); ppairs[rand2].used = 1; //Line 42 in Valgrind error high = rand2; low = rand1; // initialize both data structures (bp-&gt;used is already set) bitpair * bp = &amp;ppairs[low]; bp-&gt;ref = low; bp-&gt;enc = high; bp-&gt;bit = i; bp = &amp;ppairs[high]; bp-&gt;ref = low; //Line 54 in Valgrind error bp-&gt;enc = high; //Line 55 in Valgrind error bp-&gt;bit = i; //Line 56 in Valgrind error } return; } typedef struct { long unsigned int ref; long unsigned int enc; long unsigned int len; long unsigned int bit; long unsigned int used; } bitpair; void generatePairs(bitpair * ppairs, long unsigned int bits); </code></pre> <p>Thanks!</p>
    singulars
    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