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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:37:53+00:00 2026-05-25T11:37:53+00:00

Suppose in a code I want the user to specify an integer(say x) and

  • 0

Suppose in a code I want the user to specify an integer(say x) and then allocate those many consecutive cells in computer memory on which I do some kind of processing.

My question is is it “better” to allocate this memory as new double[x] or vector<double>(x).

I know that both methods seem to work fine ( i.e. compiles and runs at least in this teeny little code)

  int x; int* p;
  cout<<"Enter integer x:";cin>>x;
  vector<double> v(x);
  p=new double[x]

If you think the answer is using new then is it possible to somehow magically convert
the memory allocated via new into a vector? Because vectors would give me more power in manipulating the data.

  • 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-05-25T11:37:54+00:00Added an answer on May 25, 2026 at 11:37 am

    It’s better to use vector whenever you can because of the many advantages it provides (automatic memory management, bounds checking, etc).

    Also, to have the vector reserve a number of elements for you to use (i.e. allocate that many all at once in memory instead of eventually resizing up to it as you add elements), you can pass that number to the vector’s constructor:

    int x;
    cout<<"Enter integer x:";cin>>x;
    vector<double> v(x);
    

    Or if you’ve already created a vector, use vector::resize:

    // created v somewhere up here and we want to reuse it
    int x;
    cout << "Enter integer x: "; cin >> x;
    v.resize(x);
    

    And no, there’s no way to tell a vector to use memory you’ve already allocated unless you use a custom allocator [R. Martinho Fernandes, comments].

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

Sidebar

Related Questions

Suppose i have a cookie set in first.com say user. Now i want to
Suppose I want to execute code, for example value += 5 inside a namespace
Suppose I want to count the lines of code in a project. If all
suppose you update your code to revision 10 which is broken. You want to
I want to build a user control suppose MyDiv.ascx. This control renders the div
suppose i have one dowpdown in master page and i want that when user
I want to first form a 3-digit code once a user enters some input
so suppose I detect a user's ip using some code to perform restrictions.... is
Suppose my windows logon user name is test. I want to list all attributes
if possible then please provide me code for implementing url routing. suppose i have

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.