Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:50:27+00:00 2026-06-12T00:50:27+00:00

This is some code I wrote for implementing Sieve of Eratosthenes : #include <iostream>

  • 0

This is some code I wrote for implementing Sieve of Eratosthenes:

#include <iostream>
#include <vector>
#include <cmath>
#include <cassert>
#include <cstdlib>

int allPrimes (unsigned long n) {
    std::vector<int> track (n, 0);
    int index = 2;
    int m = sqrt(n);
    while(index < n) {
        if (track[index] == 0) {
            std::cout << index << std::endl;
            int mul = 1;
            while ((index <= m) && (n >= (index * ++mul))) {
                track[index * mul] = 1;
            }
        }
        index++;
    }
}

int main() {
    int num;
    std::cin >> num;
    allPrimes(num);
}

Strangely, whenever num is in the series 6, 10, 14, 18, 22, … the code aborts with the folloiwng stack while deallocating memory (runs okay for other n):

raise () from /lib64/libc.so.6
abort () from /lib64/libc.so.6
__libc_message () from /lib64/libc.so.6
malloc_printerr () from /lib64/libc.so.6
_int_free () from /lib64/libc.so.6
__gnu_cxx::new_allocator<int>::deallocate
std::_Vector_base<int, std::allocator<int> >::_M_deallocate
std::_Vector_base<int, std::allocator<int> >::~_Vector_base
std::vector<int, std::allocator<int> >::~vector
allPrimes (n=6) at allprimes.cpp:20
main () at allprimes.cpp:26

But I do not see the bug, or the logic behind these numbers spaced by 4. What is the bug here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:50:29+00:00Added an answer on June 12, 2026 at 12:50 am

    Here:

    while(index <= n) {
        if (track[index] == 0) {
    

    you allow index to run up to n, which is out of bounds. You need n-1, or while (index < n). There are other such indexing errors in the code, all of which lead to undefined behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a simple client-server program. Some parts of the i wrote is: int
Can someone write some sample code to explain this concept? I know what a
This is some code that I picked up which I tried to implement. Its
This is some code that is behaving peculiarly. This is a simplified version of
This is some code I found on the internet. I'm not sure how it
this is some code that SDL requires in visual studios 2005 in order for
I saw this in some code: $(<p/>).append(<div>something</div>).appendTo(body); and noticed that it automatically closed the
Context I came across some code, like this: if( Some_Condition ) throw 0; I
This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
I have a java code that looks like this: //UI thread //Some code Job

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.