c++ Memory related job interview questions involving memory related
For the resize function for containers like vector. This will return a bad_alloc there is no available memory left to resize for the container. Resize will copy the original vector and reclaims the space. It will also add the elements on the back. The memory will be allocated in another spot (if available), and the contents of the vector are copied into this space. This memory allocation is based on two strategies of best fit or first fit.
Stack vesus heap memory allocation:
For stack, local variables through functions are declared here unless they are static or register type. Function parameters are copied here as well. Note that local variables are not automatically initialized so garbage values will be stored here as well. All local variables are automatically destroyed as the function goes out of scope. They are then recreated for each time the function is called.
Declared variables (??) are stored on the heap before the execution of the program takes place. These are initialized to zero while they are not automatically destroyed when the function end (or goes out of scope) but you will not have access to them. Global, static, and register variables are created on the heap as well. Memory allocated by new, malloc, and calloc are created on the heap too.
There are other types of memory including unitialized including static variables before the execution of the program. They are also initialized as zero as well. This is also known as Block Started Symbol (BSS).
Some trickier questions to be asked:
Can class size and object size be the same?
If the class hsa static members, the static members are not stored in the object themselves. This is not the case if the class has not been instantiated
Why should malloc never be directly used to dynamically allocate instances of a class?
Malloc never allocates memory for objectsize * and it does not initialize the class (i.e. invoke the constructor or destructor) where new allocates the memory of objectsize * and implicitly invokes the constructor (the default would be considered if the class constructor does not exist). This is to initialize the class.
Difference between malloc/free and new/delete?
Malloc will allocate memory for the object on the heap, but it will not the object’s constructor to initialize the object. new allocates memory and also invokes constructor to initialize the object.
malloc() and free() do not support object semantics
Does not construct and destruct objects
string * ptr = (string *)(malloc (sizeof(string)))
Are not safe
Does not calculate the size of the objects that it construct
Returns a pointer to void
int *p = (int *) (malloc(sizeof(int)));
int *p = new int;
Are not extensible
new and delete can be overloaded in a class
“delete” first calls the object’s termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty :-
Int_t *my_ints = new Int_t;