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.1 kB
{ "timestamp": "2025-08-02T16:16:07.656Z", "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) : data(new char[s]), size(s) {}\n \n // Copy constructor\n Buffer(const Buffer& other) : 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 : data(std::exchange(other.data, nullptr)), size(std::exchange(other.size, 0)) {}\n \n // Destructor\n ~Buffer() { delete[] data; }\n};\n```\nIn the copy constructor, we are allocating new memory and copying `other`'s contents into it. In the move constructor, we are transferring ownership of `data` pointer to this object from `other` without any copies (using `std::exchange`). Finally, in destructor, we deallocate the memory that was allocated for `data`.\n", "improvedCode": "class Buffer {\n char* data;\n int size;\npublic:\n // Constructor\n Buffer(int s) : data(new char[s]), size(s) {}\n \n // Copy constructor\n Buffer(const Buffer& other) : 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 : data(std::exchange(other.data, nullptr)), size(std::exchange(other.size, 0)) {}\n \n // Destructor\n ~Buffer() { delete[] data; }\n};", "duration": 6895, "success": true, "qualityMetrics": { "hasCode": true, "hasDestructor": true, "hasStdLibrary": true, "score": 0.5384615384615384, "rawScore": 0.7, "maxScore": 1.3 }, "error": null }