Ryan
Ryan

Reputation: 28227

Deallocating vector of primitives

I'm digging through some ancient code, originally built in C++ Builder 6. Littered through-out the code base is the following pattern, which, IMHO, segfaults as it should:

vector<int> x;
try {
    ... some run of the mill vector manipulation
} __finally {
      delete &x;
}

The delete &x; is really odd. As I understand it, the vector<int> will only exist for the lifetime of the function it is contained within, so no need to call delete on it.

Is there some sensible motivation behind this pattern, or is it just plain wrong?

Upvotes: 1

Views: 54

Answers (2)

Bathsheba
Bathsheba

Reputation: 234825

It's certainly not standard C++.

A delete should only be used if new is used. Else the behaviour is undefined.

Upvotes: 4

Jerry Coffin
Jerry Coffin

Reputation: 490518

At least as you've shown it here, it's just plain wrong.

Upvotes: 2

Related Questions