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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:32:55+00:00 2026-05-22T16:32:55+00:00

I’m familiar with Java and trying to teach myself C/C++. I’m stealing some curriculum

  • 0

I’m familiar with Java and trying to teach myself C/C++. I’m stealing some curriculum from a class that is hosting their materials here. I unfortunately can’t ask the teacher since I’m not in the class. My concern is with the section under “dynamically declared arrays”:

If you
want to be able to alter the size of
your array at run time, then declare
dynamic arrays. These are done with
pointers and the new operator. For the
basics on pointers, read the pointers
section.

Allocate memory using new, and then
you access the array in the same way
you would a static array. For example,

int* arrayPtr = new int[10]; for
(int i = 0; i < 10; i++) {
arrayPtr[i] = i; }

The memory picture is identical to the
static array, but you can change the
size if you need to. Don’t forget you
must deallocate the memory before
allocating new memory (or you will
have a memory leak).

delete [] arrayPtr; // the []
is needed when deleting array pointers
arrayPtr = new int[50]; . . .

When you’re completely done with the
array, you must delete its memory:

delete [] arrayPtr;

Dynamic multi-dimensional arrays are
done in a similar manner to Java. You
will have pointers to pointers. For an
example, see a

My understanding is that an array in C is simply a reference to the memory address of the first element in the array.

So, what is the difference between int *pointerArray = new int[10]; and int array[10]; if any?

I’ve done some tests that seem to indicate that they do the exact same thing. Is the website wrong or did I read that wrong?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    // Initialize the pointer array
    int *pointerArray = new int[10];
    for (int i = 0; i < 10; i++){

        pointerArray[i] = i;
    }

    // Initialize the regular array
    int array[10];
    for (int i = 0; i < 10; i++){

        array[i]= i;
    }

    cout << *(pointerArray + 5) << endl;
    cout << *(array + 5) << endl;

    cout << pointerArray[5] << endl;
    cout << array[5] << endl;

    cout << pointerArray << endl;
    cout << array << endl;

    return 0;
}

Output:

5
5
5
5
0x8f94030
0xbfa6a37c

I’ve tried to “dynamically re-size” my pointer array as described on the site, but my new (bigger) pointer array ends up filled with 0’s which is not very useful.

  • 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-22T16:32:55+00:00Added an answer on May 22, 2026 at 4:32 pm

    int array[10]; declares the array size statically, that means it is fixed – which is the only major difference. It also might be allocated to be inside the function’s stack frame, i.e. on the program’s stack. You do not need to worry about using delete [] on that kind of array, in fact, you might crash the program if you delete it.

    When you use operator new, you allocate memory dynamically which could be slower and the memory usually comes from the heap rather than the program’s stack (though not always). This is better in most cases, as you are more limited in the stack space than the heap space. However, you must watch out for memory leaks and delete[] your stuff when you don’t need it anymore.

    As to your array being filled with zeros, what your class material does not say is that you have to do this:

    int *arr = new int[20]; // old array
    //do magic here and decide that we need a bigger array
    int *bigger = new int[50]; // allocate a bigger array
    for (int i = 0; i < 20; i++) bigger[i] = arr[i]; // copy the elements from the old array into the new array
    delete[] arr;
    arr = bigger;
    

    That code extends the array arr by 30 more elements. Note that you must copy the old data into the new array, or else it will not be there (in your case, everything becomes 0).

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has

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.