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++;          
            }               
        }
6
  • 3
    Your teacher is crazy if he/she expects this to be consistent. It's undefined behaviour. Commented May 5, 2013 at 15:29
  • @chris I have tried if(memcmp((void*)(t+i), (void*)&one, 4) != 0){ but it is the same. Commented May 5, 2013 at 15:33
  • 1
    I don't follow, what is your teacher asking for? Commented May 5, 2013 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. Commented May 5, 2013 at 15:35
  • 1
    Do you have multiple accounts, Robert? Your code looks familiar. link 1, link 2 Commented May 5, 2013 at 15:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You are not allowed to access to a released buffer

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.