Note that there are some explanatory texts on larger screens.

plurals
  1. POrealloc fails after multiple calls only when not debugging
    primarykey
    data
    text
    <p>The below code occasionally fails on the <code>buffer = (char*) realloc(buffer, allocated * sizeof(char));</code> call (marked down below) that I use to dynamically allocate space for a <code>char*</code>,by allocating 1 char initially, and doubling the allocated amount every time the memory I already have is insufficient to store the string. </p> <p>I have very similar code in many other parts of my project, with the same memory allocation policy and calls (changing only the type of the <code>void*</code> I pass to <code>realloc</code>). </p> <p>I am using VS2010 to debug the problem, and when I start the program on debug mode, the function always completes successfully. </p> <p>However, when calling the program from the command line, there is a good chance that one of the calls to realloc will fail after some time with an "Access violation reading location" error - though it doesn't happen all the time, and only happens after the function below has been called multiple times, with many reallocations having already taken place. </p> <p>What's weirder, I put some prints before and after the realloc call to assert if the pointer location was changed, and, when I did so and ran the program, the calls to realloc stopped failing randomly.</p> <p>What am I doing wrong?</p> <pre><code>TOKEN next_token_file(FILE* file, STATE_MACHINE* sm, STATE_MACHINE* wsssm) { char* buffer = (char*) malloc(sizeof(char)); size_t allocated = 1; size_t i = 0; while(1) { /* ... code that increments i by one and messes with sm a bit. Does nothing to the buffer. */ // XXX: This fails when using realloc. Why? if(i + 1 &gt;= allocated) { allocated = allocated &lt;&lt; 1; buffer = (char*) realloc(buffer, allocated * sizeof(char)); } buffer[i] = sm-&gt;current_state-&gt;state; /* ... more code that doesn't concern the buffer */ } // Null-terminate string. buffer[++i] = 0; TOKEN t = {ret, buffer}; return t; } </code></pre>
    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.
 

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