Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main problem here is that you're trying to use realloc with a stack-allocated array. You have:</p> <pre><code>ent a[mSize]; </code></pre> <p>That's automatic allocation on the stack. If you wanted to use realloc() on this later, you would create the array on the heap using malloc(), like this:</p> <pre><code>ent *a = (ent*)malloc(mSize * sizeof(ent)); </code></pre> <p>So that the malloc library (and thus realloc(), etc.) knows about your array. From the looks of this, you may be confusing <a href="http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html" rel="nofollow noreferrer">C99 variable-length arrays</a> with true <a href="http://en.wikipedia.org/wiki/Dynamic_array" rel="nofollow noreferrer">dynamic arrays</a>, so be sure you understand the difference there before trying to fix this.</p> <p>Really, though, if you are writing dynamic arrays in C, you should try to use OOP-ish design to encapsulate information about your arrays and hide it from the user. You want to consolidate information (e.g. pointer and size) about your array into a struct and operations (e.g. allocation, adding elements, removing elements, freeing, etc.) into special functions that work with your struct. So you might have:</p> <pre><code>typedef struct dynarray { elt *data; int size; } dynarray; </code></pre> <p>And you might define some functions to work with dynarrays:</p> <pre><code>// malloc a dynarray and its data and returns a pointer to the dynarray dynarray *dynarray_create(); // add an element to dynarray and adjust its size if necessary void dynarray_add_elt(dynarray *arr, elt value); // return a particular element in the dynarray elt dynarray_get_elt(dynarray *arr, int index); // free the dynarray and its data. void dynarray_free(dynarray *arr); </code></pre> <p>This way the user doesn't have to remember exactly how to allocate things or what size the array is currently. Hope that gets you started.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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