Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. struct array {
  2. int* data;
  3. int size;
  4. };
  5.  
  6. struct array* array_create()
  7. {
  8. struct array* array = (struct array*) malloc(sizeof(struct array));
  9. array->data = (int*) malloc(sizeof(int) * 10000);
  10. array->size = 10000;
  11. return array;
  12. }
  13.  
  14. void array_resize(struct array* array, int size)
  15. {
  16. if (size > array->size) {
  17. free(array);
  18. array->data = (int*) malloc(sizeof(int) * size);
  19. }
  20.  
  21. array->size = size;
  22. }
  23.  
  24. int array_size(struct array* array)
  25. {
  26. return array->size;
  27. }
  28.  
  29. typedef struct array* ARRAY;
  30.  
  31. ARRAY array_create();
  32. void array_resize(struct array* array, int size);
  33.  
  34. void edit()
  35. {
  36. array_resize(array, 100); // I can run the code but program crashes
  37. }
  38.  
  39. ARRAY array;
  40. array = array_create();
  41. edit(); // Program crashes
  42.  
  43. free(array);
  44. array->data = (int*) malloc(sizeof(int) * size);
  45.  
  46. void array_resize(struct array* array, int size)
  47. {
  48. if (size > array->size) {
  49. free(array);
  50. array->data = (int*) malloc(sizeof(int) * size);
  51. }
  52.  
  53. array->size = size;
  54. }
  55.  
  56. struct array;
  57. struct array *array_create();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement