Comprehensive Guide to C++ Memory Management: From Alignment to Leak Prevention

Comprehensive Guide to C++ Memory Management: From Alignment to Leak Prevention

1.1. Memory Alignment: Hidden Costs of Structures Principle Revealed The compiler inserts padding bytes between structure members to improve access efficiency. For example: cpp struct Data { char c; // 1 byte int i; // 4 bytes → Actual usage4 bytes (including3 bytes padding) }; Actual layout:[c][pad][pad][pad][i], total size8 bytes. Optimization Techniques · Use#pragma pack(1) … Read more

C++ Interview Weekly (7): Implementation Principles of unique_ptr and shared_ptr

C++ Interview Weekly (7): Implementation Principles of unique_ptr and shared_ptr

In C++, manual resource management (<span>new/delete</span>) is prone to errors, leading to: •Memory leaks•Double free•Exception safety issues To address these problems, C++11 introduced smart pointers: •<span>std::unique_ptr</span>: Exclusive ownership•<span>std::shared_ptr</span>: Shared ownership, managed through reference counting We will analyze from three dimensions: principles, performance, and application scenarios. 1. Technical Background and Design Intent 1unique_ptr• Goal: Exclusive resource … Read more

Discussing C Programming – Caution with Pointer Function Returns

Discussing C Programming - Caution with Pointer Function Returns

In C language, the return value of pointer functions must be handled with caution. Here are the key considerations: ‌1. Valid Return Types‌ Pointer functions should return a pointer that matches the declared type (such as <span>int*</span>, <span>char*</span>, etc.), and the memory pointed to must be valid: int* func_returning_int_ptr() { /* … */ } char* … Read more

Tencent Engineer Submits Patch to Linux Kernel Introducing Swap Table Architecture

Tencent Engineer Submits Patch to Linux Kernel Introducing Swap Table Architecture

Source: Reprinted with permission from OSC Open Source Community (ID: oschina2013) Recently, Tencent engineer Kairui Song submitted a new patch to the Linux kernel mailing list, proposing the introduction of a new architecture called Swap Table, which integrates swap cache, swap mapping, and swap allocator into a new backend infrastructure. According to reports, the latest … Read more

In-Depth Analysis of C Language: What Happens to Variables in Memory? (Continued)

In-Depth Analysis of C Language: What Happens to Variables in Memory? (Continued)

In the last lesson, we explored the “behind-the-scenes story” of variable definitions in memory, and I believe everyone has gained a preliminary understanding of memory, hard drives, and the binary language of computers. Today, we will continue to delve deeper, uncovering the true mystery behind why an int type variable “occupies four bytes” and the … Read more

Three Parameter Passing Mechanisms in C++: From Underlying Principles to Practical Applications

Three Parameter Passing Mechanisms in C++: From Underlying Principles to Practical Applications

In C++ programming, function parameter passing seems like a basic operation, yet it hides many key details that affect code performance and safety. Beginners often get confused about why “pass by value cannot modify the original variable,” and even experienced developers may stumble over the choice between “pointers vs references.” The core of this issue … Read more

Flexible Array Members in C99

Flexible Array Members in C99

Actually, the title should omit C++. The flexible array is a feature introduced in C99. Introduction A Flexible Array Member (FAM) is a feature introduced in the C99 standard that allows the last member of a structure to be an array of unspecified size. This design is typically used to implement variable-length data structures, such … Read more

In-Depth Analysis of C Language Variables: What Happens in Memory?

In-Depth Analysis of C Language Variables: What Happens in Memory?

In the last lesson, we briefly introduced the usage of variables, but many students expressed that they still find the question, “What exactly happens in the computer when we define a variable?” a bit “peculiar” and hard to understand. Don’t worry, in this lesson we will delve into this seemingly simple yet very core question—what … Read more

Linux102: Analysis of ramdisk.c in Linux Kernel 0.11

Linux102: Analysis of ramdisk.c in Linux Kernel 0.11

Public Account “Focus on Linux”, dedicated to Linux kernel development The Linux102 series will detail 102 files in version 0.11 of Linux. This article discusses the source code of the 45th file, <span>【Linux102】45-kernel/blk_drv/ramdisk.c</span>. 1. Main Function of ramdisk.c This is a memory virtual disk driver, written by Theodore Ts’o. It means that: If RAMDISK is … Read more

C Language – Dangling Pointers & Wild Pointers – How to Avoid Them?

C Language - Dangling Pointers & Wild Pointers - How to Avoid Them?

The previous article C Language – The Lifecycle of Variables discussed the lifecycle of dynamic memory and mentioned dangling pointers. So, what is a dangling pointer? Wild pointers and dangling pointers are like “time bombs” in C/C++ programs, and they must be strictly avoided through good programming practices and modern tools (such as smart pointers). … Read more