Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
c [2019/03/01 20:26] 127.0.0.1 external edit |
c [2020/10/23 02:53] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== C and C++ ====== | ====== 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. | ||
+ | |||
+ | < | ||
+ | char *(*(**foo [][8])())[]; | ||
+ | </ | ||
+ | |||
+ | 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:// | ||
+ | * 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++ ===== | ===== Differences Between C and C++ ===== | ||
Line 72: | Line 104: | ||
This way you can use a macro expansion in a if else statement with no curly braces | 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. | 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 Three ===== | ||
Line 111: | Line 147: | ||
An '' | An '' | ||
+ | |||
+ | ==== Pair ==== | ||
+ | |||
+ | An '' | ||
+ | |||
+ | <code cpp> | ||
+ | // pair::pair example | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main () { | ||
+ | std::pair < | ||
+ | std::pair < | ||
+ | std::pair < | ||
+ | |||
+ | product1 = std:: | ||
+ | |||
+ | product2.first = " | ||
+ | product2.second = 39.90; | ||
+ | |||
+ | std::cout << "The price of " << product1.first << " is $" << product1.second << ' | ||
+ | std::cout << "The price of " << product2.first << " is $" << product2.second << ' | ||
+ | std::cout << "The price of " << product3.first << " is $" << product3.second << ' | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
==== List ==== | ==== List ==== | ||
Line 139: | Line 202: | ||
function. | function. | ||
- | < | + | < |
std:: | std:: | ||
myMap.insert(std:: | myMap.insert(std:: | ||
Line 145: | Line 208: | ||
===== Memory Leaks ===== | ===== Memory Leaks ===== | ||
+ | |||
For every new there must be a delete. Use the following valgrind command to | For every new there must be a delete. Use the following valgrind command to | ||
Line 151: | Line 215: | ||
<code bash> | <code bash> | ||
valgrind --tool=memcheck --leak-check=yes name_of_exec | valgrind --tool=memcheck --leak-check=yes name_of_exec | ||
+ | </ | ||
+ | |||
+ | ===== 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: | ||
+ | |||
+ | {{:: | ||
+ | * [] : 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 ( for-range-declaration : expression ) statement | ||
+ | </ | ||
+ | |||
+ | Example: | ||
+ | <code cpp> | ||
+ | int array[5] = { 1, 2, 3, 4, 5 }; | ||
+ | for (int& x : array) | ||
+ | x *= 2; | ||
</ | </ | ||
Line 173: | Line 268: | ||
For macros see the following [[https:// | For macros see the following [[https:// | ||
- | ~~DISCUSSION~~ | + | |
+ | ===== Mutable ===== | ||
+ | |||
+ | '' | ||
+ | <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< | ||
+ | |||
+ | const int& r2 = n2; // reference to const bound to const object | ||
+ | // r2 = 2; // error: attempt to modify through reference to const | ||
+ | // const_cast< | ||
+ | } | ||
+ | </ | ||
+ | Output: | ||
+ | < | ||
+ | # 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 | ||
+ | </ | ||
+ | |||
+ | ==== Pointer Syntax ==== | ||
+ | |||
+ | |||
+ | <code cpp> | ||
+ | |||
+ | 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; | ||
+ | </ | ||
+ | |||
+ | You want to do: | ||
+ | |||
+ | <code cpp> | ||
+ | sf::Sprite *re_sprite_hair, | ||
+ | </ | ||
+ | |||
+ | ===== Smart Pointers ===== | ||
+ | |||
+ | The STL library provides three types of "smart pointers", | ||
+ |