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 8495831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:41:08+00:00 2026-06-10T23:41:08+00:00

I have a problem with initializing an array of double with the size determined

  • 0

I have a problem with initializing an array of double with the size determined in runtime.

MyPoly MyPoly::operator *(const MyPoly& other)
{
    int newDegree = this->_degree + other._degree;
    double array [newDegree] ;
    fillArrayWithZeros(array, this->_degree + other._degree);

    PolyRep::iterator it1 = this->_poly->begin();
    PolyRep::iterator it2 = other._poly->begin();

    for (int i = 0; i <= this->_degree; ++i, ++it1)
    {
        for (int j = 0; j <= other._degree; ++j, ++it2)
        {
            array[i + j] += (*it1) * (*it2);
        }
        it2 = other._poly->begin();
    }
    return MyPoly(array, this->_degree + other._degree);
}

It’s in the second line of the function. If ill put a number say – 10 it works just fine.
There is no compilation error and no runtime error, but when i debug the program i see the array is empty.

The thing is that in the following function the initialization works fine although the size of the array is being determined in runtime as well :

MyPoly MyPoly::operator +(const MyPoly& other)
{
    int bigDegree = (this->_poly->getDegree() > other._poly->getDegree()) ?
            this->_poly->getDegree() : other._poly->getDegree();

    double arr [bigDegree];

    PolyRep::iterator it1 = this->_poly->begin();
    PolyRep::iterator it2 = other._poly->begin();

    for (int i = 0; i <= this->_poly->getDegree(); ++i, ++it1)
    {
        arr[i] = *it1;
    }

    for (int i = 0; i <= other._poly->getDegree(); ++i, ++it2)
    {
        arr[i] += *it2;
    }

    return MyPoly(arr, bigDegree + 1);
}

Both function are in the same class.

Can someone explain what is the problem

  • 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-10T23:41:09+00:00Added an answer on June 10, 2026 at 11:41 pm

    In both codes, you are writing off the end of the array, which can cause arbitrarily bad behavior. You need to use < instead of <= in your loops, or allocate 2 extra slots.

    To answer your question, you are otherwise using runtime-sized arrays correctly.

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

Sidebar

Related Questions

I have a problem initializing an array whose size is defined as extern const.
I have a problem with initialzing a 2D array in python. I want a
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with initializing TabHost. I need to have in View several Tabs
I have a problem with initializing the application. JNDI lookup is not fully initialized
i am newbie at c++ and i have problem initializing my container occ_stat_t using
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my

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.