Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

c++ - What does *new int mean?

Can someone help me with this question?

The memory allocated on line (*) below is not deleted.

void f() {
    int z = *new int; // (*)
    //...
}

Without changing the code on line (*), is there any way to avoid leaking memory? If so, how? If not, why not?

What I don't understand is, what does *new int mean? Specifically, what does adding the * beside new mean?

Also, what if instead of int z, we have int &z?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That line is the memory leak. It also makes no sense what so ever allocating an int int on heap and derefencing it (with *) before saving it's handle (address returned by new). So the only posible way to avoid the leak is:

return;
// your silly * line here

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...