{"id":26903448,"url":"https://github.com/maximilianfeldthusen/dynamic-array-with-raii","last_synced_at":"2025-07-20T06:05:45.996Z","repository":{"id":284880056,"uuid":"956361878","full_name":"maximilianfeldthusen/dynamic-array-with-RAII","owner":"maximilianfeldthusen","description":"The code defines a DynamicArray class that serves as a simple dynamic array implementation.","archived":false,"fork":false,"pushed_at":"2025-04-24T16:26:58.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"TFD","last_synced_at":"2025-04-24T17:36:56.087Z","etag":null,"topics":["array","cpp","memory-management","raii"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximilianfeldthusen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-28T05:59:14.000Z","updated_at":"2025-04-24T16:27:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"37915eb1-e432-4921-8825-bb03a29ca8e1","html_url":"https://github.com/maximilianfeldthusen/dynamic-array-with-RAII","commit_stats":null,"previous_names":["maximilianfeldthusen/dynamic-array-with-raii"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/maximilianfeldthusen/dynamic-array-with-RAII","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Fdynamic-array-with-RAII","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Fdynamic-array-with-RAII/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Fdynamic-array-with-RAII/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Fdynamic-array-with-RAII/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximilianfeldthusen","download_url":"https://codeload.github.com/maximilianfeldthusen/dynamic-array-with-RAII/tar.gz/refs/heads/TFD","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Fdynamic-array-with-RAII/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266076093,"owners_count":23872730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","cpp","memory-management","raii"],"created_at":"2025-04-01T10:28:27.965Z","updated_at":"2025-07-20T06:05:45.988Z","avatar_url":"https://github.com/maximilianfeldthusen.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## Documentation\n\n### dynamic-array-with-RAII\n\nThis C++ code defines a `DynamicArray` class that serves as a simple dynamic array implementation. It makes use of RAII (Resource Acquisition Is Initialization) principles and modern C++ features like `std::unique_ptr` and `std::span`. Let's break down the code step by step.\n\n### Includes and Namespace\n```cpp\n#include \u003ciostream\u003e\n#include \u003cmemory\u003e\n#include \u003cspan\u003e\n#include \u003calgorithm\u003e\n```\n- `#include \u003ciostream\u003e`: For standard input and output operations.\n- `#include \u003cmemory\u003e`: For smart pointers, specifically `std::unique_ptr`.\n- `#include \u003cspan\u003e`: For `std::span`, which provides a view over a contiguous sequence of elements.\n- `#include \u003calgorithm\u003e`: For the `std::fill` algorithm.\n\n### DynamicArray Class\n```cpp\ntemplate\u003ctypename T\u003e\nclass DynamicArray {\npublic:\n    // Constructor to initialize the array with a given size\n    explicit DynamicArray(std::size_t size) \n        : size_(size), data_(std::make_unique\u003cT[]\u003e(size)) {}\n```\n- The class template `DynamicArray` is defined with a type parameter `T`.\n- The constructor takes a `size` argument and initializes the `size_` member variable. It also allocates a dynamic array of type `T` using `std::make_unique`, which ensures that memory is managed automatically.\n\n#### Deleted Copy Operations\n```cpp\n    DynamicArray(const DynamicArray\u0026) = delete;\n    DynamicArray\u0026 operator=(const DynamicArray\u0026) = delete;\n```\n- The copy constructor and copy assignment operator are deleted to prevent copying of the `DynamicArray` instances. This is important because it manages a unique resource (the dynamic array).\n\n#### Move Constructor and Move Assignment Operator\n```cpp\n    DynamicArray(DynamicArray\u0026\u0026 other) noexcept \n        : size_(other.size_), data_(std::move(other.data_)) {\n        other.size_ = 0; // Leave the moved-from object in a valid state\n    }\n\n    DynamicArray\u0026 operator=(DynamicArray\u0026\u0026 other) noexcept {\n        if (this != \u0026other) {\n            size_ = other.size_;\n            data_ = std::move(other.data_);\n            other.size_ = 0; // Leave the moved-from object in a valid state\n        }\n        return *this;\n    }\n```\n- The move constructor transfers ownership of the dynamic array from `other` to the new instance, setting `other.size_` to 0 to maintain a valid state.\n- The move assignment operator does the same but also checks for self-assignment.\n\n#### Access Operators\n```cpp\n    T\u0026 operator[](std::size_t index) {\n        return data_[index];\n    }\n\n    const T\u0026 operator[](std::size_t index) const {\n        return data_[index];\n    }\n```\n- These operators allow access to the array elements using the subscript operator (`[]`). The first version is for non-const objects, while the second is for const objects.\n\n#### Size Method\n```cpp\n    std::size_t size() const {\n        return size_;\n    }\n```\n- Returns the size of the dynamic array.\n\n#### Fill Method\n```cpp\n    void fill(const T\u0026 value) {\n        std::fill(data_.get(), data_.get() + size_, value);\n    }\n```\n- Fills the array with a specified value using `std::fill`.\n\n#### Print Method\n```cpp\n    void print() const {\n        for (std::size_t i = 0; i \u003c size_; ++i) {\n            std::cout \u003c\u003c data_[i] \u003c\u003c ' ';\n        }\n        std::cout \u003c\u003c '\\n';\n    }\n```\n- Prints the contents of the array to the standard output.\n\n#### Data Method\n```cpp\n    T* data() {\n        return data_.get();\n    }\n\n    const T* data() const {\n        return data_.get();\n    }\n```\n- Provides access to the raw data pointer of the array, both in mutable and immutable forms.\n\n### Main Function\n```cpp\nint main() {\n    DynamicArray\u003cint\u003e arr(10); // Create a dynamic array of integers\n    arr.fill(42); // Fill the array with the value 42\n    arr.print(); // Print the contents of the array\n\n    // Using std::span for safe access\n    std::span\u003cint\u003e span(arr.data(), arr.size());\n    std::cout \u003c\u003c \"Using std::span:\\n\";\n    for (const auto\u0026 val : span) {\n        std::cout \u003c\u003c val \u003c\u003c ' '; // Print values using std::span\n    }\n    std::cout \u003c\u003c '\\n';\n\n    return 0;\n}\n```\n- In the `main` function, a `DynamicArray` of integers is created with a size of 10.\n- The array is filled with the value 42 and printed.\n- A `std::span` is created from the dynamic array's data, allowing for safe iteration over the contents of the array. The values are printed again using the span.\n\n### Summary\nThe `DynamicArray` class provides a flexible and safe way to manage a dynamic array in C++, leveraging smart pointers to ensure proper memory management while avoiding issues related to copy operations. The use of `std::span` offers a modern way to handle array-like data.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximilianfeldthusen%2Fdynamic-array-with-raii","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximilianfeldthusen%2Fdynamic-array-with-raii","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximilianfeldthusen%2Fdynamic-array-with-raii/lists"}