惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Recent Announcements
Recent Announcements
Vercel News
Vercel News
M
MIT News - Artificial intelligence
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
G
Google Developers Blog
罗磊的独立博客
爱范儿
爱范儿
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Pointer Şeması
Yusuf Aytas · 2008-10-30 · via Yusuf Aytas

Published · 2 min read

C++’ta pointer şeması, bir nesnenin bellekte nerede yaşadığını, hangi pointer’ın ona sahip olduğunu ve ne zaman serbest bırakılması gerektiğini anlamanın temel yoludur. Düşük seviyeli dillerde bellek yönetimi manuel olduğu için, yanlış yapılan her işlem memory leak (bellek sızıntısı), dangling pointer (boşta kalmış pointer) veya use-after-free gibi hatalara yol açabilir. Bellek sızıntısı, heap’te ayrılan bir bölgenin hiçbir pointer tarafından işaret edilmemesi ve asla serbest bırakılmaması durumudur. Dangling pointer ise serbest bırakılmış bir bölgeyi hâlâ göstermeye devam eden pointer’dır. Bu yüzden C++’ta “kim tahsis etti, kim serbest bırakacak, kaç pointer aynı nesneyi gösteriyor?” soruları sahiplik şemasının kritik parçalarıdır.

Aşağıdaki kısa örnek, heap’te nesne oluşturmayı, int** üzerinden sahiplik/alias aktarımını, bilinçli bir bellek sızıntısını, dangling pointer oluşumunu ve nasıl önlenebileceğini tek bir akış içinde gösterir.

#include <iostream>
using namespace std;

// Allocates an integer on the heap and returns its pointer
int* allocate(int value) {
    return new int(value);
}

// Transfers ownership/alias by updating the external pointer
void transfer(int** target, int* source) {
    *target = source;
}

int main() {
    int* a = allocate(50);   // a → [50] on HEAP
    int* b = nullptr;

    transfer(&b, a);         // b now aliases a (b → [50])

    // Intentional memory leak: allocated but never deleted
    int* leak = allocate(999);  // LEAK

    cout << *a << " " << *b << endl;  // both print 50

    delete a;   // frees [50], but both a and b are now dangling pointers
    a = nullptr;
    b = nullptr;  // prevents accidental use-after-free

    return 0;
}