Resize method intArray - c++

I'm writting an IntArray class for college but don't know how to write my resize method efficiently. What I have doesn't support resizing to smaller lists and I don't know how to fix that..
Here is my code:
void IntArray::resize(unsigned int size){
for (int i = size;i<length;i++){
data[i] = 0;
}
length = size;
}
header file
#ifndef INTARRAY_H_
#define INTARRAY_H_
#include <iostream>
using namespace std;
class IntArray{
private:
int length;
int * data;
public:
IntArray(int size = 0);
IntArray(const IntArray& other);
IntArray& operator=(const IntArray& original);
int getSize() const { return length; };
int& operator[](unsigned int i);
void resize(unsigned int size);
void insertBefore(int value, int index);
friend ostream& operator<<(ostream& out, const IntArray& list);
~IntArray(){ delete[] data; };
};

When you need to resize an you are actually going to create a new array, copy the old into the new and then delete the old array.
void IntArray::resize(unsigned int size){
if (size <= length) // if we are making it smaller reset the size and do nothnig
{
my_size = size
return;
}
int * temparr = new int[size];
// copy data
for (unsigned int i = 0; i < length; ++i)
temparr[i] = data[i];
delete [] data; // get rid of the old array
data = temparr; // set data to the new array
length = size; // set the new size
}
You should also have a capacity member that tracks the actual size of the array like a std::vector. That way you can have an array that is bigger than what you need to as it grows there would need to be less re -llocations.

Related

Segmentation fault: 11 in C++ IntVector

I'm attempting to implement an intvector in C++ and am getting a "Segmentation fault: 11" error. I understand this has something to do with memory management, and considering how new I am to C++ it could definitely be a pretty minor mistake. I debugged the code with valgrind and was given messages such as the following:
Use of uninitialized value of size 8, Invalid read of size 4,Conditional jump or move depends on uninitialized value(s).
My best guess is it has something to do with how I'm implementing the arrays. I originally had the arrays stored on the heap but changed it to the stack and still got the same error. I've already implemented an intvector in java, so I was attempting to use similar logic here, which perhaps is part of the issue.
#include <iostream>
#include "IntVector.h"
#include <cmath>
using namespace std;
int num_elements = 0;
int array_size = 0;
int expansion_factor;
void IntVector::expandArray(){
int tempArr[array_size*2];
for(int i =0;i<array_size;i++){
tempArr[i] = array[i];
}
array = tempArr;
array_size = array_size * 2;
}
void IntVector::add(int val){
int tempArr[array_size];
if(array_size == num_elements){
expandArray();
array[num_elements] = val;
}
else{
for(int i = 0;i<array_size;i++){
tempArr[i] = array[i];
}
tempArr[num_elements] = val;
array = tempArr;
}
num_elements++;
}
void IntVector::remove(int index){
}
int IntVector::get(int index) const{
return index;
}
void IntVector::removeLast(){
}
void IntVector::set(int index, int val){
}
std::string IntVector::toString()const {
return "";
}
IntVector::IntVector(int initial_size){
int* array = new int[initial_size];
}
IntVector:: ~IntVector(){
delete[] array;
}
int main(){
IntVector v(0);
v.add(5);
}
#ifndef INTVECTOR_H_
#define INTVECTOR_H_
using std::cout;
class IntVector {
private:
int* array;
int num_elements;
int array_size;
int expansion_factor;
void expandArray();
public:
void add(int val);
void remove(int index);
int get(int index) const;
void removeLast();
void set(int index, int val);
std::string toString() const;
IntVector(int initial_size);
~IntVector();
};
#endif
As mention in the comments, there are definitely some holes in your understanding of C++. Really when dealing with header files you should have a main.cpp, someotherfile.h, someotherfile.cpp. That just best practices to avoid redefinition errors.
There was quite a bit wrong with the way you accessed the private variable. If a class has a private( or even public) variable you don't have to redeclare it each time you want to change its value.
There were one or two major flaws with the way you expanded the vector. If the vector size is initialized to 0 then 0*2 is still 0 so you never actually increased the size. Secondly, when you set the original array = to the new array the new array was just a local array. This means that the memory wasn't actually allocated permanently, once the function ended the temparr was destroyed.
I know this was probably a lot but if you have any question feel free to ask.
main.cpp
#include "IntVector.h"
int main()
{
IntVector v;
IntVector x(10);
v.push(5);
v.push(5);
v.push(5);
v.push(5);
v.push(5);
v.print();
cout << endl;
x.push(5);
x.push(5);
x.push(5);
x.push(5);
x.push(5);
x.print();
return 0;
}
IntVector.h
#include <string>
#include <iostream>
using namespace std;
class IntVector {
private:
int *array;
int num_elements;
int array_size;
//int expansion_factor =; you would only need this if you plan on more than double the vector size
void expandArray(); //normally c++ array double in size each time they expand
public:
//Constructors
IntVector(); //this is a contructor for if nothing is called
IntVector(int initial_size);
//setters
void push(int val); //add
void pop(); //removelast
void remove(int index); //remove
void at(int index, int val); //set
//Getters
int at(int index);
//std::string toString(); I'm changing this to print
void print(); //will print the contents to the terminal
//Deconstructor
~IntVector();
};
IntVector.cpp
#include "IntVector.h"
//constructors
IntVector::IntVector() //no arguments given
{
array = new int[0];
num_elements = 0;
array_size = 0;
}
IntVector::IntVector(int initial_size)
{
array = new int[initial_size];
num_elements = 0;
array_size = initial_size;
}
void IntVector::expandArray()
{
int *tempArr;
if(array_size == 0){
array_size = 1;
tempArr = new int[1];
} else {
//make sure to allocate new memory
//you were creating a local array which was destroy after the function was completed
//using new will allow the array to exist outside the function
tempArr = new int[array_size * 2];
}
for (int i = 0; i < array_size; i++)
{
tempArr[i] = array[i];
}
//make sure to delete the old array otherwise there is a memory leak.
//c++ doesn't have a garbage collector
delete[] array;
array = tempArr;
array_size = array_size * 2;
}
void IntVector::push(int val)
{
num_elements++;
//checking if vector needs to increase
if (array_size <= num_elements)
{
expandArray();
array[num_elements-1] = val;
}
else
{
array[num_elements-1] = val;
}
}
void IntVector::remove(int index)
{
//not sure how to implment this becuase each element has to be a number.
}
int IntVector::at(int index)
{
return array[index];
}
void IntVector::pop()
{
num_elements = num_elements-1; //not really removing it from the "vector" but it won't print out again
}
void IntVector::at(int index, int val)
{
array[index] = val;
}
void IntVector::print()
{
for (int i = 0 ; i < num_elements; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
IntVector::~IntVector()
{
delete[] array;
}
output
5 5 5 5 5
5 5 5 5 5
Hopefully, the comments help. I changed the name of the functions to better match the actual vecter class the already exists in C++. I think it's good to pick apart already defined functions like this because you get a better understanding of how they actually work and not just how to use them.
If you got any questions just leave a comment

C++ deprecated conversion from string constant to 'char*' []

Whenever I try to run my code I get these error lines. It also seems like that sometimes it crashes the program. Is that possible or something else causes the crash?
The error code comes to these lines:
Buffer<char*> s(20,"s.txt","rw"); //20 long char*
for(unsigned int i=0;i<24;i++){ //it overwrites the size of s after default 20
s[i]="hey"; //error here: "deprecated conversion from string constant to 'char*'"
The class looks like this(deleted the unimportant parts":
template<class T>
class Buffer:public File_ptr
{
unsigned int siz;
T *data;
public:
///constructor(size,filename,openmode)
Buffer(unsigned int s,const char* n, const char* m):File_ptr(n,m),siz(s)
{
data= new T[siz];
/* for(unsigned int i=0; i<siz; ++i)
{
data[i]=0;
}; */
};
///destructor
~Buffer()
{
delete[] data;
}
///operator[]
T& operator[](unsigned int i)
{
if(i>siz-1)
{
unsigned int newsize=siz*2;
T* tmp=new T[newsize];
for(unsigned int j = 0; j < siz; j++)
{
tmp[j] = data[j];
}
siz=newsize;
delete[] data;
data=tmp;
};
return data[i];
};
Any idea why I get the error?
Thanks in advance!
"hey" is a string literal and so the compiler stores it in a different part of the executable which is likely to be stored in write-protected memory along with all other strings. As a result, in C++, in a pointer context it evaluates to const char*.
The solution is to declare
Buffer<const char*> s(20,"s.txt","rw");
template<class T>
class Buffer
{
unsigned int siz;
T *data;
public:
///constructor(size,filename,openmode)
Buffer(unsigned int s,const char* n, const char* m):siz(s)
{
data= new T[siz];
}
///destructor
~Buffer()
{
delete[] data;
}
///operator[]
T& operator[](unsigned int i)
{
if(i>siz-1)
{
unsigned int newsize=siz*2;
T* tmp=new T[newsize];
for(unsigned int j = 0; j < siz; j++)
{
tmp[j] = data[j];
}
siz=newsize;
delete[] data;
data=tmp;
}
return data[i];
}
};
int main() {
Buffer<char*> s(20,"s.txt","rw"); //20 long char*
for(unsigned int i=0;i<24;i++){ //it overwrites the size of s after default 20
s[i]="hey";
}
}
Live demo: http://ideone.com/Dgdcui
You're attempting to store a string literal in a char* variable - indicating that it is writable.
A string literal will typically be stored in the read only data segment of your application binary (See this question)
From the C++ FAQ
...it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified
Attempting to write to a memory location in the read-only data segment will cause your program to terminate - hence the crash.

Program fails when I call a method from constant reference

So, I have a token class:
Token.h
class Token {
std::string name; // token name
int frequency;//frequency
Vector lines;//lines where the token is present
public:
//explanations for the methods in the Token.cpp
Token(std::string tokenname, int linenumber);
virtual ~Token();
const Vector getLines() const;
};
#endif /* TOKEN_H_ */
Token cpp
Token::Token(string tokenname, int linenumber) {
// TODO Auto-generated constructor stub
name = tokenname;
frequency=1;
lines.push_back(linenumber);
}
Token::~Token() {
// TODO Auto-generated destructor stub
}
std::string Token::getName() const{
return name;
}
int Token::getFrequency() const{
return frequency;
}
const Vector Token::getLines() const{
const Vector vec = lines;
return lines;
}
The program fails, when I pass it to the insert method of list class
class List {
private:
class Node {
public:
Token data;
Node* next;
Node(const Token &dataItem, Node* nextptr);
~Node();
};
Node* first;
int length;
public:
List();
virtual ~List();
void insert(const Token &t);
};
List.cpp:
List::Node::Node(const Token &dataItem, Node* nextptr): data(dataItem), next(nextptr){
}
List::Node::~Node(){
cout<<"dead"<<endl;
}
List::List() {
// TODO Auto-generated constructor stub
length = 0;
first = nullptr;
}
List::~List() {
// TODO Auto-generated destructor stub
Node* temp = first;
Node* newtmp;
while(temp->next != nullptr){
newtmp = temp->next;
delete temp;
temp = newtmp;
}
}
const int List::size(){
return length;
}
void List::insert (const Token &t){
Vector dammit = t.getLines();
}
I found out which line in the insert does it(Vector dammit = t.getLines()), so I leave it like that.
It gives me this error message:
double free or corruption (fasttop): 0x0000000000c34040 ***
And here something from main file if you want to run:
int main() {
// cout<<"tokens are here"<<endl;
//
Token hit("aca", 1);
Token hit2("ui", 2);
Token hit1("111", 3);
List list;
list.insert(hit);
list.insert(hit2);
list.insert(hit1);
}
Vector class:
class Vector {
int* store;
int capacity;
int next_index;
public:
Vector();
Vector(int initial_size);
Vector(const Vector &v);
virtual ~Vector();
void push_back(int item);
int pop_back();
const int size() const;
void resize();
void operator =(const Vector &v);
int& operator[] (int k);
const int& operator[] (int k) const;
friend std::ostream& operator<<(std::ostream& os, const Vector& v);
};
Vector::Vector() {
// TODO Auto-generated constructor stub
store = new int [1];
capacity = 1;
next_index = 0;
}
Vector::Vector(int initial_size){
store = new int [initial_size];
capacity = initial_size;
next_index = 0;
}
Vector::Vector(const Vector &v){
store = v.store;
capacity = v.capacity;
next_index = v.next_index;
}
Vector::~Vector() {
// TODO Auto-generated destructor stub
delete[] store;
}
void Vector::resize(){
std::cout<<"in resize"<<std::endl;
std::cout<<capacity<<std::endl;
int length = capacity;
capacity+=100;
int* tempArray;
tempArray = new int[capacity];
for (int i=0; i<length; i++){
tempArray[i] = store[i];
}
if (length>1)
delete[] store;
std::cout<<"finish re4size"<<std::endl;
store = tempArray;
}
void Vector::push_back(int item){
if(next_index >= capacity)
this->resize();
store[next_index] =item;
next_index++;
}
int Vector::pop_back(){
next_index = next_index-1;
int last = store[next_index];
return last;
}
void Vector::operator =(const Vector &v){
//delete[] store;
store = v.store;
capacity = v.capacity;
next_index = v.next_index;
}
const int Vector::size() const{
return next_index-1;
}
int& Vector::operator[] (int k){
//assert((k<next_index)&(k>=0));
return store[k];
}
const int& Vector::operator[] (int k) const{
//assert((k<next_index)&(k>=0));
return store[k];
}
ostream& operator<<(ostream& os, const Vector& v)
{
for(int i=0; i<=v.size(); i++){
os << v[i]<< ' ';
}
return os;
}
In
Vector::Vector(const Vector &v){
store = v.store;
capacity = v.capacity;
next_index = v.next_index;
}
You now have two vectors pointing to the same int* store;
In
void Vector::operator =(const Vector &v){
//delete[] store;
store = v.store;
capacity = v.capacity;
next_index = v.next_index;
}
You do the same thing.
when you call
const Vector Token::getLines() const{
const Vector vec = lines;
return lines;
}
vec = lines uses the copy constructor. You now have vec and lines pointing to the same store.
You return a copy of lines, this will trigger the copy constructor again. A third object now points to store.
When the stack unrolls, locally defined vec is destroyed. ~Vector deletes store. You now have two objects pointing to the same de-allocated store.
Kaboom! as soon as you try to do much of anything else with either of those Vectors. Looks like the destruction of the returned Vector hits first and causes the destructor to re-delete store.
You need to allocate storage for a new store and then copy the contents of source store into the new store in the = operator and the copy constructor.
Vector::Vector(const Vector &v){
capacity = v.capacity;
store=new int[capacity];
for (size_t index; index < capacity; index++)
{
store[index] = v.store[index];
}
next_index = v.next_index;
}
and
Vector & Vector::operator =(const Vector &v){
delete[] store;
capacity = v.capacity;
store=new int[capacity];
for (size_t index; index < capacity; index++)
{
store[index] = v.store[index];
}
next_index = v.next_index;
}
std::copy can be used in place of the for loop in C++11. Hoary old memcpy can also be used, but only because store is a primitive data type.
And while I'm editing, thanks Jarod42, one more little tweak:
const Vector & Token::getLines() const{ //note the return of a reference. This avoids
// making a copy of lines unless the caller really
// wants a copy.
// const Vector vec = lines; don't need to do this. lines is const-ified by the
// const on the return type of the function
return lines;
}
This error has nothing to do with calling a method with constant reference, but rather the function getLines(). For example, should you take hit1 and call the function getLines() directly, it will crash nonetheless. The issue is with how Token has a stack-allocated attribute Vector, which in turn has an int * attribute. This isn't necessarily an issue, but depending on how you implement those classes it can cause memory conflicts.
If you want to keep using your getLine() and can't use the <vector> libraries, you could change your Token's lines attribute to a Vector * and change all other syntax accordingly. Also remember to initialize your pointer lines memory or else it will crash.
However, I'd prefer to use as less dynamic-allocated memory as possible, if unnecessary. And like another user said, before while(temp->next != nullptr) you should have a condition if(temp != nullptr )

Segmentation fault when creating a row-major array

I'm trying to implement a row-major array, which is basically a single dimension representation of a 2D array.
This is my class definition
class RMA{
public:
RMA(){
size_=0;
row_=0;
column_=0;
arr_ = new double[size_];
}
RMA(int n, int m){
size_ = n*m;
column_ = m;
row_ = n;
if(size_== 0) arr_ = 0;
else arr_ = new double[size_];
}
RMA(const RMA& arr) {
size_ = arr.size_;
if(this != &arr){
delete [] arr_;
arr_ = new double[size_];
for(int i=0; i<size_; i++){
arr_[i] = arr.arr_[i];
}
}
return *this;
}
const double& operator() (int n, int m) const{
return arr_[n*column_+m];
}
double& operator()(int n, int m){
return arr_[n*column_+m];
}
~RMA(){delete[] arr_ ;}
private:
int size_;
int column_;
int row_;
double* arr_;
}
I've a calling function which creates the array.
RMA create_array() {
RMA arr;
arr = RMA(N, M);
std::cout<<"success";
return arr;
}
And this is my client
int main(int argc, char* argv[]) {
RMA arr = create_array();
return 0;
}
I end up getting segmentation fault. What am I doing wrong.
You use operations, that instead of cloning array, take a shallow copy of an object, and when destructors are used, they try to release the same memory block.
Implement the following operations:
RMA::RMA(const RMA&); // copy constructor - clone buffer
RMA& operator=(const &RMA); // assignment - clone buffer, release old
Also instead of:
RMA rma;
rma = RMA(a,b);
Use:
RMA rma = RMA(a,b) or RMA rma(a,b);
Edit: constructor code:
RMA::RMA(const RMA &rma) : size_(0), row_(0), column_(0), buffer_(0)
{
*this = rma;
}
RMA &operator=(const RMA&rma)
{
double *old = buffer_;
size_ = rma.size_;
row_ = rma.row_;
column_ = rma.column_;
buffer_ = new double[size_];
memcpy(buffer_, rma.buffer_, sizeof(buffer_[0]) * size_);
delete []old;
return *this;
}
The best solution is to get rid of all the new/delete, copy-constructors, and fluff. Use a private member variable to manage the memory, and follow the Rule of Zero. Like this:
struct RMA
{
RMA(size_t r = 0, size_t c = 0)
: row(r), column(c), arr(r * c) {}
const double& operator() (int n, int m) const
{ return arr[n * column + m]; }
double& operator() (int n, int m)
{ return arr[n * column + m]; }
private:
std::vector<double> arr;
size_t row, column;
};
That's it. You should not write any copy-constructor, assignment operator, move whatever, because the default-generated ones already do the right thing.
NB. row is actually redundant in my example too, you could remove it and calculate it when needed as arr.size() / column.
You could use .at( ) instead of [ ] on vector in order to throw an exception for out-of-bounds access, instead of causing undefined behaviour.

Class of dynamically resizable C++ string array

I need to create a class called DynamicArray based on the given main.cpp file. However I cannot make it work. The program crashes. I believe it should be the constructor problem. Please see the following codes with comments:
main.ccp
#include <iostream>
#include <dynamicarray.h>
using namespace std;
int main()
{
DynamicArray* arr=new DynamicArray(ARRAY_SIZE);
for (int i=0; i<25; i++) {
arr->add("test");
}
for (int j=0; j<10; j++){
arr->remove();
}
delete [] arr;
arr=NULL;
return 0;
}
dynamicarray.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#define ARRAY_SIZE 5
#include <iostream>
using namespace std;
class DynamicArray
{
DynamicArray* darr;
int del;
int occSize;
int totSize;
public:
DynamicArray();
~DynamicArray();
DynamicArray(int);
void add(string);
void remove();
int totalSize();
int occupiedSize();
DynamicArray operator= (string);
DynamicArray operator= (int);
};
#endif // DYNAMICARRAY_H
dynamicarray.cpp
#include "dynamicarray.h"
DynamicArray::DynamicArray()
{
darr=new DynamicArray[ARRAY_SIZE];
del=0;
occSize=0;
totSize=ARRAY_SIZE;
}
DynamicArray::DynamicArray(int n){
darr=new DynamicArray[n];
del=0;
occSize=0;
totSize=ARRAY_SIZE;
}
DynamicArray::~DynamicArray()
{
delete [] darr;
darr=NULL;
}
//adding s to the next available slot in the array. When the array is full,
//add() will grow the array by another ARRAY_SIZE
void DynamicArray::add(string s){
if (occSize<totSize){
darr[occSize]=s;
occSize++;
}else{
totSize=totSize+ARRAY_SIZE;
DynamicArray* temp=new DynamicArray[totSize];
for (int i=0; i<occSize; i++){
temp[i]=darr[i];
}
darr=temp;
delete [] temp;
temp=NULL;
darr[occSize]=s;
occSize++;
}
}
//remove the element where the index is poining to in the array.
//when there are more than ARRAY_SIZE empty slots left, the delete()
//will shrink the array by another ARRAY_SIZE
void DynamicArray::remove(){
darr[del]=NULL;
del++;
occSize--;
if (del>5){
DynamicArray* temp=new DynamicArray[totSize-ARRAY_SIZE];
int j=del;
for (int i=0; i<occSize; i++){
temp[i]=darr[del];
j++;
}
darr=temp;
del=0;
delete [] temp;
temp=NULL;
}
}
//returning the current max capacity size of the array
int DynamicArray::totalSize(){
return totSize;
}
//returning the number of occupied slots in the array
int DynamicArray::occupiedSize(){
return occSize;
}
DynamicArray DynamicArray::operator= (string s){
*this=s;
return *this;
}
DynamicArray DynamicArray::operator= (int n){
*this=n;
return *this;
}
Of course it crashes, as in the constructor of DynamicArray you create new instances of DynamicArray which calls the constructor which in turn creates new instances of DynamicArray... This infinite recursion will lead to a stack overflow which crashes the program.
If the DynamicArray should by a dynamic array (or vector) of strings, then it should contain an "array" of strings, either std::string objects or of pointers. Like
class DynamicArray
{
private:
char** darr; // The actual array of data
...
};
DynamicArray::DynamicArray(int n)
{
darr = new char*[n];
...
}