0

I know that an array with size 0 will initially allocate memory for an array with zero elements using int* arr = new int[0];.

This creates an array with no memory space, and attempting to access elements beyond the bounds of allocated memory results in undefined behavior. But when I try to insert the elements, why they are still accessed through a for loop?.

Though below thing is not the right way to do, just like vector I can copy the elements manually into a new array and point the old array to the new array. Today I was just playing with arrays and observed that with size 0 it should at least display unexpected behavior, crash, or produce incorrect results.

Is it because of some compilers?

int size = 0;
int *arr = new int[0];

int i = 0;
while(1){
    if(size == i){
        size++;
    }
    cout << "\nEnter array ele: ";
    cin >> arr[i];
    cout << "\n\n---Displaying elements---\n";
    for(int j = 0; j < size; j++){
        cout << arr[j] << " ";
    }
    i++;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    **Undefined behavior** can appear to work. – Eljay Aug 22 '23 at 13:51
  • 3
    It is undefined behavior, which means anything can happen, even expected behavior. – Ted Klein Bergman Aug 22 '23 at 13:51
  • 2
    "_it should atleast display unexpected behavior, crash, or produce incorrect results_:": No, as you said yourself accessing out-of-bounds cause undefined behavior. – user17732522 Aug 22 '23 at 13:54
  • 2
    " it should atleast display unexpected behavior, crash, or produce incorrect results." what you observe is unexpected and incorret results. An array with 0 elements has no elements. – 463035818_is_not_an_ai Aug 22 '23 at 13:54
  • Ohh Okay Thanks a lot!! – Sourav Karjole Aug 22 '23 at 13:54
  • 1
    In this case, the pointer points to some memory, as the pointer must have a value. And you're adding things into that memory. This could lead to overwriting memory that are used by other areas of the program, or accessing an non-commited page (which gives you a segmentation fault). – Ted Klein Bergman Aug 22 '23 at 13:55
  • @SouravKarjole C++ is not Java. There is no checking whatsoever when you access array elements like this. To add, there is no guaranteed way (or "standard" way) to make a C++ program crash. You can make a C++ program terminate by throwing exceptions, calling `abort` or with `assert`, and other means, but deliberately writing bad code is not guaranteed to crash a program. – PaulMcKenzie Aug 22 '23 at 13:58
  • @PaulMcKenzie yeah i know that in java it will instantly throw exception. In above code I was expecting it should atleast not show the values that were inserted. – Sourav Karjole Aug 22 '23 at 14:03
  • @PaulMcKenzie Yeah. and Thanks – Sourav Karjole Aug 22 '23 at 14:05
  • @SouravKarjole -- As mentioned, the behavior is undefined. That is the difference between Java and C++ in this regard. C++ does not penalize performance by putting checks in the code when the programmer didn't ask for it. If you want to check if the array access is out-of-bounds, you have to write the code to do that (or use types that have this built in). – PaulMcKenzie Aug 22 '23 at 14:06
  • Read this: https://en.cppreference.com/w/cpp/language/ub - including all the nice articles linked at the end. – Jesper Juhl Aug 22 '23 at 15:28
  • When using `new int[0];`, can `new` return `nullptr`? People have mentioned that the pointer *must* have a value. – Thomas Matthews Aug 22 '23 at 20:07

0 Answers0