Week 38 / 2022

Mohamed Saif published on
5 min, 908 words

C: chapter 5

  • A static variable inside a function keeps its value between invocations. A static global variable or a function is "seen" only in the file it's declared in. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.
  • by far the most frequent use of arrays of pointers is to store character strings of diverse lengths
  • When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string.
  • Since argv is a pointer to an array of pointers, we can manipulate the pointer rather than index the array.
  • In C, a function itself is not a variable, but it is possible to define pointers to functions, which can be assigned, placed in arrays, passed to functions, returned by functions, and so on.

C: chapter 6

  • There are at least three possible approaches: pass components separately, pass an entire structure, or pass a pointer to it.

  • struct point *pp: says that pp is a pointer to a structure of type struct point.

  • But the very fact that the arrays are parallel suggests a different organization, an array of structures.

  • array of structs: struct key {...} keytab[] = {{..}, {..}, {..}, {..},}; inner braces are not necessary when the initializers are simple variables or character strings, and when all are present.

  • A sizeof can not be used in a #if line, because the preprocessor does not parse type names.

  • It is illegal for a structure to contain an instance of itself

  • In C, the proper method is to declare that malloc returns a pointer to void, then explicitly coerce the pointer into the desired type with a cast. malloc and related routines are declared in the standard header <stdlib.h>

  • malloc returns NULL if no space is available

  • C provides a facility called typedef for creating new data type names: typedef int Length; makes the name Length a synonym for int. likewise, typedef char *String; makes the name String a pointer of type char.

        typedef struct tnode *Treeptr;
        typedef struct tnode { /* the tree node: */
            char *word; /* points to the text */
            int count; /* number of occurrences */
            struct tnode *left; /* left child */
            struct tnode *right; /* right child */
        } Treenode;
    

    This creates two new type keywords called Treenode (a structure) and Treeptr (a pointer to the structure).

  • A union is a variable that may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements. Unions provide a way to manipulate different kinds of data in a single area of storage, without embedding any machine-dependent information in the program.

  • Bit-fields: The numbers must be powers of two. Then accessing the bits becomes a matter of bit-fiddling with the shifting, masking, and complementing operators

Makefile and CMake

  • they're build automation software
  • make is the oldest. It's pretty universal on Unix-like systems including Linux and modern macOS, and it's sometimes used on Windows too.
  • CMake much newer. Its main improvement over make is that it supports Windows and other platforms that are less like Unix much better, and it integrates better with the IDEs that people use often - for example, you can create a single software package with a single CMakefile that can then be used to generate files for Visual Studio on Windows and for XCode on macOS. Essentially those IDEs already do build automation, and CMake allows you to take advantage of those tools without maintaining two separate build files. (focusing on IDE support)
  • Make is still great for smaller projects, and projects that only need to run on Unix/Linux/macOS, and projects that aim to have as few requirements as possible. (opinion)
  • CMake tends to be more popular for open-source software libraries that need to be used by thousands of different programmers on different platforms with different requirements and uses.(opinion)
  • Bazel tends to be more popular when a single development team has a really large project (millions of lines of code) and they want to all standardize on a build tool that's extremely fast and robust. (opinion)

Memory Leaks

  • A memory leak is a block of dynamically allocated memory, to which you have no pointers in your program's state.
  • n order to free dynamic memory block that you no longer care about, you need to have a pointer to that memory block (to pass it to the deallocation function). If you don't have such a pointer and cannot derive it in some way, that memory block is a memory leak. You will never be able to deallocate it.

Programming Paradigms

Tech Books: How to Approach?

  • Do exercises, do exercises, do exercises.
  • The ultimate level of learning is where you create something using the knowledge.
  • Now that you've earned your bachelor's, the goal of your learning has to shift away from basic foundational stuff towards focused, applied stuff.

Web App Architecture 101