SegFault before main even runs?

I'm getting a segfault before the program even gets to main. It compiles just fine, but not beyond that.

Here's the program. I cut out the functions to find the fault, and it doesn't seem to think it's in them.

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
54
55
56
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;
//Functions were here

int main()
{
	cout<<"A";
	ifstream ifile;
	ifile.open("rmq_data.txt");
	int x, y=0, i, j, n, m;
	ifile>>n;
	int A[n];
	double d=(log(n)/log(2));
	m=floor(d)+1;
	x=n*m;
	int AP[x];
	cout<<"B";
	while(ifile.good())
	{
		ifile>>x;
		A[y]=x;
		y++;
	}
	ifile.close();
	
	for(i=0; i<y; i++)
	{
		for(j=0; (i+pow(2,j))<y; j++)
		//{	AP[i*m+j]=RMQs(i, (i+pow(2,j)), A);	}
	}
	cout<<"C";
	ifile.open("rmq_op.txt");
	ofstream out0, out1, out2;
	out0.open("rmq0_result.txt");
	out1.open("rmq1_result.txt");
	out2.open("rmq2_result.txt");
	
	int L, R, s1, s2;
	cout<<"D";
	while(ifile.good())
	{
		ifile>>L>>R>>s1>>s2;
		/*
		out0<<RMQ0(L,R,m,A,AP)<<endl;
		out1<<RMQ1(L,R,s1,m,A,AP)<<endl;
		out2<<RMQ2(L,R,s1,s2,m,A,AP)<<endl;
		cout<<"E";
		*/
	}
	
	return 0;
}


Any and all help is appreciated!
What compiler are you using?

int A[n];

I believe this works on G++ (I think they introduced this in C99) but doesn't on the Microsoft compiler. Either way, array size should be known at compile time, or the array should be dynamic. Same goes for the array AP[].
Make sure to flush cout, otherwise the output could be delayed and if a crash happens after you will not see the output at all.
cout<<"A"<<endl;

If you want to flush, then flush (endl will put an end of line)
Or use an unbuffered stream, like cerr,for those debug messages
Or learn to use a debugger

You are probably accessing the array out-of-bounds, as your reading method is bugged
1
2
for(int K=0; K<n and ifile>>A[K]; ++K)
   ;
I'm using the compiler built into my school's csc server.

Linux classes.csc.lsu.edu 2.6.26-2-amd64 #1 SMP Tue Jan 12 22:12:20 UTC 2010 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

I think Biscuit was right, though. When I gave it a size, it stopped giving me that particular problem.
Topic archived. No new replies allowed.