Segmentation fault while calling previously assigned values

I wrote this code to add and keep new data into a class.

1
2
3
4
5
6
7
8
9
class sample
{
public:
	int N;
    double** p;
	void concat(sample&,double a[2]);
	sample();
	~sample();
};


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
#include "test.h"
#include <fstream>

void sample::concat(sample& c, double* a){
	c.p = new double* [c.N+1];
	c.N += 1;
	c.p[c.N-1] = new double [2];
	c.p[c.N-1][0] = a[0];
	c.p[c.N-1][1] = a[1];
}
sample::sample(){
	N=0;
	p = NULL;
}

sample::~sample(){
	delete p;
}

int main(){
sample a;
double s1[] = {1.2,-2};
a.concat(a,s1);
double s2[] = {3,-6.1};
a.concat(a,s2);
printf("%i",a.N);
printf("\n%1.2f",a.p[0][0]);
}

While I call for printf("\n%1.2f",a.p[1][0]); or printf("\n%1.2f",a.p[1][1]); it perfectly gives me values 3 and -6.1. But, when I call for printf("\n%1.2f",a.p[0][0]); or printf("\n%1.2f",a.p[0][1]); it gives segmentation fault.
What goes wrong?
Last edited on
Where do you set N?
In the constructor

1
2
3
4
sample::sample(){
	N=0;
	p = NULL;
}
0+1 is 1, which means that the only valid index is 0, because 1 is past the end of the array.
L B,
I don't agree.
N increases each time I call a.concat. The first time I add {1.2,-2} to a and the second time {3,-6.1}. When I call a.p[1][1], there is no segmentation fault. But when I call a.p[0][1], there is.
It looks like it deletes the value of the array when resizing.
Forget about the above codes. The simple code below explains m problem better:
1
2
3
4
5
6
7
8
9
10
11
int main (){
	int** a = new int* [1];
	a[0] = new int [2];
	a[0][0]= 1;
	a[0][1] = 11;
	a = new int* [2];
	a[1] = new int [2];
	a[1][0] = 2;
	a[1][1]=22;
	printf("%i,%i",a[0][0],a[0][1]);
}

This gives segmentation fault, while if I call for printing printf("%i,%i",a[1][0],a[1][1]);, every thing goes all right.
Last edited on
At line 6, there is a memory leak, as the memory previously allocated at lines 2 and 3 is not deleted.

a is assigned a fresh value at line 6, so the previously allocated memory is no longer accessible.

At line 10, a[0] refers to a pointer which has not been initialised.
Dear Chervil
As far as I understand, you are saying that even if I reallocate like
1
2
3
4
5
6
7
8
9
10
11
12
13
int main (){
	int** a = new int* [1];
	a[0] = new int [2];
	a[0][0]= 1;
	a[0][1] = 11;
	a = new int* [2];
	a[1] = new int [2];
	a[0][0]= 1;
	a[0][1] = 11;
	a[1][0] = 2;
	a[1][1]=22;
	printf("%i,%i",a[0][0],a[0][1]);
}

I still get the same error. I also have to put delete a; between lines 5 and 6.
Did I get it right?
Last edited on
I'm not saying that putting the delete in the code will fix the segmentation fault. But it will fix the memory leak.
One delete for each new
One delete [] for each new []

If we ignore the memory leak for now, let's simplify the code to focus on the cause of the problem.

Look at this simpler version:
1
2
3
4
5
6
7
8
9
int main ()
{
	int** a;
	a = new int* [2];
	a[1] = new int [2];
	a[1][0] = 2;
	a[1][1] = 22;
	printf("%i,%i",a[0][0],a[0][1]);
}


Now do you see that a[0] is not initialised?
This is effectively the same as the original code, with the same error, a[0] doesn't point to anything.
Chervil,
I got your point about memory leak. On the other hand, the following code which takes care of memory leak without line 5 delete a; still gives segmentation fault.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main (){
	int** a = new int* [1];
	a[0] = new int [2];
	a[0][0]= 1;
	a[0][1] = 11;
        delete a;
	a = new int* [2];
	a[1] = new int [2];
	a[0][0]= 1;
	a[0][1] = 11;
	a[1][0] = 2;
	a[1][1]=22;
	printf("%i,%i",a[0][0],a[0][1]);
}

What I learned from you is
1- memory leak
2- each new needs a delete
Thanks
Fixed version:
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
int main ()
{
    int ** a;
    
    a = new int* [1];
    a[0] = new int [2];
    a[0][0] = 1;
    a[0][1] = 11;
    delete [] a[0];
    delete [] a;

    a = new int* [2];
    a[0] = new int [2];
    a[1] = new int [2];
    a[0][0] = 3;
    a[0][1] = 33;
    a[1][0] = 2;
    a[1][1] = 22;
    printf("%i,%i\n",a[0][0],a[0][1]);
    printf("%i,%i\n",a[1][0],a[1][1]);

    delete [] a[0];
    delete [] a[1];
    delete [] a;
}
Last edited on
Topic archived. No new replies allowed.