SHARE
TWEET

Untitled

a guest Feb 1st, 2016 214 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5.  
  6. typedef struct
  7. {
  8.   size_t size;
  9.   size_t position;
  10.   int arrm[1];
  11. } myarr;
  12.  
  13. void arr_check_and_push(myarr **arr, int *a)
  14. {
  15.  
  16.  
  17.   if ( (*arr)->size <= (*arr)->position)
  18.   {
  19.     (*arr)->size <<= 1;
  20.     //fprintf (stderr ,"realloc(%p, %zu)",*arr,(*arr)->size * sizeof(int) + sizeof(size_t) * 2);
  21.     *arr = (myarr *)realloc(*arr, (*arr)->size * sizeof(int) + sizeof(size_t) * 2);
  22.     if ((void *)*arr == NULL)
  23.       exit(ENOMEM);
  24.   }
  25.   (*arr)->arrm[(*arr)->position++] = *a;
  26. }
  27.  
  28. int main ()
  29. {
  30.  
  31.   myarr *arr = (myarr*)malloc(sizeof(size_t)*2+sizeof(int));
  32.   arr->size = 1;
  33.   arr->position = 0;
  34.   arr->arrm[0] = 0;
  35.  
  36.   for (int i = 0; i <= 9999999; i++ )
  37.     arr_check_and_push(&arr, &i);
  38.  
  39.   /*
  40.   for (size_t i = 0; i < arr->position; i++ )
  41.     printf("%i ", arr->arrm[i]);
  42.   */
  43.    
  44.   free(arr);
  45.   return 0;
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top