Skip to main content
added 8 characters in body
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92
  1. Code should not mix pointer types. On systems where sizeof(unsigned short) < sizeof(int), this is a program flaw.

     // int *children = malloc(size * sizeof(unsigned short));
     int *children = malloc(size * sizeof(int));
     // or even better
     int *children = malloc(size * sizeof *children);
    
  2. For high portability, use type size_t or unsigned long for valuesan array index that may exceed 65,535:

     // unsigned int size = count_children(input);
     size_t size = count_children(input);
     int *children = malloc(size * sizeof *children);
    
  3. Code should check the return value from fscanf(input, "%d",&count); and fscanf(input, "%d",&children[i]);

  4. Mixing types for finding the maximum is very questionable. Suggest re-write

    size_t chck_tall(const int *children, size_t size) {
      size_t total = 0;
      int tmp_max = 0;   // or maybe INT_MIN
      for(size_t i = size - 1; i-- > 0; )  {
        if(children[i] > tmp_max) {
          tmp_max = children[i];
          total++;
        }
      }
      return total;
    }
    
  1. Code should not mix pointer types. On systems where sizeof(unsigned short) < sizeof(int), this is a program flaw.

     // int *children = malloc(size * sizeof(unsigned short));
     int *children = malloc(size * sizeof(int));
     // or even better
     int *children = malloc(size * sizeof *children);
    
  2. For high portability, use type size_t or unsigned long for values that may exceed 65,535:

     // unsigned int size = count_children(input);
     size_t size = count_children(input);
     int *children = malloc(size * sizeof *children);
    
  3. Code should check the return value from fscanf(input, "%d",&count); and fscanf(input, "%d",&children[i]);

  4. Mixing types for finding the maximum is very questionable. Suggest re-write

    size_t chck_tall(const int *children, size_t size) {
      size_t total = 0;
      int tmp_max = 0;   // or maybe INT_MIN
      for(size_t i = size - 1; i-- > 0; )  {
        if(children[i] > tmp_max) {
          tmp_max = children[i];
          total++;
        }
      }
      return total;
    }
    
  1. Code should not mix pointer types. On systems where sizeof(unsigned short) < sizeof(int), this is a program flaw.

     // int *children = malloc(size * sizeof(unsigned short));
     int *children = malloc(size * sizeof(int));
     // or even better
     int *children = malloc(size * sizeof *children);
    
  2. For high portability, use type size_t or unsigned long for an array index that may exceed 65,535:

     // unsigned int size = count_children(input);
     size_t size = count_children(input);
     int *children = malloc(size * sizeof *children);
    
  3. Code should check the return value from fscanf(input, "%d",&count); and fscanf(input, "%d",&children[i]);

  4. Mixing types for finding the maximum is very questionable. Suggest re-write

    size_t chck_tall(const int *children, size_t size) {
      size_t total = 0;
      int tmp_max = 0;   // or maybe INT_MIN
      for(size_t i = size - 1; i-- > 0; )  {
        if(children[i] > tmp_max) {
          tmp_max = children[i];
          total++;
        }
      }
      return total;
    }
    
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92

  1. Code should not mix pointer types. On systems where sizeof(unsigned short) < sizeof(int), this is a program flaw.

     // int *children = malloc(size * sizeof(unsigned short));
     int *children = malloc(size * sizeof(int));
     // or even better
     int *children = malloc(size * sizeof *children);
    
  2. For high portability, use type size_t or unsigned long for values that may exceed 65,535:

     // unsigned int size = count_children(input);
     size_t size = count_children(input);
     int *children = malloc(size * sizeof *children);
    
  3. Code should check the return value from fscanf(input, "%d",&count); and fscanf(input, "%d",&children[i]);

  4. Mixing types for finding the maximum is very questionable. Suggest re-write

    size_t chck_tall(const int *children, size_t size) {
      size_t total = 0;
      int tmp_max = 0;   // or maybe INT_MIN
      for(size_t i = size - 1; i-- > 0; )  {
        if(children[i] > tmp_max) {
          tmp_max = children[i];
          total++;
        }
      }
      return total;
    }