Thread: What the hell!? Crashes and memory leaks?

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    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.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 &#37; i) )
    				cout << i << endl;
    }
    You have undefined behavior on the red line.
    It tries to assign to s[10000] among others.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Ah I see.. Ahwell easily fixed Cheers for the help.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 04:42 AM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 10:54 PM
  4. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM