MinGW crashes when running this program

Back. Noob as always. Had to take a long break due to school. Anyway, I wrote a simple program for spitting out prime numbers up to a number n (its value is hard-coded). Unfortunately, when I run the program in MinGW, it crashes if the value of n is greater than 260269. Which is weird. The cause is either a poor compiler (I doubt the GCC is so shitty) or it's because my code is so shitty that it simply can't handle such an unoptimized way of crunching numbers. Even so, I still think it's weird. Here is the program:

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
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int c, i, n = 260296, x, primes[n];
	bool flag = true;
	
	for (i = 1; i < n; i++)
		primes[i] = 0;
	primes[0] = 2;
	
	c = 0;
	i = 0;
	x = 3;
	
	while (x <= n) {
		do {
			if (x % primes[i] == 0)
			{
				flag = false;
				break;
			}
			i++;
		} while ( primes[i] <= sqrt(x) && primes[i] != 0);
		
		if (flag) {
			c++;
			primes[c] = x;
		}
		else
			flag = true;
		i = 0;
		x++;
	}
	
	for (i = 0; primes[i] != 0; i++)
		cout << primes[i] << "\n";
}


Any way I can make this code notably better?
May be because you're allocating 'primes' on the stack and it's running out of memory.
How would I allocate it on the heap? Would I write something like this?

int *ptr = new int[n];
Topic archived. No new replies allowed.