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

  • SEARCH
  • Home
  • 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 8340055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:04:01+00:00 2026-06-09T05:04:01+00:00

I have a basic program in C++ which lists a given number of primes.

  • 0

I have a basic program in C++ which lists a given number of primes. The class which does the work is below – my question is, when the input for “amount” is 10 (specifically 10 – it works fine for all other numbers I’ve tried), the array that is generated just below is not initialized to an array of zeros. Hence, “the last element of the array is empty” returns false, and my code does not get to run properly.

I don’t know whether I’ve misunderstood, but shouldn’t the int array initialize to zeros? If not, what is special about the integer 10 which causes it to initialize to strange values?

int* primecalc(int amount) {

int* primes = new (nothrow) int [amount];

//Throw an error if we can't allocated enough memory for the array.
if (primes==0) {
cout<< "Error allocating memory.";
return 0;
}

//Otherwise, start iterating through the numbers.
else {
primes[0] = 2;
primes[1] = 3;

int p = 2;

for (int i=4;primes[amount]==0;i++) {
int j = 0;
int k = 0;

    while ((primes[j]<=floor(i/2)) && !(primes[j]==0) && (k==0)) {

        if ((i % primes[j]) == 0) {
        k=1;
        } 
    j++;
    } //end the while loop

if (k==0) {
        primes[p] = i;
        p++;

}

} //end the for loop

} //end the "else" part (this was only necessary in case memory could not be allocated)

return primes;
}

I also tried without (nothrow), with the same result. Thanks in advance for any help!

  • 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-09T05:04:03+00:00Added an answer on June 9, 2026 at 5:04 am

    int* primes = new (nothrow) int[amount]; is using default-initialization, which for scalars like int is a noop (i.e. no actual initialization is performed).

    If you want explicit initialization, use value-initialization instead:

    int* primes = new (nothrow) int[amount]();
    

    From the C++11 standard, §8.5/6:

    To default-initialize an object of type T means:

    • if T is a (possibly cv-qualified) class type, the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    • if T is an array type, each element is default-initialized;
    • otherwise, no initialization is performed.

    If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

    §8.5/7:

    To value-initialize an object of type T means:

    • if T is a (possibly cv-qualified) class type with a user-provided constructor, then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
    • if T is an array type, then each element is value-initialized;
    • otherwise, the object is zero-initialized.

    An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc., even if no constructor is invoked for the object’s initialization.

    §8.5/6:

    To zero-initialize an object or reference of type T means:

    • if T is a scalar type, the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;
    • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;
    • if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits;
    • if T is an array type, each element is zero-initialized;
    • if T is a reference type, no initialization is performed.

    And finally from §8.5/10:

    An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

    (All emphasis mine.)

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

Sidebar

Related Questions

I have a very basic JavaScript question. I am writing a program which will
i have a really basic java program just one .java/.class file (.java uncompiled .class
If I have the following basic C++ program: #include <iostream> using namespace std; class
I have a Visual Basic program in Excel which opens Word documents and copies
Lets give an over-basic example, I have a program which goes through a directory
Basically, I have to write a basic program that solves the n-queen problem, which
I have a very basic program which displays a standard Textview and a extended
I have a basic program that compares two strings : #include <string> #include <iostream>
I have a very basic program in C++ compiled in VS 11 Beta on
I currently have a basic chat program in c++ that uses WinSock2.h with UDP.

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.