Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest way possible to allocate an array of strings
    primarykey
    data
    text
    <p>I have a function which takes an array of strings (<em>buffer</em>) and needs to increase its size. So I invoke a realloc</p> <pre><code>temp = (char**) realloc (buffer, newSize * (sizeof(char*))); if (temp == NULL) return false; else buffer = temp; </code></pre> <p>And thus far everything is fine. Now for every new cell I must invoke a malloc with the correct size. Notice that newSize is always even and that odd strings have a different length than even ones.</p> <pre><code>for (i = oldSize; i &lt; newSize; i++){ support = (char*) malloc (LENGTH1 * sizeof(char)); if (support == NULL){ marker = i; failedMalloc = true; break; } else buffer[i] = support; i++; support = (char*) malloc (LENGTH2 * sizeof(char)); if (support == NULL){ marker = i; failedMalloc = true; break; } else buffer[i] = support; } </code></pre> <p>The fact is that since I work with huge data sooner or later I'll finish memory and the realloc or one of the mallocs will fail. The problem is that if it's one of the mallocs the one that fails there is the risk that I'll have to invoke <strong>millions</strong> of free to clear up some memory. This takes a lot of time. Is there any way to speedup this process or even better avoid it?</p> <pre><code>if (failedMalloc){ for (i = oldRows; i &lt; marker; i++) free(buffer[i]); temp = (char**) realloc (buffer, oldRows * (sizeof(char*))); } </code></pre> <p>PS: <strong>Yes I know that pointer arithmetic is faster than array indexing.</strong> I will implement it when I find a way to solve this problem, for the moment I prefer using array indexing because I find it less error prone. But the final version will use pointer arithmetic</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