Thursday, 15 March 2012

c++ - No idea where seg fault is occuring -


For some reason, when I run this code, I get a mistake when I-for-loop. The value of this is 7654319. However, the strange thing is that when I'm not checking that the value is pan-digital, then it normally works without a default font, it works even when I'm checking That this is just a pandigital; But not for both, I used GDB to move through the code and here is the output I received:

  Program received signal SIGSEGV, Segmentation fault 0x00000000004007d3 main () . If CCI 81 81 (If Ipadigital (I) and Prings [I]) 6: Prime [i] = & lt; Error: Address 0x7ffefffffff4 & gt; 5: i = & lt; Error: 0x7ffefffffff4 is unable to access memory at address & gt; 4: Prime [7654317] = & lt; Error: Address 0x7ffefffffff8 & gt; 3: Prime [7654321] = & lt; Error: Address 0x7ffefffffff8 & gt; 2: prime [765431 9] = & lt; Error: Address 0x7ffefffffff8 & gt; Can not access storage at address. 1: Primes = & lt; Error: Address 0x7ffefffffff8 & gt; Can not use memory at address   

By output, it seems that by adding i value to the isPandigital (int) function, it mainly I affects the value of i. It did not matter to me, but I went ahead and used a different variable to represent it in the isPandigital (int) function, but still me Receives the same error.

Can anyone help me? These types of errors are very upset because everything seems like this should work, but it is not and its solution is merely hiding under the layers of the implementation. Any help is appreciated!

  #include & lt; Cstdio & gt; # Max 7700000 has been typed, not realized; Bool * GetPrimes () {int int = MAX; Bull * sieve = new hair [need]; For (int s = 0; s  (& gt; 1) is true: wrong); W = = 2; } Return sieve; } Bool's pedigital (intuum) {int arr [] = {1,2,3,4,5,6,7}, g, count = 7; Do {g = number% 10; If (ARR [G-1]) -count; ARR [G-1] = 0; } While (num / = 10); Return (count!); } Int main () {bool * prime = GetPrimes (); Int i; For (i = 7654321; i> 2; i- = 2) {If (Prime [i] & amp; amp; amp; Andapply (i) break; } Printf ("% d \ n", i); Return 0; }    

In your isPandigital function. Note that if there is a honor of num ten or correspond to 8 or 9 mod 10 , then your Some problems near are often out of off-the-ceism array access.

The first prime for which this happens is 19 (or 7654319 if you go back from 7654321):

  bool IsPandigital (int num) // num ( 76543) 19 {int arr [] = {1,2,3,4,5,6,7}, g, count = 7; Do {g = number% 10; // if G9 is (ARR [G-1]) / G1 is 8; G is only indexed from 0 to 6. - Number; ARR [G-1] = 0; // G-1 is 8; G is only indexed from 0 to 6.} while (num / = 10); Return (count!); }   

Note that although the solution does not have 8 or 9 in it, any primary test can be done.

No comments:

Post a Comment