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

推荐订阅源

WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
小众软件
小众软件
博客园 - Franky
D
Docker
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
宝玉的分享
宝玉的分享
C
Check Point Blog
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
The Cloudflare Blog
博客园 - 聂微东
博客园_首页
Engineering at Meta
Engineering at Meta

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
Use of Pointers For Lists
Yusuf Aytas · 2008-11-04 · via Yusuf Aytas

Published · 2 min read

C++’ta diziler sabit uzunlukludur; yani program çalışırken bir diziyi doğrudan büyütüp küçültmek mümkün değildir. Bu yüzden, eleman ekleyip silmeyi destekleyen dinamik bir liste yapısı oluşturmak için bellek yönetimini kendimiz yapmamız gerekir. Aşağıdaki örnek, C++’ta new ve delete kullanarak basit bir dinamik dizi tabanlı liste (dynamic array list) yapısının nasıl uygulanacağını gösterir.

Bu yapıda:

  • addItem() → Mevcut diziyi kopyalayıp yeni bir eleman ekleyerek listeyi dinamik olarak büyütür
  • deleteItem() → İstenilen index’teki elemanı silip diziyi küçültür
  • Yapıcı (constructor) → Listeyi boş başlatır (size = 0, ptr = nullptr)
  • Yıkıcı (destructor) → Bellek sızıntısını önlemek için ayrılan belleği serbest bırakır

Bu örnek, C++’ta manuel bellek yönetiminin nasıl yapıldığını ve dinamik veri yapılarının temel mantığını anlamak için oldukça öğreticidir.

#ifndef LIST_H
#define LIST_H

class List {
public:
    List();  // Constructor
    ~List();  // Destructor
    void addItem(int item);  // Add integer item
    void deleteItem(int loc);  // Delete item

private:
    int *ptr = nullptr;
    int size = 0;  // Pointer and its size
};

#endif

Here's the List.cpp

#include "List.h"
#include <stdexcept>

List::List() {
    // The constructor initializes size to 0 and ptr to nullptr
}

void List::addItem(int item) {
    int *newPtr = new int[size + 1];
    for (int i = 0; i < size; i++) {
        newPtr[i] = ptr[i];
    }
    newPtr[size] = item;
    delete[] ptr;
    ptr = newPtr;
    size++;
}

void List::deleteItem(int loc) {
    if (loc < 0 || loc >= size) {
        throw std::out_of_range("Index out of range");
    }

    int *newPtr = new int[size - 1];
    for (int i = 0; i < loc; i++) {
        newPtr[i] = ptr[i];
    }
    for (int i = loc + 1; i < size; i++) {
        newPtr[i - 1] = ptr[i];
    }
    delete[] ptr;
    ptr = newPtr;
    size--;
}

List::~List() {
    delete[] ptr;
}