Week 3.a CS3650 01/22 2024 https://naizhengtan.github.io/24spring/ (continued from last time) 5. C strings 6. C arrays 7. struct and malloc 8. IO --------------------- - review sessions * TAs are eager to help - Lab2 out * my advice: start early! * many to learn - Assignment 1 out * assignment is supposed to reinforce lectures Recap last time: 1. A life cycle of a program 2. Why C? 3. C basics 4. C pointers 5. C strings Q: what is a "string"? [Answer: a data structure (or a type) containing a sequence of characters] For example, "hello" => 'h', 'e', 'l', 'l', 'o' -if I were a language designer, here is a string design: +-----+--+-----+---- | len |c | int | ... +-----+--+-----+---- => "len" is the length of the string; "c" is the char (1B); and "int" is the position of the char in the string "hello" => [5,'e',1,'h',0,'l',2,'l',3,'o',4] - Q: do you think the design works? Why and why not? [Answer: it works. But, just super inefficient] - the design point is how to tell where is the end of a string? -- for C strings, they end with '\0' [an example: char* b = "hello"; int main() { for (int i=0; i<6; i++) { printf("char[%d] = '%c' (%d)\n", i, b[i], (int)b[i]); } return 0; } ] Q: what if I loop another time? ("i<6"=>"i<7") A: something will be printed. This char however is undefined based on what has been stored on the next byte after the string. 6. C arrays Q: What is an array? - an array: a sequence of units holding same data type (char, int, etc.) - C array: a sequence of units holding same data type (char, int, etc.) in a contiguous piece of memory also, no bound checking, no growing - two ways to access arrays: through index: buf[0], buf[1] through pointer: *buf, *(buf+1) [an example: int a[3] = {1, 2, 3}; // an array of 3 int's char b[3] = {'a', 'b', 'c'}; // an array of 3 char's int main() { printf("%d\n", a[0]); a[1] += 1; // use index access *(a+2) = 5; // pointer access printf("%d %d\n", a[1], a[2]); // pointers to array elements printf("a %p a1 %p a2 %p a2 %p\n", a, a+1, a+2, &a[2]); // pointer arithmetic uses type printf("%p %p\n", b, b+1); return 0; } ] * Notice that a C string is an array of char ending with a NULL ('\0'). So, these make sense in C: char *s = "abc"; char sx[] = "abc"; 7. struct and malloc - array is a sequence of data of **the same type**. Q: What about a collection of **different types**? A: that is called "struct" - one way to see "struct" if you have learned OO programming: You can view structs as rudimentary "objects" without associated member functions [an example: struct student { int id; char *name; }; int main() { struct student t; t.id = 1024; t.name = "alice"; struct student *p = &t; p->id += 1; printf("t.id =%d, t.name =%s\n", t.id, t.name); printf("p->id=%d, p->name=%s\n", p->id, p->name); } ] - Q: if we want to factor out a function to new a student: [a motivating example: typedef struct { int id; char *name; } student; student *new_student() { student a = {1, "bob"}; return &a; } void foo(); int main() { student *p = new_student(); // foo(); printf("p->id=%d, p->name=%s\n", p->id, p->name); } void foo() { //char *a = "this is just some random string"; student a = {99999999, "RANDOM STUDENT"}; } ] Q: why invoking "foo()" changes the result? A: because the local variables' scope is within function. when function exits, the memory will be reused by other functions. Q: so what to do? A: dynamically allocate memory - malloc/free: #include "stdlib.h" void *malloc(size_t size); void free(void *ptr); [example: student *new_student() { student *a = (student*)malloc(sizeof(student)); a->id = 1; a->name = "bob"; return a; } ] - NOTE: if you ever malloc, you need to free it. Otherwise, there will be memory leak. One can use valgrind (a memory error detector) to check memory leak: $ valgrind --leak-check=full ./your_program - malloc/free are used to "dynamically allocate memory". - recall the motivation example: where are local vars, global vars, and malloc memory locate? [draw a brief memory layout of a process] We will talk in much detail about this in our next module, processes. 8. IO * printf -- extremely versatile -- show the "man page" for printf * file IO -- helper functions for syscalls: open/read/write