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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:27:27+00:00 2026-05-27T12:27:27+00:00

Why does the following code compile and run. I have created an integer array

  • 0

Why does the following code compile and run.

I have created an integer array and assigned its size to 10, why then does the program not return an error that I am trying to access an element outside the array, when inside the for loop?

In addition, I would like to understand the concept of new, is my usage correct(I have not assigned how much memory *arrays needs), it is my understanding that new allows me to create an array of dynamic size (meaning that I can increase the size of this array indefinitely during runtime, correct me if I am wrong). If this is correct what is the difference in me using new or simply just allocating array[]; since both apparently allow me to increase the size of my array at runtime as can be seen with this example. I know about scope, stack and heap differences, so assume that both variables are only declared in main and use the following code as the example.

http://ideone.com/Tbud1

#include <iostream>
using namespace std;

int main()
{
int array[10];
int *arrays;
arrays = new int();
for (int i=0; i<450; i++)
{
   arrays[i] = i;
   cout << arrays[i] << " ";
   array[i] = i;
   cout << array[i] << endl;
}
return 0;
}
  • 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-27T12:27:28+00:00Added an answer on May 27, 2026 at 12:27 pm

    Accessing elements beyond the bounds of an array results in Undefined Behavior.
    Compilers do not need to do anything specific in such guaranteed Undefine Behavior cases.
    And potentially anything might happen, your program might crash or not or show erratic behavior and this is allowed by the Standard.

    Reference :

    C++ Standard section 1.3.24 states:

    Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    Your understanding of new is not correct. new allows you to allocate a fixed amount of memory on the freestore (a.k.a heap). It does not allocate additional memory if you extend the bounds of the allocated memory. It is up to you to ensure that you have to allocate enough memory so that you do not exceed the bounds.

    In your program you are trying to allocate memory equal to 10 array elements so you should be doing:

    arrays = new int[10];
    

    Also, do not forget to call delete[] once you are done using the allocated memory or it results in a memory leak.

    delete []arrays;
    

    If you need a data structure which increases automatically as per your usage, C++ provides that in the form of std::vector.

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

Sidebar

Related Questions

I have the following bit of legacy C++ code that does not compile: #include
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
The following code does not compile: //int a = ... int? b = (int?)
The following code does not want to compile. See the included error message. Code:
I have the following C++ code and when I run the program sometimes it
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
Why does the following code not work as I was expecting? <?php $data =
I'm trying to understand compile versus run-time computation in java. I have the following

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.