Skip to content

C++ Types and Resource Management: Practice Problems

Intuition

Learning through practice: Practice problems are like training drills, they help you apply knowledge and identify areas that need more study.

Why it matters: Regular practice builds confidence and reveals patterns in how concepts are tested. Each problem reinforces key programming concepts.

The key insight: Making mistakes during practice is valuable, each error points to a concept that needs clarification.

C++ Types and Resource Management — Practice Problems

12 practice problems covering fundamental C++ concepts: types, RAII, ownership, move semantics, smart pointers, and undefined behavior.


Basic Types and References

Ownership and RAII

Smart Pointers

Move Semantics and Undefined Behavior


TopicSiteLink
Resource ManagementProgrammingResource Management
TemplatesProgrammingTemplates & Metaprogramming
Rust OwnershipLanguagesRust Ownership
C++ ConcurrencyProgrammingConcurrency

Common Mistakes

Assuming sizeof covers dynamic allocations: sizeof returns the size of the type at compile time, not the size of heap-allocated memory. sizeof(int*) is 8 bytes on a 64-bit system regardless of how many ints the pointer references. For dynamic arrays, you must track the size separately, sizeof cannot tell you.

Using unique_ptr when shared ownership is needed: std::unique_ptr enforces exclusive ownership and cannot be copied. If two parts of your code need to own the same resource, std::unique_ptr will cause compilation errors or forced std::move transfers that leave the original null. Use std::shared_ptr for shared ownership or redesign ownership boundaries.

Ignoring alignment when casting between pointer types: Casting a char* to an int* may violate alignment requirements. On some architectures, accessing an int at an unaligned address causes a hardware fault. Use std::aligned_storage, alignasor memcpy for type-punning instead of raw pointer casts.

See Also