0

I wanted to access deleted array to see how the memory was changed it works till I deleted really big array then I get access violation exception. Please do not care about cout I know they are slow but I will get rid of them. When I do it for 1000 elements array it is ok, when I do it for 1000000 i get an exception. I know that this is weird task but my teacher is stubborn and I can't find out how to deal with that.

EDIT: I know that I never should access that memory, but I also know that there is probably trick he will show then and tell that I am not right.

    long max = 1000000;// for 10000 i do not get any exception.
    int* t = new int[max];
    cout<<max<<endl;
    uninitialized_fill_n(t, max, 1); 
    delete[] t;
    cout<<"deleted t"<<endl;
    int x;
    cin>>x;//wait little bit
    int one = 1;
    long counter = 0;
        for(long i = 0; i < max; i++){
            cout<<i<<endl;
            if(t[i] != 1){
                cout<<t[i]<<endl;
                counter++;          
            }               
        }
  • 3
    Your teacher is crazy if he/she expects this to be consistent. It's undefined behaviour. – chris May 05 '13 at 15:29
  • @chris I have tried `if(memcmp((void*)(t+i), (void*)&one, 4) != 0){` but it is the same. –  May 05 '13 at 15:33
  • 1
    I don't follow, what is your teacher asking for? – john May 05 '13 at 15:34
  • @user1825608, It's still undefined behaviour. Trying to count on anything happening every time you do that will only screw you later. I don't know what your teacher expects to happen, but you might want to get clarification. – chris May 05 '13 at 15:35
  • Find another teacher if your teaching is expecting this to work. It is undefined behaviour, and will not work reliably. – Mats Petersson May 05 '13 at 15:36
  • 1
    Do you have multiple accounts, Robert? Your code looks familiar. [link 1](http://stackoverflow.com/questions/16385453/how-to-fast-initialize-with-1-really-big-array), [link 2](http://stackoverflow.com/questions/16385864/how-to-check-how-many-different-numbers-than-1-are-in-the-array-of-362856427-n) – Blastfurnace May 05 '13 at 15:53

3 Answers3

2

That state of "deleted" memory is undefined, to access memory after delete is UNDEFINED BEHAVIOUR (meaning, the C++ specification allows "anything" to happen when you access such memory - including the appearance of it "working" sometimes, and "not working" sometimes).

You should NEVER access memory that has been deleted, and as shown in your larger array case, it may not work to do so, because the memory may no longer actually be available to your process.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

You are not allowed to access to a released buffer

nosleduc
  • 103
  • 5
0

Accessing memory that is no longer in use results in undefined behaviour. You will not get any consistent patterns. If the original memory has not been overwritten after it became invalid, the values will be exactly what they used to be.

I find the answer to this similar question to be very clear in explaining the concept with a simple analogy.

A simple way to mimic this behaviour is to create a function which returns a pointer to a local variable, for example:

int *foo(){
 int a=1;
 return &a;
}
Community
  • 1
  • 1
Marc Claesen
  • 16,778
  • 6
  • 27
  • 62