Skip to main content

Posts

Showing posts from September, 2021

Programming in the large

 Programming in the large Abstraction top-down bowm-up Readability Function Size -A general rule of thumb is that your function should fit comfortably into one screen. (Traditionally, 80 columns by 24 lines was the standard size of a terminal) Naming Formatting -indenting -Java Style Formatting Commenting and Documentation Working in teams - using a good revision control system (e.g. Git, subversion, or Mercurial) -Integration summary& understanding  after reading  <<All of Programming>>  Chapter 13

Valgrind

  Using Valgrind to check memory How to use Valgrind -To valgrind the program, run the valgrind command and give it our program name as an argument. -For example, if we want to run ./myProgram hello 42, we can simply run Valgrind ./myProgram hello 42.  Uninitialized Values -When we run the program, we may use uninitialized values. It needs to be fixed. Valgrind can tell us about the use of uninitialized values. But it only tell when the control flow of the program depends on the unitialized value. For example, uninitialized value appears in the conditional expression of an if, or a loop, or in the switch statement. -If we want to know where the uninitialized value is from, we can use Valgrind    --track-origins=yes ./myProgram -Using -fsanitize=address can find a lot of problems that Memcheck cannot.  -We can use Valgrind with GDB to debug. We can run: --vgdb=full --vgdb-error=0., then Valgrind will stop on the first error that it encounters and give control to ...

Dynamic Allocation in C

Dynamic Allocation 1. Calling Function  -When calling a function, the array is created on the stack. And the variables on the stack exit only until the function ends. After it ends, the stack will be used for a new stack frame.  2. Dynamic memory allocation -Dynamic memory allocation allows a programmer to request a specific amount memory to be allocated on the heap. Since it is not in the stack, it is not freed when the function returns. -Instead, the programmer must explicitly free the memory when using it.  -The heap changes sizes as program runs. -The memory allocation library manages the free memory in the heap. When a request cannot be satisfied from the existing free blocks of memory, the allocation library requests that the upper boundary of the heap be increased.  -We can use realloc function to allocate a new block of memory, copying original one to that place and free the old one.  3. Dynamic memory allocation -We can use malloc to allocate dynamic me...

Interaction with users in C

  INTERACTING WITH USER AND SYSTEM 1. Operating System -Most interesting interactions that read inputs and write a file require access to hardware devices. It requires the permission from permissions system.  -The program asks the operating system to access hardware. It makes the request via a system call. It transfer control from the program into the operating system. The operating system checks that the program's request is within the bounds of its permissions before performing it. -It's more common to use functions in the C library than system calls. -printf is not part of the C library and includes significant code which does not require system calls. BUT it does contain a system call in some cases. It is not correct to cal printf a system call. 2. Errors in System Calls -Errors include: read a file that does not exist or does not have permissions for.  -Whenever system calls fails, their wrapper in the C library set a global variable called 'errno'. -To know why th...

Uses of Pointers in C

 STRINGS 1. Characteristic: Unmodifiable -A string is a sequence of characters, terminated by the null terminator character '\0'. It has a numerical value 0, but since we cannot type this normally, it is written with a backslash. -Strings are a subset of arrays of characters.  -We should use const when declaring a pointer pointing to a string. But if we forget to do so, the program will still compile. However, when we want to modify the string, the program will crash with a segmentation fault . (Hardware will trap into the operating system) -String literals are typically placed in a read-only portion of the static data section for their entire lifetime of the program. i.e. since loader -> memory until exit -After initializing, loader marks the read-only portions as non-writeable in the page table . (This is the structure that the operating system maintains to describe the program's memory to hardware) 2. Mutable Strings -If we want our strings to be mutable, we have ...