简体   繁体   中英

Memory management in C++.

I have the following program:

//simple array memory test.

#include <iostream>
using namespace std;

void someFunc(float*, int, int);

int main() {

  int convert = 2;

  float *arr = new float[17];
  for(int i = 0; i < 17; i++) {
    arr[i] = 1.0;
  }

  someFunc(arr, 17, convert);

  for(int i = 0; i < 17; i++) {
    cout << arr[i] << endl;
  }

  return 0;
}

void someFunc(float *arr, int num, int flag) {

  if(flag) {
    delete []arr;
  }
}

When I put the following into gdb and insert a break point at float *arr ... , I step through the program and observe the following:

  1. Printing the array arr after it has been initialized gives me 1 17 times.
  2. Inside someFunc too, I print arr before delete to get the same print as above.
  3. Upon going back into main , when I print arr , I get the first digit as 0 followed by 16 1.0s.

My questions:
1. Once the array has been deleted in someFunc , how am I still able to access arr without a segfault in someFunc or main ?
2. The code snippet above is a test version of another piece of code that runs in a bigger program. I observe the same behaviour in both places (first digit is 0 but all others are the same. If this is some unexplained memory error, how am I observing the same thing in different areas?
3. Some explanations to fill the gaps in my understanding are most welcome.

A segfault occurs when you access a memory address that isn't mapped into the process. Calling delete [] releases memory back to the memory allocator, but usually not to the OS.

The contents of the memory after calling delete [] are an implementation detail that varies across compilers, libraries, OSes and especially debug-vs-release builds. Debug memory allocators, for instance, will often fill the memory with some tell-tale signature like 0xdeadbeef.

delete指针后取消引用指针是未定义的行为,这意味着任何事情都可能发生。

Once the array has been deleted, any access to it is undefined behavior. There's no guarantee that you'll get a segment violation; in fact, typically you won't. But there's no guarantee of what you will get; in larger programs, modifying the contents of the array could easily result in memory corruption elsewhere.

delete gives the memory back to the OS memory manager, but does not necessarily clears the contents in the memory(it should not, as it causes overhead for nothing). So the values are retained in the memory. And in your case, you are accessing the same memory -- so it will print what is in the memory -- it is not necessarily an undefined behaviour(depends on memory manager)

Re 1: You can't. If you want to access arr later, don't delete it.

C++ doesn't check for array boundaries. Only if you access a memory which you are not allowed to you will get segfault

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM