C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-26-2008, 05:25 AM   #1
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 192
What the hell!? Crashes and memory leaks?

So, what the HELL is going on here... Here's the deal, I made this to find the highest prime factor of the number n (Yeah, w/e that number is, 600 billion something). The thing is, it will run, then all of a sudden it will stop working. Like I will run it say 10 times or so, then it will continuously keep crashing. Sometimes it will print 1 number, sometimes none, sometimes it will cause a runtime error, sometimes just NOTHING happens, I have no freaking idea what the hell is happening here... Also, if I change the array to type int, then run it, it will cause a crash, but then if I change it back to char it will start working again. Every time these crashes happen if I change it to int, run it, then to char it will magically start working again... My only guess is something weird is happening in memory. But I can't see what's wrong with this. Here's the code!

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;
}
Note: When it actually did work the answer was correct... What the hell? Ok cheers.
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 09-26-2008, 05:32 AM   #2
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by pobri19 View Post
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;
}
You have undefined behavior on the red line.
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-26-2008, 05:33 AM   #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) {
when the for-loop ends, x will be the same value as N. So why not use N?

This overflows:
Code:
		while ( i < N ) {
				i+=y;
				s[i] = 0;
			}
if i + y > N you will go outsid the loop here, as you add y to i after checking if i is below N.

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   Reply With Quote
Old 09-26-2008, 05:45 AM   #4
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 192
Ah I see.. Ahwell easily fixed Cheers for the help.
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22