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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:41:58+00:00 2026-06-03T23:41:58+00:00

This is my second time using C++ and I’m trying to port some Java

  • 0

This is my second time using C++ and I’m trying to port some Java code I made but am having trouble understanding some behavior. I have a list of data and want to create another list of the same size but the values are default to zero(in Java I use Arrays.fill(list, 0); after creating it). When I try to do something similar in C++ I get variable-sized object 'list1' may not be initialized

Here’s a code to better example:

#include <iostream>
#include <boost/assign/std/vector.hpp> // for 'operator+=()'

using namespace std;
using namespace boost::assign;

int main()
{
    cout << "Hello World!" << endl;
    vector<short> data;
    data += -40, -30,-10, 20, 50;

    //int coeff [data.size()];
    cout << "data array size is " << data.size() << endl;
    short list1 [data.size()] = {0}; //does not work
    for (int i =0; i<data.size(); i++) {
        cout << "data is " << list1[i] << endl;
    }

    cout << "********** try 2 **************" << endl;
    //this works if I use a actual number to create the list but I want to declare it at runtime
    short list2 [5] = {0};
    for (int ii=0;ii<5;ii++) {
        cout << "data is " << list2[ii] << endl;
    }


    return 0;
}

Like I mentioned, I’m completely green when I comes to C++ (I have read a book and done a few tutorials) so I may be doing something completely wrong. In the event I cannot do this at runtime and only at compile time, is there something else I can use to get the same result?

  • 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-03T23:42:00+00:00Added an answer on June 3, 2026 at 11:42 pm

    c++ does not have dynamically sized arrays, so this is illegal:

    short list1 [data.size()];
    

    but you can use a vector:

    std::vector<short> list1(data.size(),0); 
    

    That creates a vector of the same length as data, full of zeroes.

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

Sidebar

Related Questions

this is my second time using ORACLE xe and Apex. Here is what I'm
This is only my second time using widgets, and first time using buttons on
The title seems confusing but this is my first time using ternary conditions. I've
I'm getting an MDA after running this code for the second time in a
I am having an error when using new XMLHttpRequest() for the second time in
This is my second question on Bamboo ( My First One ). My understanding
this is the second game that I'm having issues with in the same area...
I know I'm a newbie in Rails but this is the second day I'm
This is my first time using this site so sorry for any bad formatting
I'm using this code to play a different mp3 files with every call. The

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.