Trouble Understanding Dynamic Memory

closed account (3872Nwbp)
Title.

I've been trying to understand this better, and it's... kind of working. In my example I've been working on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Gun *rifle = new Gun();

std::cout << "get rifle accuracy: " << rifle->getAccuracy() << std::endl;

std::vector<GunPart*> parts;
parts.push_back(&rifle->barrel);
parts.push_back(&rifle->stock);
parts.push_back(&rifle->magazine);
parts.push_back(&rifle->grip);
parts.push_back(&rifle->chamber);
parts.push_back(&rifle->scope);

Gun *pistol = new Gun(*parts.at(0), *parts.at(1), *parts.at(2), *parts.at(3), *parts.at(4), *parts.at(5));

delete rifle;

parts.erase(parts.begin(), parts.end());

std::cout << "pistol accuracy: " << pistol->getAccuracy() << std::endl;
std::cout << "rifle accuracy: " << rifle->getAccuracy() << std::endl;

delete pistol;


I'm trying to work with objects that can be deleted at any time, for any number of reasons. Gun and GunPart are two different classes, Gun has 5 GunPart members. (this is just testing code, not any actual code)

This compiles and runs and everything. The part I'm confused on involves the results of trying to manage the memory here.

I declared *rifle, and later store it's GunPart members into a vector. Those parts get copied over to a different Gun object. I then delete rifle and erase the vector's elements. I've checked, the destructors are called on rifle and it's member GunPart's.

But, the line where I cout the accuracy still works. Meaning, it gives the correct value. The same as the first time it's called. But I was supposedly deleted the data. I've read the dynamic memory part of the tutorial a few times, but I feel there is still some concept I'm missing.

Any help is appreciated.
But I was supposedly deleted the data.


No, that is not what delete does. delete tells the compiler that the memory that was previously allocated by new is available for reuse. That is all it does. It does not change the data. It does not set anything to zero. Whatever values were in that memory before are still there in the same place.

The memory is available for reuse by something else, so it could be reused immediately. It could never be reused. It could get partially reused in five minutes' time. The data will not be changed until it is reused (and indeed, why would the data be changed?).

So, you are reading some memory that is available for reuse by something else. By chance, nothing else has written over the top of it yet, so the data that was there before is still there. Do not rely on this; when (if) it does get written over, you'll have no way of knowing.

You have told the compiler that you don't care about this memory anymore - that's all.
closed account (3872Nwbp)
Ok! That makes a lot more sense then. I've always misunderstood it as deleting the reference to that block of memory. I was expecting some sort of complaint from the compiler or OS when compiling or running that line.

Thanks.
Topic archived. No new replies allowed.