Programming Basics Flashcards
Are references implemented as pointers?
References are generally implemented using pointers.
What is a lock_guard?
What is the syntax to use a lock_guard?
lock_guard is a mutex wrapper which implements a convenient mechanism for owning a mutex for the duratio of the scoped block.
The syntax is:
std::lock_guard lock(g_i_mutex);
Can multiple threads exist within the same process?
Yes
When does a lock_guard try to lock a mutex?
When does a mutex locked by a lock_guard get released?
The lock_guard tries to lock the mutex as soon as it is created and unlocks it when it goes out of scope, automatically.
What is a template?
Templates are special operators that specify that a class or a function is written for one or several generic types that are not yet known.
How much memory does a pointer occupy?
32/64 bits depending on the system
What’s the C library for threads?
p_threads
How do I allocate a pointer to a constant variable?
const int * ptr;
What is the difference in scope between a static and a global variable?
Static variables can be accessed only within their scope, while global variables are visible everywhere
How do I define a template function?
template T max( T a, T b)
{
return( a > b ? a : b );
}
int max_int = max( 2.2, 2.5 ) ;
float max_float = max( 2.2, 2.5 )
What is the difference between a mutex and a semaphore?
A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work.
A semaphore is a generalized mutex. In lieu of single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers. The consumer and producer can work on different buffers at the same time.
How do I define a template class?
template < class T >
class Foo {
private:
T _value;
public: Foo( T value ) : _value(value) { }; } ---------------------------- int main( int argc, char **argv ) { Foo < int > foo_int; Foo < float > foo_float; }
If a class has a static member and I create multiple instances of such class, do these object have each their own copy of the static variable or do they share the same one?
There can not be multiple copies of same static variables for different objects.
Therefore, multiple objects of the same class will share the same static members.
How can I initialize a multidimensional array?
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
// Or even better: int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
1) What is an array?
2) Are elements in an array contiguous in memory?
3) Are elements of an array of the same size?
1) An array is a sequence of elements of the same kind.
2) The elements of an array are contiguous in memory.
3) The elements of an array have all the same size.
Difference between static and dynamically allocated arrays
Static array are allocated at compile time, therefore the size is pre-determined. Dynamically allocated arrays are allocated at runtime in the heap with the new operator, and their size is defined at runtime.
Can static methods access non-static parts of the class?
No, they can only access static members and methods.
What’s the difference between char str[4] and char* str?
The first is an array of char and as such is stored in the stack.
The second is a pointer to char and can be either:
- in the shared segment (read-only) if char *str = “GfG”;
- in the heap if char str;
int size = 4; /one extra for ‘\0’*/
str = (char )malloc(sizeof(char)size);
How can I expand the size of a string using pointers in C?
You need to allocate a new memory area
Can I declare a static method in a class?
Yes, static methods can only access the static part of the class (members and methods).
What is a unique pointer?
A unique_ptr is a container for a raw pointer, which the unique_ptr is said to own. A unique_ptr explicitly prevents copying of its contained pointer (as would happen with normal assignment), but the std::move function can be used to transfer ownership of the contained pointer to another unique_ptr.
Is an exception an object?
Yes
Does each call of a recursive function allocate a new stack frame?
Yes, therefore local variables are redefined and allocated.
Memory allocation, deallocation and reallocation in C
- malloc(): to allocate memory. Example: int* ptr = (int) mallow(10sizeof(int));
- calloc(): allows contiguous allocation of memory. Example: ptr = (cast-type*)calloc(n, element-size);
- free(): to de-allocate memory. Example: free(ptr)
- realloc(): to dynamically change the memory allocation of a previously allocated memory (e.g., to increase size). Example: ptr = realloc(ptr, new_size);
Can I define a pointer to a struct?
Yes, of course.
struct movies_t {
string title;
int year;
};
movies_t amovie;
movies_t * pmovie;
What does it mean throwing an exception?
Throwing an exception means creating a new exception object containing information about the error, and providing it to the function that called the code where the error happened.
What operator should I use to access a member of a reference to a class?
If you have a reference to an object, can use the standard dot operator class.method
What is an automatic variable? Where is it stored?
auto is a modifier which specifies an “automatic” variable that is automatically created when in scope and destroyed when out of scope.
What is the time complexity for accessing an element of an array?
O(1)
Can I define a reference without initializing it?
No
Casting in C++
- const_cast: adds/remove const from a variable
- static_cast: done at compile time, does things like conversion between types
- dynamic_cast: can be used only with pointers and references to objects to convert between relative classes (e.g., from derived to base class)
- reinterpret_cast: It is used to convert one pointer of another pointer of any type
How do I allocate a constant pointer to a variable?
int *const ptr;
What is the asterisk operator? What is the ampersand operator?
Asterisk: unary operator that applied to a pointer, returns the content of the pointed variable
Ampersand: unary operator that returns the address where a variable is stored.
How do I access the members of a struct?
Dot operator if you have the struct itself of a reference to it, arrow operator if you have a pointer to the struct.
Does a reference occupy memory?
No
When should I use call by value and when call by reference?
If you want to change the object passed, call by reference or use a pointer; e.g., void f(type& x); or void f(type* x);.
If you don’t want to change the object passed and it is big, call by const reference; e.g., void f(const type& x);.
Otherwise, call by value; e.g. void f(type x);.
1) What is a multidimensional array?
2) How can a multidimensional array be stored in memory?
Multidimensional arrays are 2D arrays, for example to memorize matrices (but in general are n-D).
In a 2D array, each element, we have an array of arrays: there is a first array, in which each element is an array itself.
Each element of the first array represents a row in the matrix. Each element in an element of the second array represents the column.
What is a semaphore?
What is the syntax to create a semaphore?
What is the count of a semaphore?
A semaphore has a predefined maximum count, and a current count. You take ownership of a semaphore with a wait operation, also referred to as decrementing the semaphore. You release ownership with a signal operation, also referred to as incrementing the semaphore.
What happens if I call n times a function in which a static variable is defined?
The static variable is allocated only the first time, any other time the function is called, the same memory is used (e.g., if a static variable is created and incremented in a function and this is called n times, the same variable is increased n times).
What is the time complexity for adding an element in an array?
- Linear time at the beginning and in the middle
- Constant time at the end
What is an extern variable? Where is it stored?
extern is used when a file needs to access a variable in another file that it may not have #included directly. It is stored anywhere the actual variable is stored when created.
How can I wait for a thread to finish before continuing?
Using the join function.
If a class is static and goes out of scope, is its destructor invoked? When is it invoked?
Nope, it stays in memory and the destructor is invoked at the end of the main.
Can a pointer be NULL? Can a reference be NULL?
A pointer can be NULL. A reference cannot be NULL.
What is a shared pointer?
A shared_ptr is a container for a raw pointer. It maintains reference counting ownership of its contained pointer in cooperation with all copies of the shared_ptr. An object referenced by the contained raw pointer will be destroyed when and only when all copies of the shared_ptr have been destroyed.
Can I declare a static class object? How?
Just like variables, objects also when declared as static have a scope till the lifetime of program.
- What is a mutex?
- How do I create a mutex?
- What does it mean that a thread own a mutex?
The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.
- std::mutex mtx;
- A thread owns a mutex when it locks it, meaning that it is the only one having access to it.
Are the global variables defined in a process available to its threads?
Yes, since threads share the data segment.
What is a pointer? How do I define a pointer?
A pointer is a variable that holds memory address of another variable.
int* ptr;
What is recursion? How can I implement recursion?
Recursion tries to decompose a bigger problem into smaller sub-problems. The result of these sub-problems compose the solution to the original problem.
A function is said to be recursive if it calls itself.
A recursive function can only solve directly the so-called base case. If it is asked to solve a non-base case, it has to call itself with simplified input. At each call, the input data are simplified until a base case is found. Once this is found, the calls are closed, computing intermediate results.
How can I handle strings in C++?
C++ has in its definition a way to represent sequence of characters as an object of class. This class is called std:: string. String class stores the characters as a sequence of bytes with a functionality of allowing access to single byte character.
How do I allocate a constant pointer to a constant variable?
const int *const ptr;
Where is a standard static array allocated?
It depends, can be in the stack frame or the data segment (if static).
What’s the lifetime of a static variable?
Entire program.