What do I need to do before deleting elements in a vector of pointers to dynamically allocated objects?

Yes Vectors are implemented using template memory allocators that take care of the memory management for you, so they are somewhat special. But as a general rule of thumb, you don’t have to call delete on variables that aren’t declared with the new keyword because of the difference between stack and heap allocation. If stuff … Read more

Swapping pointers in C (char, int)

The first thing you need to understand is that when you pass something to a function, that something is copied to the function’s arguments. Suppose you have the following: void swap1(int a, int b) { int temp = a; a = b; b = temp; assert(a == 17); assert(b == 42); // they’re swapped! } … Read more

How is the result struct of localtime allocated in C?

The pointer returned by localtime (and some other functions) are actually pointers to statically allocated memory. So you do not need to free it, and you should not free it. http://www.cplusplus.com/reference/clibrary/ctime/localtime/ This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content … Read more