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

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
有赞技术团队
有赞技术团队
美团技术团队
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
J
Java Code Geeks
Martin Fowler
Martin Fowler
I
InfoQ
小众软件
小众软件
MongoDB | Blog
MongoDB | 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
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;
}