C++ · programming

C programming basics

Tricky operators:

  1. Global variable
  2. Reference
  3. Static
  4. computation operators: % (remainder), / (different for int and double)
  5. logic operators: ! (opposite)
  6. ++: x = y++; vs x = ++y;

Flow control:

  1. while vs do while: at least do once in do while

Functions:

  1. recursion vs iteration: iteration is better, because we need to call the recursion many times in recursion, and the information of calling functions is stored in stack, which is not a large part of memory. One may run out of memory when doing recursion, and this will not happen to for loops in iteration.
  2. In “void” functions without a return value, one can use “return” to stop the function, which is similar to break. Using “return” is simpler than “break”, because one does not need to state the condition. (not sure whether this is  a good manner)
  3. default argument:
  4.  Scope:  variables in a function/loop have local scope, so we need to pass the variable by reference (&) in order to modify the original variable.

Memory:

  1. stack: limited memory, storing initialized local variables, calling function
  2. heap: memory allocated by “new” and “malloc”
  3. BSS: uninitialized variables
  4. DATA:  initialized variables, read only
  5. Text: codes, read only

Pointers: int, 4 byte

  1. A pointer is a 4 byte int variable. A pointer contains an address and points to a value.
  2. The name of an array is actually a pointer to the first element in the array. So, we need to  use * to print the value of an array. For a char array, use the array name to print.
  3. const pointers:  const int a ;  const int *a (the value at that address cannot be changed, but the pointer can point to a new address); int * const a (the address cannot be changed); const int * const a (both the value and the address cannot be changed)
  4. class pointer: if there is a class called Book, and one wants to initialize several Book instances, one can do: Book books[] (define an array, convenient but not flexible); Book *books (define a pointer, the pointer may point to an array, and the entry of the array is a Book instance, so the length of the entries is changing, use “new”; the pointer can also point to …??); Book **books ( still define a pointer, the pointer may point to an array, and the entry of the array is a fixed length pointer, and this pointer points to a Book instance, recommended); Book *books[];                                                    A objBT; int* p = (int*)&objBT; (objBT is a class A instance, so we cannot let an int pointer p point to it, this sentence just tells the compiler to let p points to objBT, none of the data types change )                                                                                                           A* objBT; then A can point to a derived class instance without type cast.

String:

  1. Use “” for string, and ” for characters.
  2. char  c = “a”  (initialize a character);  char * a  = “abcd”  (a points to a string, it is a const stored in DATA, read only);  char a[] = {‘a’,’b’,’c’,’d’} or char a[] = “abcd” (a is an array, stored in stack, can be changed); string strA = “abcd” ( allocate 128 bytes when it is defined, when the memory used is close to 128 bytes, allocate new memory automatically, recommended);

Const:

  1. const string getValue() (the return value is const, cannot be changed); string getValue()const{…;} (the variable in the function is const, cannot be changed)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s