Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

2

Static – variables in function are declared only once, like a count variable that is declared at 0 and
every time the function rolls it increases;

Static – variables in class are defined and declared only once -there is only one instance of them -
and can be used outside class. Their value needs to be declared/incremented before making any
objects of the class – globally, like “int Car::Count = 0”

Static – variables outside of class can be used by any function or class in the file but by nothing
outside of it, they aren’t global, just restrained to the file

Static – functions in a class are available outside of the class, and can only reach the other static
members of said class. (“Car::ChangeMember()”)

Pointers – void *ptr = nullptr;

- Void *ptr = &x;


- Int * ptr = new int(10);
- Delete ptr;
- Char* f = new char[10];
- Delete[] f;
- Char** ptr2 = &f; (pointer to pointer)

STACK VS HEAP

Hmm. Cred ca ajunge sa știi ca heap e memoria dinamică. Și cum se aloca si șterge memoria.
Și ce fel de date se stochează in memoria dinamică (clasele de obicei). Si stack unde se țin de
exemplu toate variabilele primitive (int, char, etc.) Și memoria se eliberează automat când se
iese din scope. Si pe stack se salvează si call-urile funcțiilor

C style strings

<cstring> header

<iterator> for std::size

Strcpy(dest, source) -> copy string to another string (can cause overflows though)

IT’S RECOMMENDED TO USE strncpy INSTEAD, WHICH ALLOWS TO SPECIFY THE SIZE
OF THE BUFFER, BUT DOESN’T ENSURE THE STRINGS ARE NULL TERMINATED SO OVERFLOW IS STILL
POSSIBLE

Strcpy_s() is also recommended, you can specify how many characters to add (not
accepted by all compilers) also you need #define __STDC_WANT_LIB_EXT1__ 1
Strlen(source) -> length of string without the null terminator

strcat() -- Appends one string to another (dangerous)

strncat() -- Appends one string to another (with buffer length check)

strcmp() -- Compare two strings (returns 0 if equal, <0 if s1 < s2, >0 if s1>s2)

strncmp() -- Compare two strings up to a specific number of characters (returns 0 if equal)

strchr(str, ch) – pointer to the first appearance of ch in the string

strstr(str1, str2) – pointer to first appearance of str2 in str1

strlen() - <string.h>, num of characters before null

string class functions:

 Append()
 Assign()
 At()
 Begin(), end()
 Capacity()
 Compare()
 Empty()
 Erase()
 Find()
 Length()
 Swap()

Bit fields

- Int x : 4;
- Use it to Reduce class/struct size if we can
- Unsigned int, int, bool, char, u char, short, long, long long, u short, u long, u long long
- 1 byte = 8 bits;
- Int = 4 bytes;
- Fill the rest of the byte boundary with int : 0;
- Pointers to bit field members not allowed (they don’t start at byte field boundary)
- Can’t have static bit fields
- Can’t have array of bit fields

Bit manipulation

 & (bitwise AND) – does AND on every bit of 2 numbers, the result of AND is 1 if both bits are
1
o Ex a = 00000101
o B = 00001001
o A&b = 00000001
o A|b = 00001101
o A^b = 00001100
 | (bitwise OR) does OR on every bit of 2 numbers, the result is 1 if at least 1 bit is 1
 ^ (bitwise XOR) does XOR, returns 1 if only 1 of the 2 bits is 1
 << (left shift) – take 2 numbers, left shifts the bits of the first operand, the second operand
decides the number of places to shift, ex: b=9; b<<1 => b = 18
o Ex b = 9 (00001001)
o B<<1 => b = 00010010 = 18
 >> right shift
o B = 9 (00001001)
o B>>1 => b = 00000100 = 4
 ~ (bitwise NOT) – inverts all bits in a number
o Ex B = 9 = 00001001 => ~B = 11110110

!!!!!!!!!

- Check a bit using AND & masking


- Set bit to 1 using OR & masking
- Toggle/flip a/multiple bit using XOR & masking
- Toggle all bits using NOT
- Clear a digit to 0 using OR and NOT (flip the bit, use OR, flip the bit back)

10 EXCEPTIONS

Throw -> literally any variable

Try{code}

Catch(<variable type> e){code}

Catch(…){code} //DEFAULT EXCEPTION


THREADS: <thread>

 Thread x (functionname, arg1, arg2, etc)


 X.join();
 Thread-a series of instructions that together they fulfil a specific task
 Preemptive multithreading – using a schedule to switch between threads
 Threads have Priorities, schedule boosts priorities of threads that don’t get executed

SEMAPHORES – signaling approach

It’s all about lock and signal

It’s an integer, binary or counting semaphore

Multiple treads can access a finite instance of resources

Semaphore value can be updated by any process

Signal/wait are used to change the value of the semaphore

It waits until the resource count increases

https://www.geeksforgeeks.org/semaphores-in-process-synchronization/

binary -> 0-1, wait&do

counting -> queue, push&pop

MUTEX – mutual excluxion, lock based approach

It’s all about locking, put a lock on the mutex object

It’s an object

Allows multiple treads to access a single resource, but not simultaneously

<mutex>

If multiple threads are called on a function (variabile commune), it locks said function until the thing
is complete, then unlocks it, so that each thread can do it one at a time

Waits until the lock is released

Mutex m;

m.lock;

m.unlock;

When many resources are available use semaphore, when only one use mutex
SPINLOCK – lock that causes a thread to wait in a loop (spin) while repeatedly checking if the lock is
available

Mutex puts the thread to sleep (wait state) while waiting for the lock to open, but spinlock makes it
check continuously

TIME COMPLEXITIES:

 O(1) – constant time


 O(log(n)) – logarithmic time (de la 2^n)
o Halving (imparti la 2 pana ajungi la 1), ex BINARY SEARCH
 O(n) – linear time
 O(n*log(n)) - EX Merge sort(log(n) levels, each done linear)
 O(n^2) – ex bubble sort, selection sort, insertion sort
 O(2^n) or O(4^n) or … -> Exponential time, ex subsets, backtracking, recursive
 O(n!) – n factorial -> first calls n times, then for each previous call it calls again n-1 times, etc

OPTIMIZING: decrease the time, increase the space OR increase the time, decrease the space

ANSWER: we can buy memory, we can’t buy time, so better decrease time and increase space than
the other way around

SPACE COMPLEXITY: how does space usage scale as input gets very large

O(n) -> linear space -> an array

?????
Input:

Cin

Cin.getline(x, length); sau cin.getline(x);

Scanf(“%d”, x);

Output:

Printf(“abs %d”, x);

Cout

You might also like