0

This is a sample code of a bigger program. The modifyArray function increases the array size by one then adds a new string element which here is "eeee", calling the function once works fine but when calling it the second time it modifies the original array, i want it to modify the array that came out the first modifyArray function. here is the code

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <filesystem>
#include <iomanip>
#include <string.h>
#include <sstream>

using namespace std;

void modifyArray(string*&, int);

int main()
{
    int arraySize = 4;
    string* playlist;
    playlist = new string[arraySize];
    playlist[0] = "aaaa";
    playlist[1] = "bbbb";
    playlist[2] = "cccc";
    playlist[3] = "dddd";
    
    modifyArray(playlist, arraySize);
        modifyArray(playlist, arraySize);
    for (int i = 0; i < arraySize; i++)
    {
        cout << playlist[i] << endl;
    }
}

void modifyArray(string*& Fplaylist, int FarraySize)
{
    string* subPlaylist;
    subPlaylist = new string[FarraySize];
    copy(Fplaylist, Fplaylist + FarraySize, subPlaylist);

    delete[]Fplaylist;
    FarraySize = FarraySize + 1;
    Fplaylist = new string[FarraySize];
    copy(subPlaylist, subPlaylist + FarraySize - 1, Fplaylist);
    Fplaylist[FarraySize - 1] = "eeee";
    

}

expected output:

aaaa
bbbb
cccc
dddd
eeee
eeee

Real output:

aaaa
bbbb
cccc
dddd

ps: I can't use vectors

I tried other things like storing the array in an external text file but I get the same results. I am a beginner at C++ so I really don't understand pointers quite well.

  • 2
    *ps: I can't use vectors* -- The `std::vector` has been officially part of C++ for 24 years now. When will this silly restriction of not using vector be put in the dustbin of history.? – PaulMcKenzie Dec 23 '22 at 14:26
  • Ditch the stupid manual memory management as step 0 towards fixing this. – Jesper Juhl Dec 23 '22 at 14:30
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Dec 23 '22 at 14:31
  • 2
    `I can't use vectors`...FIre your teacher. – Casey Dec 23 '22 at 14:32
  • Compare both parameters to the function. Keep staring at them until you see the difference. There's something radically different between them. Therein lies the explanation for your bug. – Sam Varshavchik Dec 23 '22 at 14:34
  • Your function modifies the array and returns it. Does it do the same thing for the array size? – john Dec 23 '22 at 14:41
  • I took your code above and ran it. It doesn't produce either the 'real output' or the expected output, so something doesn't add up here. – john Dec 23 '22 at 14:44
  • Do you understand that `FarraySize = FarraySize + 1;` means *nothing* to the *caller* of `modifyArray`? `arraySize` in `main` never changes, which you'll see if you run your code in a **debugger**. Therefore, neither your "expected" nor "real" output can possibly be the result of this posted code. – WhozCraig Dec 23 '22 at 14:48
  • @SamVarshavchik I am sorry I forgot to remove the third parameter when I was copying it from the source code into the sample one here. The issue still exists. – Ahmed Salah Dec 23 '22 at 14:49
  • @WhozCraig then how can i change FarraySize in the function? – Ahmed Salah Dec 23 '22 at 14:51
  • The same way you changed the value of that pointer; pass it by reference. That's the stark difference Sam was eluding to. And unless you don't want a failing grade, I suggest you also fix your memory leaks. Best of luck. – WhozCraig Dec 23 '22 at 14:53
  • @WhozCraig It worked! Thank you, I am sorry this is the first time asking on here. – Ahmed Salah Dec 23 '22 at 15:01

1 Answers1

2

There are two problems with the modifyArray function.

The first problem is that FarraySize should be a reference so that changes to it affect the variable used to call the function (exactly like Fplaylist).

The second problem is that the function makes two allocations and two copies, when it should be one of each. Making two allocations leaks memory, and make two copies is needlessly inefficient.

Here's a version that works

void modifyArray(string*& Fplaylist, int& FarraySize)
{
    string* newPlaylist = new string[FarraySize + 1];
    copy(Fplaylist, Fplaylist + FarraySize, newPlaylist);
    newPlaylist[FarraySize++] = "eeee";
    delete[] Fplaylist;
    Fplaylist = newPlaylist;
}
john
  • 85,011
  • 4
  • 57
  • 81