0

I'm trying to expand and add a new object to a array inside a function and have that array be effected outside the function as well (the arrays pointer is sent as a parameter).

void addMedia(Media* medias[], int &nrOfMedias, string title, int publYear, string author, int nrOfPages)
{
    Media** tempArray = new Media*[nrOfMedias +1];
    for(int i = 0; i < nrOfMedias; i++)
    {
        tempArray[i] = medias[i];
    }
    delete [] medias;
    medias = tempArray;
    delete [] tempArray;
    medias[nrOfMedias] = new Book(title, publYear, author, nrOfPages);
    nrOfMedias++;
}

This code works great inside the function but when I get outside it the array is still empty. As i understand this it's because the pointer is changed inside the function but how can i expand the array without having it change the pointer?

(I can not change the return data type or the parameters, assignment requirements.)

5
  • 1
    Use vectors, and pass them as references. Much, much simpler/safer. (BTW, the second delete in there looks really fishy.) Commented May 14, 2012 at 17:42
  • I can not change the return data type or the parameters Then it's impossible to do what you want. Commented May 14, 2012 at 17:43
  • Yeah, I had to delete my answer that said use vectors because I saw this requirement. Commented May 14, 2012 at 17:46
  • How is your array defined and allocated outside the function? Commented May 14, 2012 at 17:51
  • Media** medias = NULL; medias = new Media*[0]; Commented May 14, 2012 at 17:57

3 Answers 3

2

Do change medias = tempArray; to *medias = tempArray;, make it compile, polish your memory management (consider, what really should be freed, what not).

Sign up to request clarification or add additional context in comments.

4 Comments

did you mean? *medias = *tempArray; Got it to work with this code and removed delete [] medias; thx.
No, I mean *medias = tempArray, exactly as Rollie answer.
that give me an error. IntelliSense: a value of type "Media **" cannot be assigned to an entity of type "Media *"
That's what I meant by "make it compile" :-). See Rollie answer.
2

Don't view medias as an array of pointers, view it as a pointer to an array. Working example (slightly simplified):

class Media
{   
public:
    Media () { m_strTitle = "unknown";}
    string m_strTitle;
};

class Book : public Media
{
public:
    Book(string strTitle) { m_strTitle = strTitle; }
};

void addMedia(Media* medias[], int &nrOfMedias)
{
    Media * tempArray = new Media[nrOfMedias +1];
    for(int i = 0; i < nrOfMedias; i++)
    {
        tempArray[i] = (*medias)[i];
    }
    delete [] *medias;
    (*medias) = tempArray;

    (*medias)[nrOfMedias] = Book("newTitle");
    nrOfMedias++;
}

int main()
{
    int numMedia = 10;
    Media * myArray = new Media[numMedia];
    addMedia(&myArray, numMedia);
    for (int i = 0; i < numMedia; i++)
    {
        cout << i << ") " << myArray[i].m_strTitle << endl;
    }

    return 0;
}

2 Comments

Ok thx the code seems to work fine for one problem. I should probably have mention this earlier but Media is a abstract class so it doesn't take well to first line of code in addMedia.
If that is the case, and you can't change the function signature, I believe it is impossible to alter the passed in medias array.
0

You don't need delete [] tempArray; because tempArray actually points to the same memory block as medias does after medias = tempArray;

Your function will work well whithout that line but I assume that you know what you pass with Media* medias[]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.