Problem with my C++ code with pointer, array and memory leak. 1.The file impleme
ID: 3726713 • Letter: P
Question
Problem with my C++ code with pointer, array and memory leak.
1.The file implements sorted array of singned char. Assume char has range [-128,127].
2. copy constructor and assignment operater provides deep-copy funtionality. Function insert()'s takes a signed char insert it in the array and make sure array is sorted in the ascending order when done.
----------Code below----.hpp file-----------------------------
// File: a2.hpp
#ifndef A2_HPP
#define A2_HPP
#include <algorithm>
class sorted_sc_array {
public:
sorted_sc_array() : size_(0), ptr_(nullptr) { }
~sorted_sc_array() { delete[] ptr_; }
// IMPLEMENT ME - copy constructor (deep copy)
sorted_sc_array(const sorted_sc_array& A){
size_ = A.size_;
ptr_ = new signed char[size_];
for(int i=0; i<size_; i++){
ptr_[i] = A.ptr_[i];
}//copy into ptr
}
// IMPLEMENT ME
sorted_sc_array& operator=(const sorted_sc_array& A){
size_ = A.size_;
ptr_ = new signed char[size_];
for(int i = 0; i < size_; ++i)
{
ptr_[i] = A.ptr_[i];
}//copy into ptr
return *this;
}
// RETURNS SIZE OF THE ARRAY (i.e. HOW MANY ELEMENTS IT STORES)
int size() const { return size_; }
// RETURNS RAW POINTER TO THE ACTUAL DATA, CAN BE INVOKED AT ANY TIME
const signed char* data() const { return ptr_; }
// IMPLEMENT ME: AFTER INSERT COMPLETES THE ARRAY MUST BE IN ASCENDING ORDER
void insert(signed char c){
//add new element to array, keep them ascending order
//temp char array
signed char* newPtr = new signed char[size_+1];
//compare c and input it to correct posistion
int i ;
for (i = 0; i < size_; ++i)
{
/* code */
if(ptr_[i]>c)
newPtr[i]=c;
else
newPtr[i]=ptr_[i];
}
if(i==size_)
newPtr[size_]=c;
else{
for (int j = i; j < size_; ++j)
{
/* code */
newPtr[j+1]=ptr_[j];
}
}
delete [] ptr_;
size_ = size_+1;
ptr_ = new signed char[size_];
delete [] newPtr;
}
private:
int size_; // size of the array
signed char* ptr_; // pointer to the array
}; // class sorted_sc_array
#endif // A2_HPP
/-------------------following is the code given to test my code------------------------
/***
* File: a2.cpp
// DO NOT EDIT THIS FILE !!!
// YOUR CODE MUST BE CONTAINED IN a2.hpp ONLY
#include <iostream>
#include "a2.hpp"
int main(int argc, char* argv[]) {
sorted_sc_array A;
{
sorted_sc_array T;
for (signed char c = -128; c < 127; ++c) T.insert(c);
T = T;
sorted_sc_array V = T;
A = V;
}
const auto first = A.data();
const auto last = first + A.size();
auto size = A.size();
bool res = std::is_sorted(first, last);
if (!res || (A.size() != 255)) std::cout << "fail";
else std::cout << "pass";
std::cout << std::endl;
return 0;
} // main
The code works. I got an output 'pass' when excuting the code, which is what I'm suppose to see. The problems is that I have a memory leak in my code.
Please debug/fix my code and attach a Vargrind report at the end. Please show me if you know an easier way to do this problem (meaning reduce the excution time). I know you can use sort and copy functions but not sure how to implement.
Explanation / Answer
For me it is failing . but i still saparated function to reduce exicution time.
// File: a2.hpp
#ifndef A2_HPP
#define A2_HPP
#include <algorithm>
class sorted_sc_array {
public:
sorted_sc_array() : size_(0), ptr_(nullptr) { }
~sorted_sc_array() { delete[] ptr_;}
// IMPLEMENT ME - copy constructor (deep copy)
sorted_sc_array(const sorted_sc_array& A){
size_ = A.size_;
ptr_ = new signed char[size_];
for(int i=0; i<size_; i++){
ptr_[i] = A.ptr_[i];
}//copy into ptr
}
// IMPLEMENT ME
sorted_sc_array& operator=(const sorted_sc_array& A){
size_ = A.size_;
ptr_ = new signed char[size_];
for(int i = 0; i < size_; ++i)
{
ptr_[i] = A.ptr_[i];
}//copy into ptr
return *this;
}
// RETURNS SIZE OF THE ARRAY (i.e. HOW MANY ELEMENTS IT STORES)
int size() const { return size_; }
// RETURNS RAW POINTER TO THE ACTUAL DATA, CAN BE INVOKED AT ANY TIME
const signed char* data() const { return ptr_; }
//SORT FUNCTION
int sort(signed char* newPtr, signed char c)
{
//add new element to array, keep them ascending order
//compare c and input it to correct posistion
int i ;
for (i = 0; i < size_; ++i)
{
/* code */
if(ptr_[i]>c)
newPtr[i]=c;
else
newPtr[i]=ptr_[i];
}
}
void copy(signed char* newPtr, signed char c,int i)
{
if(i==size_)
newPtr[size_]=c;
else{
for (int j = i; j < size_; ++j)
{
/* code */
newPtr[j+1]=ptr_[j];
}
}
}
// IMPLEMENT ME: AFTER INSERT COMPLETES THE ARRAY MUST BE IN ASCENDING ORDER
void insert(signed char c){
//temp char array
signed char* newPtr = new signed char[size_+1];
int i = 0 ;
TART=$(date +%s)
# do something
# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here
END=$(date +%s)
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds
copy(newPtr, c, i);
delete [] ptr_;
size_ = size_+1;
ptr_ = new signed char[size_];
delete [] newPtr;
}
private:
int size_; // size of the array
signed char* ptr_; // pointer to the array
}; // class sorted_sc_array
#endif // A2_HPP
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.