UNPKG

codebridge-ai

Version:

Complete fullstack AI coding platform with AST-based code integration and local LLM support. Now with comprehensive web technology support (HTML/CSS/JS) plus JavaScript, Python, Rust, and C++.

21 lines 2.58 kB
{ "timestamp": "2025-08-02T14:34:33.877Z", "model": "deepseek-coder:6.7b", "language": "cpp", "scenario": "memory-safety", "task": "Add destructor, copy constructor, and move semantics", "originalCode": "class Buffer {\n char* data;\n int size;\npublic:\n Buffer(int s) {\n data = new char[s];\n size = s;\n }\n};", "rawResponse": "Here is your updated class with added destructor, copy constructor, and move constructor:\n\n```cpp\nclass Buffer {\n char* data;\n int size;\npublic:\n // Constructor\n Buffer(int s) \n : data(new char[s]), size(s) {}\n \n // Copy Constructor\n Buffer(const Buffer& other) \n : data(new char[other.size]), size(other.size) {\n std::copy(other.data, other.data + other.size, data);\n }\n\n // Move constructor\n Buffer(Buffer&& other) noexcept \n : data(std::exchange(other.data, nullptr)), size(std::exchange(other.size, 0)) {}\n \n // Destructor\n ~Buffer() {\n delete[] data;\n }\n};\n```\nIn this code:\n- The constructor initializes the `data` member with a new array of characters of size `s` and sets the `size` to `s`.\n- The copy constructor creates a deep copy of the other object's `data` by allocating a new array of the same size as the other object, then copying over each element from the other object's data into this newly allocated data.\n- The move constructor takes ownership of the other object's resources (its `data` and `size`) by swapping them with nullptrs using `std::exchange`. This is a common way to implement move semantics in modern C++.\n- The destructor deletes the dynamically allocated memory for `data` when an instance of this class goes out of scope or gets destroyed.\n", "improvedCode": "class Buffer {\n char* data;\n int size;\npublic:\n // Constructor\n Buffer(int s) \n : data(new char[s]), size(s) {}\n \n // Copy Constructor\n Buffer(const Buffer& other) \n : data(new char[other.size]), size(other.size) {\n std::copy(other.data, other.data + other.size, data);\n }\n\n // Move constructor\n Buffer(Buffer&& other) noexcept \n : data(std::exchange(other.data, nullptr)), size(std::exchange(other.size, 0)) {}\n \n // Destructor\n ~Buffer() {\n delete[] data;\n }\n};", "duration": 9126, "success": true, "qualityMetrics": { "hasCode": true, "hasDestructor": true, "hasStdLibrary": true, "score": 0.5384615384615384, "rawScore": 0.7, "maxScore": 1.3 }, "error": null }