Professional Documents
Culture Documents
Interview
Interview
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()”)
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
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
strcmp() -- Compare two strings (returns 0 if equal, <0 if s1 < s2, >0 if s1>s2)
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
!!!!!!!!!
10 EXCEPTIONS
Try{code}
https://www.geeksforgeeks.org/semaphores-in-process-synchronization/
It’s an object
<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
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:
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
?????
Input:
Cin
Scanf(“%d”, x);
Output:
Cout