ReviseAlgo Logo

Modern C++ Features

C++ Core Guidelines

Write clean, safe, and maintainable C++ code by following the official C++ Core Guidelines.

Interview: Ownership rules, using raw pointers only as non-owning observers, and ensuring type and resource safety.

Last Updated: June 13, 2026 9 min read

The C++ Core Guidelines are a set of best practices and design rules curated by Bjarne Stroustrup and Herb Sutter. Following them helps you write type-safe, resource-safe, and maintainable C++ code.

Express Intent

Write self-documenting code. Use specific types and compiler features (like const and override) to express your intent.

RAII Resource Management

Wrap raw resources in manager objects. Never use raw new or delete operators in application code.

Type Safety

Avoid raw pointer casting and union checks. Prefer standard containers and type-safe casts (like static_cast).

The Raw Pointer Rule: Non-Owning Observers Only

One of the most important rules in the guidelines is the ownership rule:

Rule R.3: A raw pointer (T*) is non-owning. Owning pointers must be wrapped in smart pointers (like std::unique_ptr).

This means raw pointers should only be used to inspect objects, never to manage their lifetimes or delete them. This prevents double-free errors and memory leaks.

Code Walkthrough

Contrasts C-style resource management with modern, Core Guidelines-compliant code.

#include <iostream>
#include <memory>
#include <vector>

// BAD: Owning raw pointers can easily lead to memory leaks void processBad() { int* data = new int[10]; // If an exception is thrown here, the memory leaks delete[] data; }

// GOOD: Safe, auto-managed container compliance void processGood() { std::vector<int> data(10, 0); // Resource managed by vector RAII }

// GOOD: Raw pointer used only as a non-owning observer void printData(const int* val) { if (val) std::cout << *val << '\n'; }

int main() { auto val = std::make_unique<int>(42); printData(val.get()); // Pass raw pointer as non-owning observer return 0; }

Interview-Relevant Information

Q: Why do the guidelines discourage raw pointer ownership?
Answer: Raw pointers do not specify who owns the object or who is responsible for deleting it. This can easily lead to double-free errors, memory leaks, or use-after-free bugs. Wrapping owning pointers in smart pointers (like std::unique_ptr) defines clear ownership rules.

Q: What does 'express intent directly in code' mean?
Answer: It means you should use explicit compiler features to make your intent clear. For example, use const to declare read-only variables, use override to mark virtual function overrides, and use [[nodiscard]] to warn developers if they ignore a function's return value.

Quick Checklist

Did you replace raw new and delete calls with RAII wrappers? Are your raw pointers non-owning? If yes, your code follows the Core Guidelines.

Use Cases

Establishing design patterns and code review checklists for enterprise teams.

Configuring static analysis tools (like clang-tidy) to enforce code safety rules.

Common Mistakes

Managing object lifetimes using owning raw pointers, which leads to memory leaks.

Passing large parameters by value instead of const reference, introducing unnecessary copy overhead.