-5

In c/c++:

The delete operator is used to delete a dynamic array, but after deleting an array using delete operator, then printing it, some values still the same. why ?- I expect the values to be garbage.

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    int*A=new int[n];

    for (int i=0;i<n;i++)
       A[i]=i+1;

    delete [] A; //deleting 'A' array

    for (int i=0;i<n;i++)
       printf("%d ",A[i]);

}

The output will be:

0 0 3 4 5

If we remove delete [] A; the output will be :

1 2 3 4 5

Why some values of the array still be the same assuming that he elements of the array should have garbage values?

NAND
  • 663
  • 8
  • 22

1 Answers1

-1

Your code has an access after free bug. If you fix the bug, the mystery will go away. It really is that simple.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • How can I fix it ? – NAND Jun 14 '19 at 05:15
  • 4
    @AbdulrhmanAboghanima - By never accessing memory you freed already. – StoryTeller - Unslander Monica Jun 14 '19 at 05:16
  • 1
    @AbdulrhmanAboghanima You can remove the `delete[]` call or you can remove the access to `A[i]` after it. But until you do that, your compiler is not required to make your program behave in any specific way. – David Schwartz Jun 14 '19 at 05:20
  • OK thanks . But I saw an online video that is trying to execute the same code of mine .. After execution the output is garbage values unlike mine .. – NAND Jun 14 '19 at 05:28
  • @AbdulrhmanAboghanima Right. As I said, the compiler is not required to make your program behave any specific way, so it doesn't. Fix the bug and the mystery will go away. You code could make your dog pregnant, it could break your dishwasher, it could even make demons fly out of your nose. The compiler is not required to handle it any particular way and is entirely free to do whatever it wants. – David Schwartz Jun 14 '19 at 05:35
  • 1
    While correct, this answer doesn't really answer the question asked. The OP is evidently aware of the bug (by expecting garbage) and want to understand why the bug doesn't yield the "expected" result (garbage), not how to fix it or do it correctly. – Max Langhof Jun 14 '19 at 08:25
  • 1
    @AbdulrhmanAboghanima Anything can happen when you do illegal things in C++. "Anything" includes "only some of the data is overwritten". There is no guarantee that you will read garbage - it might be the original data, it might be partially or randomly overwritten or it might be garbage, or it might segfault, not execute your code at all, execute _other_ code that you didn't ask it to etc... To find out what the compiler decided to do with your code, [inspect](https://godbolt.org/z/SIDQNH)/debug (step through) the assembly of your program. Don't expect exact behavior from undefined behavior. – Max Langhof Jun 14 '19 at 08:27
  • @MaxLanghof And the answer is that so long as the code has a bug of this type, its behavior will be mysterious. The solution is simply to fix the bug and the mystery goes away. The answer to why a program with undefined behavior doesn't behave as it was expected to behave is always that you should fix the UB and understand that UB can't reasonably be expected to behave any particular way without reference to a specific platform, compiler, compilation flags, phase of the moon, and so on. – David Schwartz Jun 14 '19 at 09:30