Error allocating memory

Hey,
first of all, i am writing my Code in C not, C++. I hope you could help me though.

As the title says i've got an Problem after allocating memory for an array. Allocating memory for the first array wiorks pretty fine, but after allocating memory for my second array, i can't access anymore on my first array.

I summarized my code a bit, hope i didn't neglect any important part.

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

int* pressureentries;
double* outputs;

void analyze_trans_file(const char* filename)
{
  if (pressureentries != NULL)
  {
  free(pressureentries);
  pressureentries = NULL;
  }
  if ((pressureentries = (int*) calloc(30, sizeof(int))) == NULL)
      FAIL_WITH_ERROR_CONFIG("Not enough memory");

  //Working with pressureentries, works pretty fine
}

void read_trans_file(int id)
{
  //access on pressureentries still works
  if (outputs != NULL)
  {
      free(outputs);
      outputs = NULL;
  }
  if ((outputs = (double*) calloc(23, sizeof(double))) == NULL)
      FAIL_WITH_ERROR_CONFIG("not enough memory");

  //access on pressurentries throws segmentation fault
}
other_func
{
read_trans_file(5);
}


it seems like the memory of pressurentries is overwritten after allocating memory for outputs. I remember that in previous versions i could access on some first ~7 elements of pressureentries.
Last edited on
Are you calling analyze_trans_file before calling read_trans_file?
Are you calling analyze_trans_file before calling read_trans_file?


No, only in read_trans_file.
Do you call it before trying to access pressureentries?
Topic archived. No new replies allowed.