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

推荐订阅源

博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Vercel News
Vercel News
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
Jina AI
Jina AI
B
Blog
Recorded Future
Recorded Future
MyScale Blog
MyScale Blog
I
InfoQ
aimingoo的专栏
aimingoo的专栏
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
爱范儿
爱范儿
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Schneier on Security
Schneier on Security
V
V2EX
TaoSecurity Blog
TaoSecurity Blog
H
Hacker News: Front Page
Cloudbric
Cloudbric
D
DataBreaches.Net
B
Blog RSS Feed
P
Palo Alto Networks Blog
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
I
Intezer
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
K
Kaspersky official blog
Forbes - Security
Forbes - Security

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
Heap-Allocated 2D Structures in C++
Yusuf Aytas · 2008-10-15 · via Yusuf Aytas

Published · 2 min read

C++ öğrenirken en çok zorlanılan konulardan biri, işin mutfağı olan manuel bellek yönetimi ve pointer yapılarıdır. Bugün çoğu modern dil bizim için bellekle ilgilense de, C++ hâlâ düşük seviye kontrolün ne anlama geldiğini öğretmek için altın standarttır. Özellikle çift pointer (int**) kullanarak 2D dinamik dizi oluşturmak, bilgisayar biliminde veri yapılarının bellek üzerinde gerçekte nasıl tutulduğunu anlamamızı sağlar.

Bu örnekte:

  • stack üzerinde değil, heap üzerinde dinamik bir 2D dizi oluşturuyoruz,
  • her satır için ayrı bellek alanı ayırıyoruz,
  • elemanları dolduruyoruz,
  • bir fonksiyon aracılığıyla diziyi işliyoruz,
  • ve en önemlisi, ayırdığımız belleği doğru şekilde serbest bırakıyoruz.

Bu, C++’ta bellek yönetimini anlamanın temel taşıdır:
Belleği sen ayırırsın, sen doldurursun, sen temizlersin.

Aşağıdaki kod, pointer mantığını, dinamik bellek tahsisini ve 2D veri yapılarının düşük seviyede nasıl işlendiğini gösteren sade ama çok öğretici bir örnektir.

#include <iostream>

using namespace std;

void printElements(int **haha, int size1, int size2);

int main() {
    // Initialize the array sizes
    const int size1 = 4, size2 = 3;

    // Define a pointer to a pointer (for a 2D array)
    int **ptr;

    // Initialize the 2D array
    ptr = new int*[size1];
    for (int i = 0; i < size1; ++i) {
        ptr[i] = new int[size2];
    }

    // Assign values to the 2D array and print them
    for (int i = 0; i < size1; ++i) {
        for (int j = 0; j < size2; ++j) {
            ptr[i][j] = i + j;  // Example assignment
        }
    }

    // Print the elements using the function
    printElements(ptr, size1, size2);

    // Deallocate memory
    for (int i = 0; i < size1; ++i) {
        delete[] ptr[i];
    }
    delete[] ptr;

    return 0;
}

void printElements(int **haha, int size1, int size2) {
    cout << "Elements in the array:" << endl;
    for (int i = 0; i < size1; ++i) {
        for (int j = 0; j < size2; ++j) {
            cout << haha[i][j] << " ";
        }
        cout << endl;
    }
}