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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:30:15+00:00 2026-06-17T22:30:15+00:00

I’m learning about Dynamic Memory Allocation in C++ and the keywords new and new[]

  • 0

I’m learning about Dynamic Memory Allocation in C++ and the keywords new and new[] are mentioned.
It is said to enable users to specify the size of the memory allocation at runtime, unlike simply declaring a variable or array with a fixed size in the source code.

I don’t understand this concept. How does it work? I just need a clarification on the idea and an example would be helpful!

  • 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-17T22:30:16+00:00Added an answer on June 17, 2026 at 10:30 pm

    So, if you want an array of 10 integers, you’d be writing:

    int arr[10]; 
    

    But what if you wanted to do something like this;

    cout << "How many?";
    cin >> num;
    
    int arr[num];
    

    Well, the C++ language doesn’t allow that. Instead, you have to do:

    int *arr = new int[num]; 
    

    to create your array. And later on you MUST[1] use:

    delete [] arr; 
    

    to free the memory.

    So, how does this work? When you call new, the C++ runtime library [the code that you didn’t have to write that makes up the fundamentals of C++] will figure out how much space num integers take up, and find some space in memory for that. I’m not going into details of “how you find some memory”. For now, just trust me, there is some memory available somewhere that can be used to store some integers in.

    When you later call delete, that same memory is given back to the “pool” or “heap” of memory that it came from.

    Of course, if you have a machine with, say, 256 MB of memory, and you try to ask for space to store 250 million integers, bearing in mind that an integer takes up more than one byte, it’s not going to work out – there is no “magic” here – the memory is still limited to how much is available in the machine…. You just have the right to determine in the program, when it’s running, how much memory you need, rather than having to decide when WRITING the program.

    Edit: It is generally best to “hide” any memory allocation using the already existing “container-” and “wrapper-classes” that are useful for this very purpose. For example:

     std::vector<int> arr;
    

    would work as a variable storage for integers, and you never have to worry about freeing the memory, or even knowing how many you need before you have stored them there.

     std::shared_ptr<int> arr = new int[num]; 
    

    is another case, where when the “shared_ptr” is no longer in use [it track that inside the shared pointer class, so you never need to care about freeing the memory].

    [1] If you don’t want to leak memory, and it’s “bad style” to leak memory. Not making anyone happy if you do.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.