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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:29:01+00:00 2026-06-14T19:29:01+00:00

According to the correct answer in Static array vs. dynamic array in C++ static

  • 0

According to the correct answer in Static array vs. dynamic array in C++ static arrays have fixed sizes.

However, this compiles and runs just fine:

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

int myArray[2];

myArray[0] = 0;
myArray[1] = 1;

cout<<myArray[0]<<endl;
cout<<myArray[1]<<endl;

myArray[4];

myArray[2] = 2;
myArray[3] = 3;

cout<<myArray[2]<<endl;
cout<<myArray[3]<<endl;

return 0;
}

Does this mean a static array can be resized?

  • 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-14T19:29:02+00:00Added an answer on June 14, 2026 at 7:29 pm

    You’re not actually enlarging the array. Let’s see your code in detail:

    int myArray[2];
    
    myArray[0] = 0;
    myArray[1] = 1;
    

    You create an array of two positions, with indexes from 0 to 1. So far, so good.

    myArray[4];
    

    You’re accessing the fifth element in the array (an element which surely does not exist in the array). This is undefined behaviour: anything can happen. You’re not doing anything with that element, but that is not important.

    myArray[2] = 2;
    myArray[3] = 3;
    

    Now you are accessing elements three and four, and changing their values. Again, this is undefined behaviour. You are changing memory locations near to the created array, but "nothing else". The array remains the same.

    Actually, you could check the size of the array by doing:

    std::cout << sizeof( myArray ) / sizeof( int ) << std::endl;
    

    You’ll check that the size of the array has not changed. BTW, this trick works in the same function in which the array is declared, as soon you pass it around it decays into a pointer.

    In C++, the boundaries of arrays are not checked. You did not receive any error or warning mainly because of that. But again, accessing elements beyond the array limit is undefined behaviour. Undefined behaviour means that it is an error that may be won’t show up immediately (something that is apparently good, but is actually not). Even the program can apparently end without problems.

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

Sidebar

Related Questions

According to Wikipedia this syntax looks correct... INSERT INTO dbo.metadata_type (name, publishable) VALUES (Content
I wrote an answer for this question: New background according to url , the
Arising from my answer to Dynamic array of objects in C++ and as a
According to this answer : HTML 4.01 specifies that <a> elements may only contain
I have searched lot for this answer but there is no near answer for
According to this site the static methods static Point rectangular(float x, float y); static
Hey, This is just a simple exercise from class that I decided to have
I have searched the web for the correct answer, but I've been failing to
According to rule SA1201 in StyleCop elements in class must appear in correct order.
I believe ( correct me if i am wrong ), according to the C#

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.