##master-page:HomepageReadWritePageTemplate ##master-date:Unknown-Date #format wiki #language en = C Dynamic Array = {{{ int AddToArray (DATA item) { if(num_elements == num_allocated) // Are more refs required? { // Feel free to change the initial number of refs // and the rate at which refs are allocated. if (num_allocated == 0) num_allocated = 3; // Start off with 3 refs else num_allocated *= 2; // Double the number // of refs allocated // Make the reallocation transactional // by using a temporary variable first void *_tmp = realloc(the_array, (num_allocated * sizeof(DATA))); // If the reallocation didn't go so well, // inform the user and bail out if (!_tmp) { fprintf(stderr, "ERROR: Couldn't realloc memory!\n"); return(-1); } // Things are looking good so far the_array = (DATA*)_tmp; } the_array[num_elements] = item; num_elements++; return num_elements; } }}} ...