[C++] update array or vector from inside a function

Hi, I’ve tried to do this for a long time, but I’ve got almost no success trying to not crash the engine everytime.

How to properly set inside a cpp pawn an array or vector that can be updated or changed from inside a function?

For example:

//from pawn.h
int array[1000]; //or int array = new int[1000];
int* arrayPtr;

//from pawn.cpp
int* arrayPtr = array;

function APawn::DoSomething(int *pointerToArray) //or int *&pointerToArray
{
    delete[] pointerToArray; pointerToArray = new int[1000]; //if dynamic array

    for(int i=0;i<1000;i++){   
              pointerToArray[0] = i;                    
     }

    return pointerToArray;

}

I’ve tried a lot of times and return in various combinations crashes or weird values ( I believe memory address)

I’ve tried also vector the compiler says that is not anymore supported

I’ve tried TArray but crash the engine as well.

The examples that I’ve shown are perfectly compiled “stand alone” with a simple new win32 console vc++ project.

How to update an array (or show me a simple way to store integers) in c++ in UE4 way?

Personally I’d prefer to use the unreal specific classes, e.g. TArray, unless you have a specific reason for c++ arrays.
Should work this way:

// MyPawn.h
void UpdateArray(TArray& Array);

// MyPawn.cpp
void AMyPawn::UpdateArray(TArray<int32>& Array) {
    // do whatever you want to with Array
}

...
    // Usage in case of a local TArray variable
    // The array can also be stored as the class member, it does not matter in this case
    TArray<int32> MyIntsArray;
    UpdateArray(MyIntsArray);
...

Thank you very much :slight_smile: !