I am resizing an array inside of a method, but since I am using a pointer, the memory address is now incorrect. The diffDataTot pointer is initially created in main(), and then passed into the calculateField() method. The calculateField() method contains new information that needs to be added to diffDataTot, and some old information needs to be removed. I indicated this using the resize() method call.
void calculateField(diffData* diffDataTot){
diffData* filler = (diffData*)malloc(newSize * sizeof(diffData));
filler = resize(newSize, diffDataTot);
free(diffDataTot);
diffDataTot = filler;
free(filler);
}
int main{
diffData* diffDataTot = (diffData*)malloc(sizeof(diffData));
calculateField(diffDataTot);
}
I had to abbreviate this since it is from a full scale simulation that is thousands of lines. However, the issue is that diffDataTot contains the correct information locally inside of the calculateField() method. But the pointer points to a different address in memory than in main(). I could try without using a pointer, but I would like to avoid copying the entire array every time calculateField() is called since it is quite large.
Is there any way for me to pass back the memory of the first element in the array from the calculateField() method?
c++changingvoid calculateField(diffData* diffDataTot){tovoid calculateField(diffData*& diffDataTot){should allow the pointer to be changed in the caller. This is the difference between passing the pointer by value and by reference.