![]() |
| | #1 | |
| Registered User Join Date: May 2008 Location: Australia
Posts: 192
| What the hell!? Crashes and memory leaks? Code: #include <iostream>
#include <cstdlib>
#define uint unsigned long long int
#define N 10000
using namespace std;
char s[N];
void FillSieve() {
int x;
int y = 2;
int i = 0;
for (x = 0; x < N; x++)
s[x] = 1;
while (y < x) {
if (s[y] == 1) {
i = y;
while ( i < N ) {
i+=y;
s[i] = 0;
}
}
y++;
}
}
int main() {
FillSieve();
uint n = 600851475143;
int temp = 0;
for (int i = 2; i < N; i++)
if (s[i] == 1)
if ( !(n % i) )
cout << i << endl;
}
__________________ Quote:
| |
| pobri19 is offline | |
| | #2 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
It tries to assign to s[10000] among others.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #3 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: for (x = 0; x < N; x++)
s[x] = 1;
while (y < x) {
This overflows: Code: while ( i < N ) {
i+=y;
s[i] = 0;
}
As to why it only causes crashes SOMETIMES, that's just the way "undefined behviour" works - it's like running a red light: Sometimes there's a car going across the junction the other direction with a green light that hits you, at other times it's clear and you get across unhurt. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #4 | |
| Registered User Join Date: May 2008 Location: Australia
Posts: 192
| Ah I see.. Ahwell easily fixed Cheers for the help.
__________________ Quote:
| |
| pobri19 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Freeing memory | C_ntua | C Programming | 17 | 06-29-2008 04:42 AM |
| Memory usage and memory leaks | vsanandan | C Programming | 1 | 05-03-2008 05:45 AM |
| Program crashes and memory leaks | ulillillia | Tech Board | 1 | 05-15-2007 10:54 PM |
| Copying memory, pointers and the like. | psychopath | C++ Programming | 34 | 12-12-2006 01:37 PM |
| Locating A Segmentation Fault | Stack Overflow | C Programming | 12 | 12-14-2004 01:33 PM |