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);