简体   繁体   中英

Can I delete a double* passed to a function as a parameter?

I have written the following function to dynamically allocate memory to a double* array: (I also wrote a function just like this for int* )

void add_memory(double* double_array, int current_idx, int max)
{
  max = max * 2;
  double* temp_double_array = new double[max];
  for (int i = 0; i < current_idx; i++) {
    temp_double_array[i] = double_array[i];
  }
  delete [] double_array;
  double_array = temp_double_array;
}

I call this function like so:

int n_max = 10;
int m_max = 10;
double* val = new double[n_max];
int* col_ind = new int[n_max];
int* row_ptr = new int[m_max];
int n = 0;
int m = 0;

for (int i = 0; i < sz; i++) {
  int first_val_ind = -1;
  for (int j = 0; j < sz; j++) {
    if (B[i][j] == 0) {
      if (n == n_max) {
        add_memory(val, n, n_max);
        add_memory(col_ind, n, n_max);
        n_max = n_max * 2;
      }
      val[n] = B[i][j];
      col_ind[n] = j;
      if (first_val_ind == -1) {
        if (m == m_max) {
          add_memory(row_ptr, m, m_max);
          m_max = m_max * 2;
        }
        first_val_ind = n;
        row_ptr[m] = first_val_ind;
        m++;
      }
      n++;
    }
  }
}

I am getting an error which according to this SO question appears to be happening because I am deleting memory I haven't declared.

My error is: *** glibc detected *** ./mm2: free(): invalid next size (fast): 0x00000000016ae220 ***

Is it because I'm trying to delete a global variable locally?

The problem is you're deleting the same array twice. Consider what happens here:

void add_memory(double* double_array, int current_idx, int max)
{
    // ...
    delete [] double_array;
    double_array = temp_double_array; // <== double_array is local to add_memory
                                      // so this line has no effect
}

You call it with val , val gets deleted. But then on the next iteration of the loop, you call it with val again. But val was already deleted! Sure, you allocate a new array within add_memory , but nothing external to the function has access to it. You need to return it:

double* add_memory(double* double_array, int current_idx, int max)
{
    // ...
    delete [] double_array;
    return temp_double_array;
}

And then you have to overwrite val :

val = add_memory(val, n, n_max);

Note that you still will have to delete [] all your arrays at the end of the program, as right now they're being leaked.

(Token note about using vector instead here).

Passing a reference to a pointer should work:

void add_memory(double*& double_array, int current_idx, int max)
{
  max = max * 2;
  double* temp_double_array = new double[max];
  for (int i = 0; i < current_idx; i++) {
    temp_double_array[i] = double_array[i];
  }
  delete [] double_array;
  double_array = temp_double_array;
}

This won't require you to return anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM