Esempio n. 1
0
int main(void) {
    char* name = NULL;
    int* age = NULL;
    name = (char*) calloc(BUFFER, sizeof(char)); // Allocate array using calloc
    age = (int*) malloc(sizeof(float)); // Allocate a float using malloc
    mem_leak(); // Creates memory leak allocated memory not freed
    printf("Enter your name: ");
    scanf("%s", name);
    printf("Enter your age: ");
    scanf("%d", age);
    // Arrays are like pointers, print each character of the name
    for (int i = 0; i < strlen(name); ++i) { // strlen fail if didn't use calloc!
        printf("%c\n", name[i]);
    }
    // Don't forget to free the memory. Can't free munch don't have pointer!
    free(name), free(age);
    return 0;
}
Esempio n. 2
0
int
main(int argc, char**argv)
{
  if (argc<2) {
    printf("Syntax:");
    printf("  %s <test-number> <test-number> <test-number> . . .\r\n", argv[0]);
    return -1;
  }

  if (mallopt(M_CHECK_ACTION, 7) != 1) {
    fprintf(stderr, "mallopt() failed");
    return(-1);
  }
  
  int ctr;
  for( ctr=1; ctr < argc; ctr++ )
  {
    int test_number = atoi(argv[ctr]);

    switch (test_number) {
      case 1: unintialized_use(); break;
      case 2: rw_after_free(); break;
      case 3: rw_malloc_overrun(); break;
      case 4: rw_bad_stk_location(); break;
      case 5: mem_leak(); break;
      case 6: test_6(); break;
      case 7: memcpy_overlapping_src_dst(); break;
      case 8: double_free(); break;
      case 9: systemcall_unaddressable_bytes(); break;
      case 10: stk_overrun(); break;
      case 11: glob_buff_overrun(); break;
      default: printf("No test or invalid test specified (only 1--9 are valid).");
               return -1;
    }
  }

  return 0;
}