myVector push_in() Just Not Working

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        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 = new int[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.
Last edited on
what will happen if you call push_in twice?
My guess is that you aren't allocating tempVector properly. Or maybe add_on is breaking everything.
what will happen if you call push_in twice?


Worse:

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 = new int[itsLength];

            for (int i = 0; i < itsLength; i++)
                tempVector[i] = pVector[i];

            delete pVector;
            itsLength++;
            pVector = new int[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.

EDIT: Solved

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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];

            for (int i = 0; i < itsLength; i++)
                std::cout << "\ntempVector[" << i << "] = " << tempVector[i];

            delete pVector;
            itsLength++;
            int test = itsLength;
            pVector = new int[itsLength];

            for (int i = 0, j = 0; i < test; i++)
            {
                if (i == location)
                {
                    std::cout << "\n" << newElement << " just got added on\n";
                    pVector[i] = newElement;
                }
                else
                {
                    std::cout << "\nj: " << j << "\t" << tempVector[j] << " just got added on\n";
                    pVector[i] = tempVector[j];
                    j++;
                }
            }

            delete tempVector;
        }


add_on was modifying tempVector's elements :S
Last edited on
1
2
3
4
void push_in(/* */){
  //....
  delete tempVector;
}
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.
Last edited on
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 = new int[itsLength]; to the beginning requiring a delete for consistency .

you can avoid the copy loop with tempVector=pVector


I got a bunch of newElements and garbage.
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 = new int[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 []
}



Topic archived. No new replies allowed.