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!
#include <iostream>
#include <ctime>
#include <cstdlib>
int * aRand(constint &nL_bound, constint &nU_bound, constint &nSize, constunsignedint &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 = newint[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.
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:
#include <iostream>
#include <ctime>
#include <cstdlib>
int * aRand(constint &nL_bound, constint &nU_bound, constint &nSize, constunsignedint &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 = newint[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?
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/