C & Cpp Notes - robbiehume/CS-Notes GitHub Wiki
- Programming with C++ as a second language
- C++ for Python Programmers
- C++ for Python Programmers (different resource)
- PCAL steps
- Preprocessing
- Compiling
- Linking
- Assembly
- To compile and get executable:
- C: gcc, C++: g++
g++ <file_name1> <file_name2> ... -o <executable_name>
- clang is another compiler that can be used. It uses CMake
- Joe recommends CMake (he says its the latest and greatest)
- Allow you to just compile parts you want to
- Structure:
-
<target>: <dependencies> <action>
-
- Determines what pieces of a large program need to be re-compiled, and issue the commands necessary to recompile them
- It's typically used to build executable programs and libraries from source code
-
A pointer contains the starting memory address of the data it points to
-
Address operator (
&
) is used to get the address of a variable&var
-
Declare a pointer with
*
-
int *ptr
orint* ptr
-
-
Assigning address to pointer variable
ptr = &var
-
Dereferencing a pointer
- When
*
is used outside of variable declaration, it means dereferencing the pointer -
*ptr
// means the value at the address that ptr points to - When dereferencing a pointer, it looks at the type of pointer to know how many bytes from the base address to retrieve
- When
-
Pointer arithmetic:
- Can add / subtract integers to/from a pointer to access different memory addresses
- Whatever integer is added/subtracted, multiply it by the size of the pointer type
- Ex: int ptr equals contains 1000
-
ptr = ptr + 1
// ptr now equals 1004: 1000 + (1 * sizeof(int))
-
-
p[1]
is equivalent to*(p+1)
; both get the value of the 2nd element -
&p[1]
is equivalent top+1
; both get the memory address of the 2nd element
-
Double pointers (pointer to a pointer):
- Use cases
- A pointer itself occupies a memory address, so you can have another pointer that points to it
- An example use would be if you wanted an array of char *
- Not really needed in C++?
-
Pass by value
- Changes made by the function have no effect on value of the original argument from the calling function
- definition:
func(int x)
call:func(x)
-
Pass by reference (C)
- Addresses of variables are passed so that changes are made to the original argument variable
- definition:
func(int *x)
call:func(&x)
orfunc(ptr)
(where ptr is a pointer to x)
-
C++
-
Pass by pointer vs reference
- Pass by reference is preferred, unless specifically needing to use a pointer
- Pass by address / pointer: same as pass by reference in C
-
Pass by reference:
- In C++, you can add a
&
to a function definition parameter and it will create a reference to the variable passed and any changes will change the original variable - Essentially get to have the capability of pass by pointer, but in a cleaner / easier way where you don't have to do any pointer dereferencing
- If passing an rvalue (see rvalue vs rvalue below), must add
const
in front of parameter type (const QString& s
)- The benefits of using the reference are to avoid unnecessary copying and construction of temporary values
- In C++, you can add a
-
int i = 2; func(i); // after this i will be changed to 5 } func(int& i){ i = 5; // don't need to dereference like with a pointer (*i = 5) }
-
Pass by pointer vs reference
Arrays vs pointers (link)
- When an array is passed to a function, it just passes the pointer to the first element
- A few differences of arrays vs pointers
-
sizeof(arr)
returns the total size of the elements in the array;sizeof(ptr)
returns the size of the pointer itself -
arr
is an alias for&arr[0]
(address of the first element of the array);&ptr
returns the address of the pointer variable itself -
char arr[] = "abc"
sets first 4 elements in arr to 'a','b', 'c', and '\0'-
char *ptr = "abc"
sets pointer to address of the "abc" string (which may be stored in read-only memory and thus unchangeable
-
-
- Initializing an array:
- On the stack:
int arr[n];
- Will be deleted once the calling function ends (goes out of scope)
- On the heap:
int* arr = malloc(n * sizeof(int));
- Will stay around until deallocated with
free()
- Preferred way unless you know the array size is small or you only need it for a short period of time
- Will stay around until deallocated with
- On the stack:
-
Virtual function: a member function that is declared in the base class and overridden by a derived class
- Pure virtual function: means it can't be defined in the parent but has to be defined in any child subclass
- They ensure the correct function is called for an object, regardless of the type of reference (or pointer) used for function call
-
Note: only need to add the
virtual
keyword to the function in the base class, not any derived classes- Can add the
override
keyword to the derived function to ensure no mistakes are made (link)
- Can add the
lvalue vs rvalue: link
- lvalue: something that points to a specific memory location
- rvalue: something that doesn't point anywhere
- In general, rvalues are temporary and short lived, while lvalues live a longer life since they exist as variables
- Another way to think about it is lvalues as containers and rvalues as things contained in the containers
- Ex:
int x = 2;
-
2
is an rvalue; a number has no specific memory address except for a temporary register -
x
is an lvalue; a variable has a specific memory location and allows for things likeint* y = &x
-
- A copy constructor is a member function that initializes an object using another object of the same class
- In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor