I spent my day today playing around with a vector class I was building, attempting to convert the std::vector functions to mine as a starting point. Insert (or in myVector, push_in()) has just not clicked yet. Here's my code:
void push_in(int location, int newElement) //Still needs work...
{
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
for (int i = 0; i < itsLength; i++)
std::cout << "\ntempVector[" << i << "] = " << tempVector[i];
int test = itsLength+1;
delete pVector;
itsLength = 0;
pVector = newint[itsLength];
for (int i = 0, j = 0; i < test; i++)
{
if (i == location)
add_on(newElement);
else
{
std::cout << "\nj: " << j << "\t" << tempVector[j] << " just got added on\n";
add_on(tempVector[j]);
j++;
}
}
delete tempVector;
}
tempVector[0] = 30
tempVector[1] = 22
tempVector[2] = 12
j: 0 30 just got added on
j: 1 197200 just got added on
j: 2 131097 just got added on
push_in(1, 45) Test: myVec length: 4
30, 45, 197200, 131097,
As you can see, tempVector starts off with the correct values but becomes garbage. What is causing that? Did I modify the array without noticing? (I'm fairly tired) Any ideas are welcome.
EDIT:
Oh, and tempVector and pVector are both initialized in private.
tempVector[0] = 30
tempVector[1] = 45
tempVector[2] = 197200
tempVector[3] = 131097
j: 0 30 just got added on
j: 1 45 just got added on
j: 2 12 just got added on
j: 3 131097 just got added on
push_in(1, 45) Test: myVec length: 5
30, 45, 45, 12, 131097,
My guess is that you aren't allocating tempVector properly.
I think everything's stable but I'll give it another look through.
Or maybe add_on is breaking everything.
I don't think so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void add_on (int newElement) //adds an element to the end
{
tempVector = newint[itsLength];
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
delete pVector;
itsLength++;
pVector = newint[itsLength];
for (int i = 0; i < itsLength; i++)
pVector[i] = tempVector[i];
pVector[itsLength-1] = newElement;
delete tempVector;
}
Thanks for the ideas though, I'll give it another look through with my fingers crossed.
tempVector never changes. I don't see any tempVector = something;
If you push_in twice, you will try to access and later free non reserved memory -> crash
you can avoid the copy loop with tempVector=pVector.
tempVector never changes. I don't see any tempVector = something;
1 2 3 4
void push_in(int location, int newElement) //Adds an element to a specified location
{
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i]; //<--- ??
I also added tempVector = newint[itsLength]; to the beginning requiring a delete for consistency .
you can avoid the copy loop with tempVector=pVector
I mean the direction to which it points. tempVector[i] = pVector[i]; You are accessing memory that was never reserved
1 2 3 4 5 6
if(capacity == size){ //if you need to reallocate
tempVector = pVector; //
pVector = newint[size*2]; //double the size (if you just add one cell, you need a lot of reallocation)
//copy the elements in tempVector to pVector
delete [] tempVector; //If you create with new [] you must destroy with delete []
}