Reputation: 73
I have a assignment to create a Arraylist( from 1d arrays) in c++, and I have each method functioning, but i am getting a weird value in my array that I am not adding in. Basically what happens is when I add to my array if the number of items in my array become equal to the length of the array I double the size of the array. Well when my array length gets to length=8, in the arr[6] there is a value of 33. Is this a memory issue? this place should be 0.
here is my cpp:
#include<iostream>
#include<string>
#include<sstream>
#include "ArrayList.h"
using namespace std;
void ArrayList:: intialArr(int arr[])
{
cout << "intialArr ran" << endl;
for(int i = 0; i < length; i++)
{
arr[i] = 0;
}
}
std::string ArrayList:: toString()
{
cout << "{ ";
for(int i = 0; i < capacity; i++)
{
cout << "arr[" << i << "] = " << arr[i] << " ";
}
cout << " }";
}
ArrayList::ArrayList()
{
length = 1;
capacity=0;
arr = new int[length];
cout << "length = " << length << " " << "capacity = " << capacity << endl;
intialArr(arr);
cout << "ArrayList created"<<endl;
}
int& ArrayList:: operator[] (unsigned int i)
{
cout << "operator[] is working" << endl;
int *p = &arr[i];
return *p;
}
void ArrayList:: push_back(int m)
{
if(capacity>=length)
{
length = length*2;
cout << "length = " << length << endl;
int* curArr = new int[length];
intialArr(curArr);
for (int i = 0; i<length; i++)
{
if(arr[i])
{
cout << "arr[" << i << "] in pushback method(if) is: " << arr[i]<< endl;
curArr[i] = arr[i];
}
else
{
cout << "arr[" << i << "] in pushback method(else) is: " << arr[i] << endl;
curArr[i] = 0;
}
}
delete [] arr;
arr = curArr;
}
for(int i = 0; i < length; i++)
{
if(arr[i]==0)
{
arr[i] = m;
capacity++;
cout << "arr at " << i << " is: " << arr[i];
cout << " and should be: " << m << endl;
cout << "capacity is: " << capacity << endl;
break;
}
}
}
void ArrayList:: erase(int m)
{
for(int i = 0; i < length; i++)
{
if(arr[i]==m)
{
for(int j = i; j<length; j++)
{
arr[j] = arr[j+1];
cout<< "arr[" << j << "] is: " << arr[j] << " and should be: ";
cout << arr[j+1] << endl;
}
capacity--;
cout << "capacity is: " << capacity << endl;
}
}
if((length/capacity) == 2)
{
length = length/2;
cout << "length = " << length << endl;
int* curArr = new int[length];
intialArr(curArr);
for (int i = 0; i<length; i++)
{
if(arr[i])
{
cout << "arr[" << i << "] in pushback method(if) is: " << arr[i]<< endl;
curArr[i] = arr[i];
}
else
{
cout << "arr[" << i << "] in pushback method(else) is: " << arr[i] << endl;
curArr[i] = 0;
}
}
delete [] arr;
arr = curArr;
}
}
int main()
{
ArrayList arr;
arr.push_back(1);
arr.push_back(1);
arr.push_back(1);
arr.push_back(1);
arr.push_back(1);
arr.push_back(1);
}
and here is the results of my array:
arr[0] in pushback method(if) is: 1
arr[1] in pushback method(if) is: 1
arr[2] in pushback method(if) is: 1
arr[3] in pushback method(if) is: 1
arr[4] in pushback method(if) is: 1
arr[5] in pushback method(if) is: 1
arr[6] in pushback method(if) is: 33
arr[7] in pushback method(else) is: 0
and no vectors are allowed and when I try to hardcode arr[6]=0 I get a very odd error.
Upvotes: 0
Views: 137
Reputation: 206577
The following block of code in push_back()
is cause for undefined behavior since you are accessing arr
out of bounds.
for (int i = 0; i<length; i++)
{
if(arr[i])
{
cout << "arr[" << i << "] in pushback method(if) is: " << arr[i]<< endl;
curArr[i] = arr[i];
}
else
{
cout << "arr[" << i << "] in pushback method(else) is: " << arr[i] << endl;
curArr[i] = 0;
}
You need to save the old length, copy the data from arr
to curArr
using the old length, and proceed to add the input argument to the new array.
Also, the check:
if(arr[i]==0)
will be OK as long as you don't push a 0
to your array.
I would revise push_back
as:
void ArrayList:: push_back(int m)
{
if(capacity>=length)
{
int oldLength = length;
length = length*2;
cout << "length = " << length << endl;
int* curArr = new int[length];
intialArr(curArr);
for (int i = 0; i < oldLength; i++)
{
curArr[i] = arr[i];
}
delete [] arr;
arr = curArr;
}
arr[capacity] = m;
capacity++;
}
Upvotes: 2