Segmentation fault: 11

hello
i am compiling this code on MAC OSX with llvm-g++ and it is giving me a segmentation fault error. i checked compiling on other machines too, but no help.
here is the code:


#include <iostream>
const int size = 10000000;
using namespace std;

int displayArrays(int x[], int y[], int z[], int length)
{
for (int i=0;i<length;i++)
{
cout << x[i] << " " << y[i] << " " << z[i] << endl;
}
return 0;
}

int fillArrays(int x[],int y[],int length)
{
srand (time(NULL) - 1350000000);
for (int i=0;i<length;i++)
{
x[i]=rand()%10;
y[i]=rand()%10;
}
return 0;
}

int SAXPY(int x[], int y[],int z[], int A, int length)
{
for (int i=0;i<length;i++)
{
z[i] = (A * x[i]) + y[i];
}
return 0;
}

int main()
{
int x[size];
int y[size];
int z[size];
fillArrays(x,y,size);
SAXPY(x,y,z,3,size);
displayArrays(x,y,z,size);

return 0;
}
First please use code tags - the <> button on the right.

The variable size is not declared or initialised in main - always be aware of this - is one of the major sources of error.

Decalre i before using in the for loop.

The normal practice is to declare your functions before main, and put the function definitions after main. That way we don't have to scroll all the way to the bottom to see what main does.

Why do you have such large arrays?
i have made changes in the code as u have mentioned, but still not working.
the maximum value of size variable for which code runs is 698817.

i think this is related to array size limit or so, and i am wondering why is that?
your query about large arrays: i am just playing with arrays.
i am just playing with arrays.


Well make the array size much smaller, if it doesn't matter. The size limit of the array should only be related to the type of the array subscript, and the available memory on your computer, specifically the size of the stack.

Can you show us the latest code with code tags.
here is the new code:

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
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

int displayArrays(int x[], int y[], int z[], int length);
int fillArrays(int x[],int y[],int length);
int SAXPY(int x[], int y[],int z[], int A, int length);


int main()
{
	const int size = 698817;
	const int A = 3;
	int x[size];
	int y[size];
	int z[size];
	fillArrays(x,y,size);
	SAXPY(x,y,z,A,size);
	displayArrays(x,y,z,size);
	return 0;
}

int displayArrays(int x[], int y[], int z[], int length)
{
	int i;
	for (i=0;i<length;i++)
	{
		cout << x[i] <<  "   " << y[i] << "   " << z[i] <<  endl;
	}
	return 0;
}

int fillArrays(int x[],int y[],int length)
{
	int i;
	srand (time(NULL)  - 1350000000);
	for (i=0;i<length;i++)
	{
		x[i]=rand()%10;
		y[i]=rand()%10;
	}
	return 0;
}

int SAXPY(int x[], int y[],int z[], int A, int length)
{
	int i;
	for (i=0;i<length;i++)
	{
		z[i] = (A * x[i]) + y[i];
	}
	return 0;
}



array subscript is int (i also checked with unsigned int but no diff.)
i have macbook air with 4GB RAM x86_64

specifically the size of the stack

didn't get it..
1
2
3
4
5
const int size = 698817;

int x[size];
int y[size];
int z[size];


Wow! about 682.43848MB of memory being allocated for an array?
Mann dream BIG! I heard that the stack is about 1MB, for your info.

If you do this on the heap, I think it would be better, if your memory is not being used to that extent!
You can allocate that on the heap, either by making it static or global or dynamically allocating memory for it.

Hope it helps,
Aceix.
Last edited on
682.43848MB
So basically, you have no idea what the number in brackets means, right?
Sorry about that.
What does it mean?
Last edited on
Your result suggests that you think it's the number of kilobytes that will be reserved for the array. Makes me wonder what you think happens if one wants an array smaller than 1 KiB, but whatever.
It's the numbers of elements. Multiply that number by the size of each element (int is typically 4 bytes long). Thus, each array probably needs 2.67 MiB. So you were off by only 25600%.
Last edited on
with this code i am just sort of playing for now.
have plans to load hyperspectral image data cubes though, in which case 682.XXXXX MB would be like peanuts.. !

not interested in heap data structure

stack is about 1MB

what is this stack thing by the way?
The heap as referred to here is not a data structure. It's the store of memory used for dynamic memory allocation.

http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/
@helios
It's the numbers of elements.

Yes, I know that. What I was trying to do was to calculate the amount of memory which would be needed for the array.

Anyway, Thanks for the info!
Aceix.
Topic archived. No new replies allowed.