Home > Software engineering >  Why does the data still exist after I delete the space of the array?
Why does the data still exist after I delete the space of the array?

Time:11-19

Today, I found a small problem when creating dynamic arrays.

I use the resize () function to change the size of the array. In the resize () function, I created a temporary array "newData", and then I assigned it the new size I wanted. After assigning the value of the initial array "Data" to it, I set

Data=newData;

At this time, the task of the resize () function has been completed. Before exiting the function, I had a whim and deleted the space of "newData" by

delete [] newData;

After that, when I output the value of Data [2], I can still output the value.

Now I'm a little confused. "Data" and "newData" should be pointers, right? When I use the statement "Data=newData;", what "Data" points to should become the address space that "newData" points to. If I delete the address space of "newData", shouldn't the space corresponding to "Data" also disappear? Why can I continue to output values?

The following is the complete code

#include<iostream>
using namespace std;
int Length = 0;
int* Data;
void resize(int,int *);

int main()
{
  Data = new int[5];//This is the space I allocated for the initial array
  for (int i = 0; i < 5; i  )
     Data[i] = i;
  Length = 5;//There are five data in the initial array
  int newLength = 10;//I started distributing new sizes to the initial array
  resize(newLength,Data);
  cout << Data[2];//The output is still 2
}

void resize(int newLength,int *Data)
{
  int* newData = new int[newLength];//A temporary array
  for (int i = 0; i <Length; i  )//Move the value of the original array to the temporary array
  {
     newData[i] = Data[i];
  }
  Data = newData;
  delete[] newData;//Delete the space where the temporary array is located
}

CodePudding user response:

You (try to¹) access deleted space; that's what is called undefined behaviour in C : Anything might happen. There might be the original values, the might be some other data you worked on put there, there might be the value 0xdeadcafe all over the place, your program might crash or cause a fire, delete all files or give an attacker access.

It's undefined, and you found one of the things that might happen.


¹ From your question, that was your intent. Luckily, you messed up your resize function prototype, and pass in the Data pointer by value, not by reference, so that your Data = newData; doesn't do anything outside your function. That together with the global variables on top of your files: Maybe revisit what a function is and what scope and pass-by-reference mean!


Generally, you're a C beginner: cool! That's a good path to be on.
Maybe do less with new and delete. It's been a while since I used these two, they're becoming a rare pattern in modern C ! C can very well (for the most part) be written without these, completely, by using other methods, where your memory allocation and deallocation are linked to objects' lifetimes. And that's way less error-prone, and honestly, easier to understand.
For example, in your use case, instead of your Data, newData, new and delete handling, you could have said std::vector<int> Data(Length);, have put the values in exactly as you did, and then just called Data.resize(newLength);. No need for manually knowing where to delete your memory!

I'm not sure why you're doing this, but you're declaring your variables globally. That's a bad idea from start to finish, so, um, just don't do that: Declare variables in the scope you need them, which would be your main function. Maybe someone who has had a very early 1970's copy of some C book confused obsolete C with the C you should be taught? I don't know. Global variables are generally a bad idea.

  • Related