lundi 29 juin 2015

Pushing element to dynamically allocated pointer array int C++

[EDIT 1] I'll preface by saying that for this project, I am required to "Create a container class" where I can push, pop, and retrieve elements from a list. I am supposed to use pointers, and must write the functions for pushing, popping, etc. [/EDIT 1]

I am having difficulty pushing an element to a dynamically allocated pointer array. I am able to initially create an array just fine. You can see from my comments my thought process for how I think I should be able to add to the array:

1) create new array with room for the one new element;
2) add the new element to index 0;
3) copy the old array into the rest of the new array;
4) delete old array;
5) set the new array as the value of the pointer

I have the following three files:

IntegerList.h:

/** 
*IntegerList.h
*/

#ifndef IntegerList_H
#define IntegerList_H

class IntegerList
{
private:
    int * pArray;
    int length;

public:
    IntegerList(); // default constructor

    void createArray(int howlong);
    int getValue(int index);
    void deleteArray();

    void pushFront(int element);
};

#endif

IntegerList.cpp:

/**
* IntegerList.cpp
*/

#include "IntegerList.h"
#include <iostream>

using namespace std;

// IntegerList constructor:
IntegerList::IntegerList()
{
    pArray = 0;
    length = 0;
}


// creates an array of length howlong (determined by main.cpp); sets the values
// to equal ten times the index number. For example, if an array of size 4 is
// to be created, then an array with the following values will be created by
// this method: 0, 10, 20, 30. Sets length equal to howlong.
void IntegerList::createArray(int howlong)
{
    length = howlong;
    pArray = new int[length];
    for (int i = 0; i < length; i ++)
        pArray[i] = (i*10);
}


int IntegerList::getValue(int index)
{
    return pArray[index];
}


void IntegerList::deleteArray()
{
    delete[] pArray;
}


// places element at front of array
void IntegerList::pushFront(int element)
{
    // create new array with room for the one new element
    int newArray[length+1]; // nope

    // start by adding the new element
    newArray[0] = element;

    // copy the old array, put it into the new array starting at index 1 (since
    // index 0 is the new element)
    for(int i = 0; i < length; i ++)
    {
        newArray[i+1] = pArray[i];
    }

    // delete old array
    deleteArray();

    // set pArray equal to the new array;
    pArray = newArray;

    // update the value of length
    length += 1;
}

And my main file, main.cpp:

#include "IntegerList.h"
#include <iostream>

using namespace std;

int main()
{
    // create object
    IntegerList myArray;

    // create array of length 5
    myArray.createArray(5);

    // print array
    for (int i = 0; i < 5; i ++)
        cout << "Element " << i << ". " << myArray.getValue(i) << endl;

    // everything works ok so far

    // push the number 99 to front
    myArray.pushFront(99);

    // print array
    for (int i = 0; i < 6; i ++)
        cout << "Element " << i << ". " << myArray.getValue(i) << endl;

    myArray.deleteArray();
}

The first printArray() shows that everything is going as planned. However, after I try to push 99 to the front, things get screwed up :(

Here is the output I'm getting:

Element 0. 0
Element 1. 10
Element 2. 20
Element 3. 30
Element 4. 40

Element 0. 99
Element 1. 0
Element 2. 2130567168
Element 3. 4486648
Element 4. 2686508
Element 5. 4201772

Note that in the second printout, the first two elements appear to have the value that I intended for them to have.

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire