0

I am learning Data Structures and came across Dynamic array. Now they are supposed to be resizable by creating a new loop and pasting it in new array. But when i take more values than the defined size, shouldn't it throw an error ?

Here's the code:

int main() {
    
    int *arr= new int[3];

    for (int i = 0; i < 6; i++)
        cin >> arr[i];

    for (int i = 0; i < 10; i++)
        cout << arr[i] <<" ";

    return 0;
}

The size of array is 3. I have taken 6 elements and displayed 10. I am confused how this happens.

Please help me understand.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    Arrays are not resizable. `std::vector` is.' – Thomas Matthews Aug 07 '20 at 22:40
  • 1
    There are no facilities in C++ that prevent you from going past the end of an array. It's called Undefined Behavior and anything can happen. – Thomas Matthews Aug 07 '20 at 22:41
  • The code does not "work fine". You are overrunning the buffer. – PaulMcKenzie Aug 07 '20 at 22:41
  • 3
    First of all, dynamic arrays are not resizable. You can only create a new array and copy the existing elements over. Second, C++ is not a "friendly" language that checks for all possible errors and gives you nice error messages when you screw up. Just because it doesn't crash doesn't mean it "works". Nobody said wrong code has to "throw an error", and undefined behavior means any behavior is possible. – eesiraed Aug 07 '20 at 22:43
  • For example, if your array is on the stack, you could overwrite other things on the stack, such as the return addresses of function calls. – Thomas Matthews Aug 07 '20 at 22:43
  • One facet of Undefined Behavior is that you were lucky and it worked *this* time. There's no guarantee that your code will *work* correctly next time. – Thomas Matthews Aug 07 '20 at 22:44
  • so it causes memory leak? – gursimran sarao Aug 07 '20 at 22:45
  • Memory leaks are different. You are access memory outside of the allocated space. – Thomas Matthews Aug 07 '20 at 22:45
  • @gursimransarao As Thomas said, but as a side note: Your example leaks memory in addition to the undefined behaviour. – eerorika Aug 07 '20 at 22:47
  • Understood. thankyou :) – gursimran sarao Aug 07 '20 at 22:47
  • Every bit of protection a program gives to a programmer comes at a cost, usually in performance. C++ has a policy of not making programs pay for anything they don't use. A correct program needs no checks to ensure the programmer did their job correctly, so C++ does not require any checks you do not ask for. Note: This doesn't mean that checks can't be added and some environments add checks for common mistakes to debug builds of programs, where trapping bugs is more important than program performance. – user4581301 Aug 07 '20 at 22:54

2 Answers2

2

shouldn't it throw an error ?

When you read or write outside the bounds of the array, behaviour of the program is undefined. There is no guarantee that an error would be thrown, nor are there any other guarantees about the behaviour.

Now they are supposed to be resizable by creating a new loop and pasting it in new array.

In your example, you only created one array, rather than creating a new larger one.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Funny, you are coding a stack overflow and posying it on stackoverflow ! arr is a pointer on the first int from the array. Whatever the size you allocated for the array arr[n] is n int further in memory. But it can be outside the allocated memory for the array. You are overwriti'g something else...

Ben
  • 149
  • 1
  • 6
  • 1
    *you are coding a stack overflow* No they are not. Definitely a buffer overflow, but a stack overflow is when you allocate too much storage for it all to fit on the stack. In addition , `int *arr= new int[3];` allocated dynamic, not automatic (stack), storage. – user4581301 Aug 07 '20 at 23:04
  • My bad you're right ! – Ben Aug 07 '20 at 23:11