Segmentation fault. gdb does not give me a clue

Hi there, I have a segmentation fault caused by a function. I display the code below. Any suggestions as to why this might be happening? Thanks a million!

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
#include <iostream>
#include <ctime>
#include <cstdlib>


int * aRand(const int &nL_bound, const int &nU_bound, const int &nSize, const unsigned int &nSeed = time(NULL)) {
//return a pointer to an array of size N of p-random numbers.
//parameters: boundaries, size and seed (by default set to time(NULL).


//initialise seed
	srand(nSeed);

//declare range and array to store p-random numbers.
	int nRange = nU_bound - nL_bound + 1;

	int * pnRandom = new int[nSize];

//loop over size to assign random number to each element of array.
	for (int i = 0; i < nSize; i++) pnRandom[i] = nL_bound + (rand()/100%nRange);

	delete[] pnRandom;

//line 24  pnRandom = 0 //???
	return pnRandom;
}

//*******test driver

int main () {

	int * p;

	p = aRand(1,100,10);

	for ( int i = 0; i < 10; i++ ) {
		std::cout << "*(p + " << i << ") : ";
		std::cout << *(p + i) << std::endl;
	}

	return 0;
}

The code as it is will produce always a zero in the first printed element. If I leave uncommented pnRandom = 0; on line 24 then it will return a segmentation fault (core dumped).
Both versions compile without any warning message.

When I use gdb to debug, it does not tell me useful info as to where the problem is.

Any hints?

Many thanks!
Last edited on
valgrind gives a somewhat more helpful error message.

valgrind wrote:
Invalid read of size 4
at 0x400F1E: main (b.cpp:38)
Address 0x0 is not stack'd, malloc'd or (recently) free'd


aRand returns a pointer to memory that you have freed using delete[], or a null pointer if line 24 is uncommented. Trying to dereference that pointer, like you do on line 38, is an error.
Thank you so much Peter87. I also notice that valgrind is a better tool, I will try it.

My intention with the expression delete[] inside the function was trying to avoid a "memory leak", so that that bit of dynamically allocated memory could not be accessed or reallocated or deleted afterwards.

I have deleted that line and added it inside the test driver. Now it seems to give right values every time I run the program. I am pasting the tweaked code here:
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
#include <iostream>
#include <ctime>
#include <cstdlib>


int * aRand(const int &nL_bound, const int &nU_bound, const int &nSize, const unsigned int &nSeed = time(NULL)) {
//return a pointer to an array of size N of p-random numbers.
//parameters: boundaries, size and seed (by default set to time(NULL).


//initialise seed
	srand(nSeed);

//declare range and array to store p-random numbers.
	int nRange = nU_bound - nL_bound + 1;

	int * pnRandom = new int[nSize];

//loop over size to assign random number to each element of array.
	for (int i = 0; i < nSize; i++) pnRandom[i] = nL_bound + (rand()/100%nRange);

	return pnRandom;
}

//*******test driver

int main () {

	int * p;

	p = aRand(1,100,10);

	for ( int i = 0; i < 10; i++ ) {
		std::cout << "*(p + " << i << ") : ";
		std::cout << *(p + i) << std::endl;
	}

	delete[] p;

	return 0;
}


Does it seem more correct? Is the delete[] p; in line 38 appropriate?

Thanks again for the advice... MUCH appreciated!!
The program seems to work OK but there is no need passing the int arguments by reference to the function. If you don't want to worry about delete[] you could use a vector instead.
http://www.cplusplus.com/reference/vector/vector/
Last edited on
Magnificent, that's brilliant! I need to get to grips with vector though. Thanks and have a nice day Peter87!
Topic archived. No new replies allowed.