1

I am writing a sieve program in c++. But for every legitimate input, the program always produces output with 4 primes founded and "2 3 5", no matter how the input varies. As I try to run the program via the console, it gives an error message saying that double free or corruption (fasttop): 0x000000000063d070 ***. Btw, I am new to c++. And also, I am trying to format the output correctly, but the they are just flying around.

This is the desired format.

2   3   5   7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71

73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Echo111
  • 139
  • 2
  • 9
  • `PrimesSieve(limit).~PrimesSieve();` -- Why are you calling the destructor explicitly? – PaulMcKenzie May 26 '16 at 02:37
  • So that's not the correct way to call it? Or do we not need to call it? I am new to C++. I am sorry. – Echo111 May 26 '16 at 02:39
  • There is no need to call destructors explicitly. When the object goes out of scope, the destructor is called automatically. All you end up doing is calling the destructor, and then the destructor is called again -- that will wind up with double free errors as you're seeing now. Also, what C++ book or tutorial do you see destructors called like that? I bet you haven't found one (unless it is advanced and discusses *placement-new*) – PaulMcKenzie May 26 '16 at 02:41
  • And also, I am trying to format it nicely. But numbers are just flying around... – Echo111 May 26 '16 at 03:13
  • Formatting is another question. Worry about the crash first -- pretty output means nothing if the program doesn't produce the results. – PaulMcKenzie May 26 '16 at 03:23
  • the results are all correct. I am just so confused. ......The program produces correct spacing for 1-digit and 3-digit numbers, but the program does not work for the spacing for the 2-digit numbers, and it output 8 spaces in front of 2(which should be none) when finding the primes up to 2. – Echo111 May 26 '16 at 03:28
  • `PrimesSieve(limit).~PrimesSieve();` - this line creates a temporary object, then calls the destructor of the temporary object, then destroys the temporary object (calling its destructor *again*) – user253751 May 26 '16 at 03:37

1 Answers1

1

Aside from your double-free being caused by calling the destructor explicitly as @PaulMcKenzie said in the comments, your problem with only outputting the first few primes is because of this line:

int n = sizeof(is_prime_);

is_prime_ is a pointer and so its size is fixed at compile time (probably 4 or 8 bytes depending on your system).

You already have limit_ as a value, you should use that to work out your n.

The Dark
  • 8,453
  • 1
  • 16
  • 19
  • The program produces correct spacing for 1-digit and 3-digit numbers, but the program does not work for the spacing for the 2-digit numbers, and it output 8 spaces in front of 2(which should be none) when finding the primes up to 2. I am confused. How could this happen?... – Echo111 May 26 '16 at 03:27
  • `setw` sets the width of the output field - there is no need to add the number of digits in the current value to it, as it will already take that into account. Just use `cout < – The Dark May 26 '16 at 04:54