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

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

The Archive Base Latest Questions

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

Let’s say we start out with: int *newArray = new int[1]; And then later

  • 0

Let’s say we start out with:

int *newArray = new int[1];

And then later have something like:

ifstream inputFile("File.txt");

Counter=0;

while (inputFile >> newValue)
{
    newArray[Counter] = newValue;
    Counter++
}

If I try to pull 100 lines from the text file, the program will eventually crash. However, if I had used

int *newArray = new int[100];

originally, it doesn’t crash.

If it’s dynamically allocating memory, why does it need an initial value more than 1? That makes no sense to me. Having to define any initial length beyond a small number such as 1 or 10 defeats the whole purpose of dynamic memory allocation…

EDIT: This is for school, we aren’t allowed to use vectors yet.

  • 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-15T17:09:19+00:00Added an answer on June 15, 2026 at 5:09 pm

    It’s not dynamic in the sense that it can dynamically resize itself. It’s dynamic in the sense that its size can be chosen dynamically at runtime, instead of compile time. One of the primary philosophies of C++ is that you don’t pay for what you don’t use. If dynamic arrays worked the way you are asking, that would require bounds checking, something I don’t need, so I don’t want to pay for it.

    Anyway, the problem is solved with the standard library.

    std::vector<int> vec;
    ...
    while (inputFile >> newValue)
    {
        vec.push_back(newValue);
    }
    

    Isn’t that much nicer? You don’t even have to keep track of the size, because vector keeps track of it for you.

    If you can’t use vector, then you’ve got a lot of work ahead of you. The principle is essentially this. You keep 2 additional integer variables. One to indicate the number of values you are using in your array, and one to indicate the current capacity of your array. When you run out of room, you allocate more space. For example, here is a poor man’s non-exception safe version of a vector:

    int size = 0;
    int capacity = 1;
    int array = new int[capacity];
    
    while (inputFile >> newValue)
    {
        if (size == capacity)
        {
            capacity *= 2;
            int * newArray = new int[capacity];
            for (int i=0; i<size; ++i)
                newArray[i] = array[i];
            delete [] array;
            array = newArray;
        }
        array[size++] = newValue;    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I get a PreparedStatement from a Connection object, and then later I
Let's say I have some text as follows: do this, do that, then this,
Let's say you have an array like this: array ( ['one'] => array (
Let's say you have a string that looks like this: token1 token2 tok3 And
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let's say I've got a collection of 10 million documents that look something like
Let say you have an XML like this: <?xml version=1.0 encoding=utf-8?> <Class HashCode=307960707> <Person>

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.