pointer deletion, getting segfault?

I have read a lot about pointers and I don't see why this syntax is not doing the right thing. I know I am confused still about what kinds of objects have automatically freed memory and how subroutines work with memory allocation exactly (I come from a FORTRAN background, where this is more hidden).

The attached code dies at the end of the subroutine stat_compare with a segfault whether I delete the pointer res_x_1jy_V_data or not. It's been paired down from a massively larger code so that's why the structure looks odd.

Three files, external header/subroutine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "externVarDeclaration.h"

void stat_compare(double* results, double* info, double* stat) {
   int size;
   int i;

   size = 33;
   double *res_x_1jy_V_data = get_1jy_V();
   cout << " here 1 " << size << endl;
   std::vector< double> x_1jy_V_data(size);
   //x_1jy_V_data = get_1jy_V();
   for (i=0;i<=size;i++) {
     x_1jy_V_data[i] = res_x_1jy_V_data[i];
   }
     
   cout << " here 2 " << size << endl;
   
   delete res_x_1jy_V_data;
      
   //delete [] res_x_1jy_V_data;
      
   
   cout << " here we are at the end" << size << endl;
}



static double *get_1jy_V() {
     static double array[] = {1.98,1.14,1.22,2.85,1.17,1.39,4,1.12,1.99,1.69,1.6,1.32,1.03,2.62,1.46,1.61,1.01,1.53,1.09,2,2.35,1.96,1.42,1.87,1.08,2.62,2.26,1.66,1.23,1.26,2.12,4.77,1.03,1.19};
     return array;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef EXTERN_VAR_DECL_H
#define EXTERN_VAR_DECL_H
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>

using namespace std;

void stat_compare(double* results, double* info, double* stat);

static double *get_1jy_V();

#endif //EXTERN_VAR_DECL_H 



main cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "externVarDeclaration.h"

int main(int argc, char **argv) {

  int i;
  int popsize = 10;
  int ngen = 10;
  int nres = 11;
  int nE = 10;
  int npars = 10;
  int infosize = 10;
  int statsize = 10;
  double *population = new double[popsize*ngen];
  double *results = new double[popsize*nres];

  double *elist = new double[nE];
  double *d_elist = new double[nE];
  double *resp = new double[nE];
  double *parameters = new double[npars];
  
  double *info = new double[popsize*infosize];  
  
  double *stat = new double[statsize];
  
  int *loopnum = new int;
  *loopnum = 0;


  cout << "stats:\n";
  stat_compare(results,info,stat);
  cout << "stats complete!\n";
  
  cout << "calling write-out..." << endl;
  cout << "end simulation..." << endl;
   delete [] population;
  delete [] results;
  delete [] parameters;
  delete [] elist;
  delete [] d_elist;
  delete [] stat;
  delete [] info;
  cout << "deallocated...\n" << endl;
  return(0);
}
1) Only delete things you allocated with new. If you didn't new it, don't delete it.

Did you use new to create 'res_x_1jy_V_data'? No, you got it from get_1jy_V. Therefore you shouldn't be trying to delete it.

2) This much dynamic allocation is wretched. You really should favor container classes. But these don't even look like they need to be dynamically sized, so normal arrays might be fine.

3) Every new should be paired with a delete, and every new[] should be paired with a delete[]. It doesn't look like you're deleting 'loopnum'. Furthermore, why are you even dynamically allocating loopnum? Why not just make it a normal int?

4) static has a different meaning when used with functions. get_1jy_V should not be a static function. It should just return a double*.
Topic archived. No new replies allowed.