Advertisement
carefulnow

Managing an array containing structure pointers

Aug 3rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. /* structlist.c
  2.  * A testing program for managing an array containing structure pointers. */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. #define MAX_STRUCT_COUNT 5
  9. #define MAX_ITERATION_COUNT 10
  10.  
  11. typedef struct {
  12.     int x;
  13.     int y;
  14.     int index;
  15. } test_struct_t;
  16.  
  17. test_struct_t* struct_list[MAX_STRUCT_COUNT];
  18.  
  19. test_struct_t* add_struct(int x, int y) {
  20.     // find the next free position in the array
  21.  
  22.     for (int i = 0; i < MAX_STRUCT_COUNT; i++) {
  23.         if (struct_list[i] == NULL) {
  24.             if ((struct_list[i] = malloc(sizeof(test_struct_t))) == NULL) {
  25.                 return NULL;
  26.             }
  27.  
  28.             struct_list[i]->x = x; 
  29.             struct_list[i]->y = y;
  30.             struct_list[i]->index = i;
  31.  
  32.             return struct_list[i];
  33.         }
  34.     }
  35.  
  36.     return NULL;
  37. }
  38.  
  39. void remove_struct(test_struct_t* target) {
  40.     struct_list[target->index] = NULL;
  41.     free(target);
  42. }
  43.  
  44. int main(void) {
  45.     int valid_structs = 0;
  46.     int max_iterations = (MAX_ITERATION_COUNT > 255) ? 255 : MAX_ITERATION_COUNT;
  47.  
  48.     // register structs
  49.  
  50.     for (int i = 0; i < max_iterations && valid_structs < MAX_STRUCT_COUNT; i++) {
  51.         printf("Iteration %d: ", i + 1);
  52.  
  53.         if (!add_struct(i, i + 4)) {
  54.             printf("Could not create struct; skipping...\n");
  55.             struct_list[i] = NULL;
  56.             continue;
  57.         }
  58.  
  59.         printf("Created struct at pos. %d with X = %d and Y = %d\n", struct_list[valid_structs]->index, struct_list[valid_structs]->x, struct_list[valid_structs]->y);
  60.         valid_structs++;
  61.     }
  62.  
  63.     if (valid_structs < 1) {
  64.         fprintf(stderr, "No structs could be created... exiting\n");
  65.         return 1;
  66.     }
  67.  
  68.     // testing (free a random one)
  69.  
  70.     printf("\nSuccessfully created %d structs (max iteration count of %d).\n\nFreeing a random one...\n", valid_structs, max_iterations);
  71.  
  72.     srand(time(NULL));
  73.     int target;
  74.     while (struct_list[target = rand() % MAX_STRUCT_COUNT] == NULL);
  75.  
  76.     printf("Removing struct at pos. %d: %p (x = %d, y = %d)\n", target, struct_list[target], struct_list[target]->x, struct_list[target]->y);
  77.     remove_struct(struct_list[target]);
  78.     printf("Removed struct at pos. %d\nProof: struct_list[%d] = %p\n", target, target, struct_list[target]);
  79.  
  80.     // auto-cleanup
  81.  
  82.     for (int i = 0; i < MAX_STRUCT_COUNT; i++) {
  83.         if (struct_list[i] != NULL) {
  84.             remove_struct(struct_list[i]);
  85.         }
  86.     }
  87.  
  88.     printf("\nAll other structs cleaned.\nExiting...\n\n");
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement