Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
enum { TABLESIZE = 100 };

int *table = NULL;

int insert_in_table(int pos, int value){
  if (!table) {
    table = (int *)malloc(sizeof(int) * TABLESIZE)new int[TABLESIZE];
  }
  if (pos >= TABLESIZE) {
    return -1;
  }
  table[pos] = value;
  return 0;
}

...

Code Block
bgColor#ccccff
enum { TABLESIZE = 100 };

int *table = NULL;

int insert_in_table(size_t pos, int value){
  if (!table) {
    table = (int *)malloc(sizeof(int) * TABLESIZE)new int[TABLESIZE];
  }
  if (pos >= TABLESIZE) {
    return -1;
  }
  table[pos] = value;
  return 0;
}

...

Code Block
bgColor#ccccff
template <typename T> 
void clear(T array[], size_t n) {
  for (size_t i = 0; i < n; ++i) {
    array[i] = 0;
  }
}

template <typename T, size_t n>
inline void clear(T (&array)[n]) {
  clear(array, n);
}

int int_array[12];

clear(int_array); // deduce T is int, and that n is 12

...