diff --git a/.gitignore b/.gitignore index f0cafca..ba70071 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,4 @@ metals.sbt /.worksheet/ # Misc. -.png +*.c diff --git a/src/main/scala/ch01/13-test.scala b/src/main/scala/ch01/13-test.scala index d5d5a5d..5a91ace 100644 --- a/src/main/scala/ch01/13-test.scala +++ b/src/main/scala/ch01/13-test.scala @@ -2,7 +2,6 @@ package ch01.test import scalanative.unsafe.{CQuote, CString, CSize, CChar, Ptr, sizeof} import scalanative.libc.{stdio, string} -import scala.scalanative.unsigned.UnsignedRichInt @main def cStringExperiment2: Unit = diff --git a/src/main/scala/ch01/bad_stuff.c b/src/main/scala/ch01/bad_stuff.c deleted file mode 100644 index 9edae9a..0000000 --- a/src/main/scala/ch01/bad_stuff.c +++ /dev/null @@ -1,69 +0,0 @@ -#include // this has to be included in any file doing I/O -#include // needed for exit() and constants -// #include - -// This was an attempt to replicate segfault in badSscanfExample, but in C: -// int main(int argc, char *argv[]) -// { -// char *line_buffer = calloc(1024, sizeof(char)); - -// while (fgets(line_buffer, 1023, stdin) != NULL) -// { -// char *string_pointer = calloc(1, sizeof(char *)); -// int scan_result = sscanf(line_buffer, "%s\n", string_pointer); - -// if (scan_result < 1) -// { -// printf("insufficient matches in sscanf: %d\n", scan_result); -// exit(EXIT_FAILURE); -// } - -// printf("scan results: %s\n", string_pointer); -// } - -// return EXIT_SUCCESS; -// } - -// Stack overflow -// int main(void) -// { -// return main(); // segfault! Clang compiles without errors. -// } - -int main(int argc, char *argv[]) -{ - // Writing to read-only memory - // char *s = "hello world\n"; - // *s = 'H'; // compiles, but if you run, segfault! - - // Dereferencing a null pointer, attempting to read its value - // int *ptr = NULL; // it also works with: int *ptr; - // printf("%d", *ptr); // compiles, but if you run, segfault! - - // Dereferencing a null pointer, attempting to update its value - // int *ptr = NULL; - // *ptr = 1; // compiles, but if you run, segfault! - - // Dereferencing a null pointer without doing anything - // int *ptr = NULL; - // *ptr; // no segfault but unused warning (dead-code elimination by compiler) - - // Buffer overflow - // array index 20 is past the end of the array (which contains 12 elements) - // char s[] = "hello world"; - // char c = s[20]; // clang compiles with warnings, not always segfaults. - - // Accessing an address that has been freed - int *p = malloc(sizeof(int)); // allocating memory to p - *p = 2; - printf("The value of the int: %d\n", *p); // prints 2 - free(p); // deallocated the space allocated to p - *p = 34; // no segfault on clang or gcc. Why? - printf("The value of the int: %d\n", *p); // prints 34, no problems! - - // Improper use of scanf - // int n = 2; - // scanf(" ", n); // no segfault! Damn. - - return EXIT_SUCCESS; -}