fork(2) download
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4.  
  5. void report(char* eaf, char* initials, char* foo)
  6. {
  7. printf("eaf = %p, initials = %p, foo = %p\n", eaf, initials, foo);;
  8. printf("*eaf = %d, *initials = %d, *foo = %d\n", eaf[0], initials[0], foo[0]);
  9. }
  10.  
  11. void load(const char* label, const char* into, size_t intoSize)
  12. {
  13. assert(intoSize > 1); // can't store a string in 1 character.
  14. printf("%s\n", label);
  15.  
  16. char input[1024] = "";
  17. fgets(input, sizeof(input), stdin);
  18.  
  19. size_t len = strlen(input);
  20. // strip trailing \n off.
  21. if (len > 0 && input[len - 1] == '\n') {
  22. input[--len] = 0;
  23. }
  24.  
  25. // abort on empty input
  26. if (len <= 0) {
  27. fprintf(stderr, "Invalid input - terminated.\n");
  28. exit(1);
  29. }
  30.  
  31. if (len >= intoSize) {
  32. fprintf(stderr, "Invalid input - length was %u, limit is %u\n", len, intoSize - 1);
  33. exit(2);
  34. }
  35.  
  36. strncpy(into, input, intoSize);
  37. }
  38.  
  39. int main(int argc, const char* argv[])
  40. {
  41. char eaf[10], initials[1], foo[10];
  42. report(eaf, initials, foo);
  43.  
  44. load("Insert states of the optimized AFD", eaf, sizeof(eaf));
  45. report(eaf, initials, foo);
  46.  
  47. load("Insert initial AFD state", initials, sizeof(initials));
  48. report(eaf, initials, foo);
  49.  
  50. printf("eaf = %s\ninitials = %s\n", eaf, initials);
  51.  
  52. return 0;
  53. }
Runtime error #stdin #stdout #stderr 0s 1968KB
stdin
fred
bob
stdout
eaf = 0xbfd135ac, initials = 0xbfd135ab, foo = 0xbfd135b6
*eaf = -69, *initials = -65, *foo = -47
Insert states of the optimized AFD
eaf = 0xbfd135ac, initials = 0xbfd135ab, foo = 0xbfd135b6
*eaf = 102, *initials = -65, *foo = -47
stderr
prog: prog.c:13: load: Assertion `intoSize > 1' failed.