2

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?

1
  • If this is c++ changing void calculateField(diffData* diffDataTot){ to void 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. Commented Jul 3, 2019 at 20:29

1 Answer 1

3

You need to pass the address of your pointer (pointer on pointer).

void
resizing_func(int **elements,
              int *elem_count)
{
  //---- get rid of additional indirection ----
  int *p=*elements;
  int count=*elem_count;
  //---- work with local variables ----
  // ...
  int new_count=2*count; // whatever is needed for the problem
  p=realloc(p, new_count*sizeof(int));
  // ...
  //---- make changes visible in calling context ----
  *elements=p;
  *elem_count=new_count;
}

void
testing_func(void)
{
  int count=100;
  int *elements=malloc(count*sizeof(int));
  // populate elements
  resizing_func(&elements, &count);
  // use updated elements and count
}

Depending on the problem, realloc() can be replaced by:

  • allocate a second buffer,
  • work with first and second buffer,
  • free the first buffer and continue with the second,

as in the OP's question.
(by the way, notice that free(filler) will free what was intended to be kept in diffDataTot)

note: as the question is tagged c++, it would have probably been better to use a std::vector and pass it by (non-const) reference; but the provided source code looks like c.

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

1 Comment

Amazing thanks, this is exactly what I needed. The code that I am using is definitely c++, but since I hand wrote it to simplify what I am trying to do maybe I changed some syntax

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.