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

推荐订阅源

V
Vulnerabilities – Threatpost
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
D
DataBreaches.Net
博客园_首页
罗磊的独立博客
B
Blog
T
Threat Research - Cisco Blogs
C
Cisco Blogs
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
G
GRAHAM CLULEY
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
SecWiki News
SecWiki News
T
Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
Google Online Security Blog
Google Online Security Blog
T
Tor Project blog
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
H
Hacker News: Front Page
V
V2EX
Security Latest
Security Latest
Cloudbric
Cloudbric
Simon Willison's Weblog
Simon Willison's Weblog
Attack and Defense Labs
Attack and Defense Labs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
NISL@THU
NISL@THU
S
Secure Thoughts
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
V2EX - 技术
V2EX - 技术
Vercel News
Vercel News
P
Palo Alto Networks Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
Google DeepMind News
Google DeepMind News
Webroot Blog
Webroot Blog

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 Prisoner's Dilemma Climbing No More The Weekly Win Mevlana Candy Brewing Turkish Tea Onboarding Your Engineering Manager Technical Deep Dives Yapay Zekâ Çağında Bilgisayar Mühendisliği Building Remote Teams From Idea to Launch in 2 Weeks Reflecting on Software Engineering Handbook Representing the Business New Manager Survival Guide Take Self Reviews Seriously Chasing Real Respect The Invisible Difference Learning the Johari Window Management is a Lonely Place Simple Task Management AI Balance in Work PIP Manager Insights Engineering Manager Interview Preparation Work-Life Balance as a Manager Bridging the Management Disconnect Tech Hiring Bubble Bursts Traits for EMs Simple Acts of Recognition Matter The Question I Ask Every New Report The Reality of an Employer's Market Bridging Ideals and Reality Hiring Red Flags Why The Godfather Is So Damn Good Subteam Tenets No Fluff Please Losing a Top Performer Balancing Act of Reliability Building Trust in Engineering Teams Ideal Number of Direct Reports Overriding a People Leader’s Decision From Misperception to Promotion Perception vs Perspective Setting Goals From Engineer to Manager Getting Delegation Right Interviewing Your Future Boss Celebrating Our Book in Iceland Operational Skills Needed On Writing Software Engineering Handbook Charlie Munger Quotes Working with Dependencies From Las Vegas to Canyons Navigating Layoffs Handling Competitive Dynamics A Weekend Getaway to Malta Engineering Health Essentials Should Dev Managers Code? Confronting the Life on Pause Winning Eleven Kindness is A Choice Bireysel Katılımcılar ve Yöneticiler Leading from Where You Are The Subtle Art of Listening Coding in Leadership The Power of Consistency The Making of a Leader The Path to Leadership Embracing TikTok Talent Sourcing Journey Leading Self Managing Teams Cracking Coding Bottlenecks
A Basic Pointer Example
Yusuf Aytas · 2008-10-13 · via Yusuf Aytas

Published · 2 min read

C++’ta pointer kavramı, dilin düşük seviyeli kontrol anlayışının temel parçasıdır. Burada artık yalnızca değerlerle değil, bu değerlerin bellekte nerede durduğu ile ilgilenilir. Pointer bir değeri değil, o değerin adresini taşır. Bu ayrım küçük görünür ama fonksiyon çağrılarında kimin neye dokunabildiğini belirler.

Bir değeri fonksiyona gönderdiğinde kopya gönderilmiş olur. Bir pointer gönderildiğinde ise erişim gönderilir. Yani fonksiyon, çağıranın değişkenine doğrudan ulaşabilecek konumdadır. Bu da C++’taki en net prensiplerden birini doğurur: Erişim verdiğin anda sorumluluk da vermiş olursun.

Bu örnekte şunlar yapılır:

bir integer değişken tanımlanır,
bu değişkenin adresi bir pointer içinde tutulur,
pointer dereference edilerek değer okunur,
aynı adres bir fonksiyona parametre olarak gönderilir,
fonksiyon bu adres üzerinden hesap yapar.

Aşağıdaki kod, pointer kullanımının en temel hâlini gösterir.

#include <iostream>

int squareValue(int* value);

int main() {
    int number = 5;
    int* numberPtr = &number;

    std::cout << "number is " << *numberPtr << std::endl;

    int squaredNumber = squareValue(&number);
    std::cout << "The square of number is " << squaredNumber << std::endl;
    std::cout << "The number after squaring is " << number << std::endl;

    return 0;
}

int squareValue(int* value) {
    int squared = (*value) * (*value);
    return squared;
}

Burada &number ifadesi, değişkenin değerini değil adresini gönderir. Fonksiyon tarafında alınan int* value bu adresi temsil eder. *value ise adresin gösterdiği yerdeki değere erişir.

Bu örnekte fonksiyon sadece okur ve sonuç döndürür. Bu yüzden number çağrıdan sonra değişmez. Ama fonksiyon içinde *value = (*value) * (*value); gibi bir atama yapılırsa, çağıran taraftaki değişken doğrudan değişir. Pointer’ın farkı budur. Değil “kare aldım”, “kimin verisine dokundum” meselesidir.

C++’ta bellek ve sahiplik konuları da aynı çizgide ilerler. Adresi paylaştığında, kontrol paylaşırsın. Kontrol paylaşıldığında, hatanın bedeli de paylaşılmaz, doğrudan sana yazar.