Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
c [2019/02/16 03:48]
paul [STL containers]
c [2020/10/23 02:53] (current)
Line 1: Line 1:
-===== Rule of Three =====+====== C and C++ ======
  
 +===== C declerations =====
 +
 +A c decleration can be read by following this rule and knowing about precedence.
 +
 +Go left when you can and right when you must.
 +
 +<code>
 +char *(*(**foo [][8])())[];
 +</code>
 +
 +foo is an array to an array of 8 pointers to poiners to a function that returns
 +a pointer to an array of pointers to char
 +
 +===== C++ Interview questions =====
 +
 +I was given a pop quiz by a non-tech interviewer once on c++. Here was his questions:
 +
 +  - What is the name of an invalid pointer? 
 +      - Weird question and I don't think it's right. Answer he was looking for was null.
 +  - What is the difference between a struct and a class? 
 +  - What is the keyword to deallocate memory? Lol
 +  - When using a new keyword where is the memory stored, stack or heap? Heap OBVI.
 +  - If a program crashes and you have a core dump and debugger, what is the first thing you do? 
 +      - Look at a backtrace, see where the program crashed, look at what the variable were.
 +===== General C++ ===== 
 +
 +Principles of C++: (([[https://www.grimm-jaud.de/images/stories/pdfs/c%2B%2B11_AnOverview.pdf|Rainer Grimm's C++ 11 overview]]))
 +  * Trust the programmer.
 +  * You don't have to pay for something you don't need.
 +  * Don't break existing code.
 +  * Prefer compile time errors over run time errors.
 +
 +===== Differences Between C and C++ =====
 +
 +Here are some differences I have come across between C and C++. This stuff is
 +interesting because it highlights the features that make C++ more powerful, and also
 +gives you a better understanding of the way the two languages work.
 +
 +  * Biggest one: C does not have classes and objects
 +  * C++ has pass by reference. C does not.
 +  * C++ implements name mangling when compiling functions, C does not.
 +
 +===== C Preprocessor (CPP) =====
 +
 +Much of what I have learned comes from two places: the GNU CPP manual
 +[[https://gcc.gnu.org/onlinedocs/gcc-8.3.0/cpp/|link]] and a guide to the CPP
 +[[https://docs.freebsd.org/info/cpp/cpp.pdf|link]].
 +
 +''%%__VA_ARGS__%%'' is a CPP (c pre processor) identifier that gets replaced with the values
 +of ... when used in a variadic macro.
 +
 +A simple macro is a kind of abbreviation. It is a name which stands for a fragment of
 +code. Some people refer to these as manifest constants. 
 +
 +Before you can use a macro, you must define it explicitly with the
 +''%%#define%%'' directive. ''%%#define%%'' is followed by the name of the macro
 +and then the code it should be an abbreviation for. For example,
 +
 +<code c++>
 +#define BUFFER_SIZE 1020
 +</code>
 +
 +The use of all upper case for macro names is a standard convention. Programs are easier
 +to read when it is possible to tell at a glance which names are macros.
 +
 +The C preprocessor scans your program sequentially, so macro definitions take
 +effect at the place you write them.
 +
 +<code c++>
 +foo = X;
 +#define X 4
 +bar = X;
 +</code>
 +
 +Produces as output:
 +
 +<code c++>
 +foo = X;
 +bar = 4;
 +</code>
 +
 +You can also use ''%%#defines%%'' to define functions. 
 +
 +The ''%%do { } while(0)%%'' technique in macro definitions is used to avoid
 +issues with generating double expressions caused by the addition of a semicolon.
 +Doing the following:
 +
 +<code c++>
 +#define SOME_MACRO(VAR) \
 +do {    int i = var;    \
 +        i = 12*i; }     \
 +        while (0) 
 +</code>
 +        
 +When you call this macro in the your code as ''%%SOME_MACRO(var);%%'' it expands into one
 +statement which is:
 +
 +<code c++>
 +do { ... } while (0); 
 +</code>
 +
 +This way you can use a macro expansion in a if else statement with no curly braces
 +where more than one statement would mess stuff up.
 +
 +===== C Declerations =====
 +
 +First, understand the breakdown of a decleration in c.
 +
 +===== Rule of Three =====
  
 ===== Rule of Five ===== ===== Rule of Five =====
Line 24: Line 133:
     * **begin()**  returns an iterator at the beginning of the container     * **begin()**  returns an iterator at the beginning of the container
     * **end()** returns an iterator at the start of the container     * **end()** returns an iterator at the start of the container
-    * **cbegin()** returns a const iterator at the start of the container +    * **cbegin()** returns a ''%%const%%'' iterator at the start of the container 
-    * **cend()** returns a const iterator at the end of the container+    * **cend()** returns a ''%%const%%'' iterator at the end of the container
  
-Iterators are members of an stl container. For example to get an iterator for a specific type of list:+Iterators are members of an STL container. For example to get an iterator for a specific type of list:
 <code c++> <code c++>
 std::map<int, std::string>::const_iterator mapIt = myMap.cbegin();  std::map<int, std::string>::const_iterator mapIt = myMap.cbegin(); 
Line 37: Line 146:
 ==== Deque ==== ==== Deque ====
  
-An std::deque is double ended array that can grow from each end?+An ''%%std::deque%%'' is double ended array that can grow from each end
 + 
 +==== Pair ==== 
 + 
 +An ''%%std::pair%%'' is an object that holds a pair of objects. 
 + 
 +<code cpp> 
 +// pair::pair example 
 +#include <utility>      // std::pair, std::make_pair 
 +#include <string>       // std::string 
 +#include <iostream>     // std::cout 
 + 
 +int main () { 
 +  std::pair <std::string,double> product1;                     // default constructor 
 +  std::pair <std::string,double> product2 ("tomatoes",2.30);   // value init 
 +  std::pair <std::string,double> product3 (product2);          // copy constructor 
 + 
 +  product1 = std::make_pair(std::string("lightbulbs"),0.99);   // using make_pair (move) 
 + 
 +  product2.first = "shoes";                  // the type of first is string 
 +  product2.second = 39.90;                   // the type of second is double 
 + 
 +  std::cout << "The price of " << product1.first << " is $" << product1.second << '\n'; 
 +  std::cout << "The price of " << product2.first << " is $" << product2.second << '\n'; 
 +  std::cout << "The price of " << product3.first << " is $" << product3.second << '\n'; 
 +  return 0; 
 +
 +</code>
  
 ==== List ==== ==== List ====
  
-An std::list is a sequence container where each element contains a pointer to the next and previous element. You can't randomly access elements, you have to "walk the list". But inserting elements is very fast if you know where to insert them.+An ''%%std::list%%'' is a sequence container where each element contains a pointer to 
 +the next and previous element. You can't randomly access elements, you have to 
 +"walk the list". But inserting elements is very fast if you know where to insert 
 +them.
  
 ==== Set ==== ==== Set ====
  
-An std::set is an ordered list of unique elements that get automatically sorted as we insert them.+An ''%%std::set%%'' is an ordered list of unique elements that get automatically 
 +sorted as we insert them.
  
 ==== Multiset ==== ==== Multiset ====
  
-An std::multiset is an ordered list of elements that can contain duplicate data which is automatically sorted as we insert them.+An ''%%std::multiset%%'' is an ordered list of elements that can contain 
 +duplicate data which is automatically sorted as we insert them.
  
 ==== Map ==== ==== Map ====
  
-An std::map is an associated array which is a map, symbol table or dictionary that has a collection of key value pairs such that a key only shows up once. +An ''%%std::map%%'' is an associated array which is a map, symbol table or 
 +dictionary that has a collection of key value pairs such that a key only shows 
 +up once. 
  
-Data pairs must be inserted into a map, and while you do this they are automatically sorted. You can make pairs with the *std::make_pair(x,y)helper function.+Data pairs must be inserted into a map, and while you do this they are 
 +automatically sorted. You can make pairs with the ''%%std::make_pair(x,y)%%'' helper 
 +function.
  
-<code>+<code cpp>
 std::map<int, std::string> myMap; std::map<int, std::string> myMap;
 myMap.insert(std::make_pair(1, "mango")); myMap.insert(std::make_pair(1, "mango"));
 </code> </code>
 +
 ===== Memory Leaks ===== ===== Memory Leaks =====
  
-For every new there must be a delete. Use the following valgrind command to profile for mem leaks((https://www.cprogramming.com/debugging/valgrind.html)): 
  
-<code>valgrind --tool=memcheck --leak-check=yes name_of_exec+For every new there must be a delete. Use the following valgrind command to 
 +profile for memory leaks [[https://www.cprogramming.com/debugging/valgrind.html|link]]. 
 + 
 +<code bash> 
 +valgrind --tool=memcheck --leak-check=yes name_of_exec 
 +</code> 
 + 
 +===== Lambda Functions ====== 
 + 
 +Lambda functions are: 
 +  * functions without a name 
 +  * define their functionality right in place 
 +  * can be copied like data 
 + 
 +Lambda functions should be precise and self explaining. 
 + 
 +The syntax is as follows: 
 + 
 +{{::screenshot_2019-03-13_17-07-19.png?400|}} 
 +  * [] : captures the used variables per copy of per reference 
 +  * () : is required for parameters 
 +  * -> : is required for a return value 
 +  * {} : may include expressions and statements 
 + 
 +===== Range-based for statement ===== 
 + 
 +C++ 11 introduced the range-based ''%%for%%'' statement (([[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf|Section 6.5.4 c++ 11 standard]])) and takes the form: 
 +<code> 
 +for ( for-range-declaration : expression ) statement 
 +</code> 
 + 
 +Example: 
 +<code cpp> 
 +int array[5] = { 1, 2, 3, 4, 5 }; 
 +for (int& x : array) 
 +    x *= 2;
 </code> </code>
  
 ===== Debugging ===== ===== Debugging =====
  
-There are lots of options for debugging. I make extensive use of DDD with ROS which helped a lot. gdbgui is an interesting web based front end, but i tried it for 5 mins and ran into issues with it debugging a simple program.+There are lots of options for debugging. I make extensive use of DDD with ROS 
 +which helped a lot. gdbgui is an interesting web based front end, but i tried it 
 +for 5 mins and ran into issues with it debugging a simple program. 
 + 
 +==== GDB ==== 
 + 
 +Debugging in GDB is not that bad! It is also really useful when looking at simple 
 +stuff for learning stuff. 
 + 
 +This is a really nice intro [[http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb|link]]. 
 + 
 +This is a really nice video on gdbtui (which is the text user input) mode [[https://www.youtube.com/watch?v=RM3xwFxr9-Q|link]]. 
 + 
 +To debug in GDB you need to compile a program with a debug with a ''%%-g%%'' 
 +option.  
 + 
 +For macros see the following [[https://sourceware.org/gdb/onlinedocs/gdb/Macros.html|link]]. 
 + 
 + 
 +===== Mutable ===== 
 + 
 +''%%mutable%%'' permits modification of the class member declared mutable even if the containing object is declared const. 
 +<code cpp> 
 +int main() 
 +
 +    int n1 = 0;           // non-const object 
 +    const int n2 = 0;     // const object 
 +    int const n3 = 0;     // const object (same as n2) 
 +    volatile int n4 = 0;  // volatile object 
 +    const struct 
 +    { 
 +        int n1; 
 +        mutable int n2; 
 +    } x = {0, 0};      // const object with mutable member 
 +  
 +    n1 = 1; // ok, modifiable object 
 +//  n2 = 2; // error: non-modifiable object 
 +    n4 = 3; // ok, treated as a side-effect 
 +//  x.n1 = 4; // error: member of a const object is const 
 +    x.n2 = 4; // ok, mutable member of a const object isn't const 
 +  
 +    const int& r1 = n1; // reference to const bound to non-const object 
 +//  r1 = 2; // error: attempt to modify through reference to const 
 +    const_cast<int&>(r1) = 2; // ok, modifies non-const object n1 
 +  
 +    const int& r2 = n2; // reference to const bound to const object 
 +//  r2 = 2; // error: attempt to modify through reference to const 
 +//  const_cast<int&>(r2) = 2; // undefined behavior: attempt to modify const object n2 
 +
 +</code> 
 +Output: 
 +<code> 
 +# typical machine code produced on an x86_64 platform 
 +# (only the code that contributes to observable side-effects is emitted) 
 +main: 
 +    movl    $0, -4(%rsp) # volatile int n4 = 0; 
 +    movl    $3, -4(%rsp) # n4 = 3; 
 +    xorl    %eax, %eax   # return 0 (implicit) 
 +    ret 
 +</code> 
 + 
 +==== Pointer Syntax ==== 
 + 
 + 
 +<code cpp>sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes;</code> 
 + 
 +Does not declare 3 pointers - it is one pointer and 2 objects. 
 + 
 +<code cpp> 
 +sf::Sprite* unfortunately does not apply to all the variables declared following it, just the first. It is equivalent to 
 + 
 +sf::Sprite* re_sprite_hair; 
 +sf::Sprite re_sprite_body; 
 +sf::Sprite re_sprite_eyes; 
 +</code> 
 + 
 +You want to do: 
 + 
 +<code cpp> 
 +sf::Sprite *re_sprite_hair, *re_sprite_body, *re_sprite_eyes; 
 +</code> 
 + 
 +===== Smart Pointers ===== 
 + 
 +The STL library provides three types of "smart pointers", %%std::unique_ptr%%, %%std::shared_ptr%% and %%std::weak_ptr%%. 
 + 
  • c.1550288918.txt.gz
  • Last modified: 2019/02/16 03:48
  • by paul