我是否需要删除在堆中创建的堆栈中的指针?

发布于 2025-01-14 23:03:26 字数 246 浏览 2 评论 0原文

int *createInt()
{
   int *a = new int;
   return a;
}

int main()
{
   int *x = createInt();
   *x = 10;
   cout << *x << '\n';
   delete x; // do I need this?
   return 0;
}

我需要删除x吗?如果不这样做,会导致内存泄漏问题吗?

int *createInt()
{
   int *a = new int;
   return a;
}

int main()
{
   int *x = createInt();
   *x = 10;
   cout << *x << '\n';
   delete x; // do I need this?
   return 0;
}

DO I need delete x? If i don't, will it cause a memory leak problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残花月 2025-01-21 23:03:26

您似乎对指针和所指向的对象之间的区别感到困惑。 ax 是指针。想象一张写有聚会地址的纸。

int *a = new int; 在堆上分配一个新对象,并将地址分配给指针a。这就像在家里举办聚会,然后将地址写在名为 a 的纸上。

return a; 返回指向调用者的指针。这就像将地址纸交给告诉您开始聚会的人。

*x = 10; 访问对象并修改它。这就像去纸上的地址,传递愚蠢的帽子,你已经把它变成了化装舞会。

删除x; // 我需要这个吗? 是的,您需要删除您分配的对象。这表示“在本文中的地址结束聚会”。


Other questions:

如果我不删除内存怎么办?然后聚会将永远继续下去,并且其他人都不能再在该地址举行聚会,因为第一个聚会仍在进行。

如果我删除记忆两次怎么办?两次结束聚会是没有意义的。可能什么也不会发生。否则它可能会烧毁。或者每次可能都不一样。

如果我在删除指针后使用它怎么办?去参加一个结束的聚会是没有意义的。也许什么也没有发生。或者也许有人用军事基地取代了房子,而你非法侵入并被逮捕。或者每次可能都不一样。

如果我重新分配指针而不删除会怎样?如果你擦掉纸并在上面写上新的地址,这不会对聚会产生任何影响。只是你可能再也找不到它了,而且聚会永远继续下去。

有人告诉我,当类被销毁时,类的成员也会被销毁。这会删除该对象吗? 不,烧掉那张写有地址的纸并不会结束聚会。

You appear confused about the distinction between a pointer and the pointed at object. a and x are pointers. Think of a piece of paper with a party address written on it.

int *a = new int; allocates a new object on the heap and assigns the address to the pointer a. This is like starting a party at a house, and then writing the address on a paper named a.

return a; returns the pointer to the caller. This is like handing the address-paper to the person that told you to start a party.

*x = 10; accesses the object, and modifies it. This is like going to the address on the paper, and passing around silly hats, you've changed it to a costume party.

delete x; // do I need this? Yes, you need to delete the object that you allocated. This says "end the party at the address on this paper".


Other questions:

what if I don't delete memory? Then the party continues forever, and nobody else can party at that address ever again because the first party is still going.

what if I delete the memory twice? Ending a party twice makes no sense. Nothing might happen. Or it might burn down. Or it might be different each time.

what if I use the pointer after deleting it? Going to a party that ended makes no sense. Maybe nothing happens. Or maybe someone replaced the house with a Military base, and you're trespassing and get arrested. Or it might be different each time.

what if I reassign the pointer without deleting? If you erase the paper and write a new address on it, that doesn't do anything to the party. Except that you probably won't ever find it again, and the party continues forever.

I was told that members of classes are destroyed when the class is destroyed. Does that delete the object? No, burning the piece of paper with the address on it doesn't end the party.

乖乖公主 2025-01-21 23:03:26

该语句

delete x;

不会删除指针 x 本身。它删除指针 x 指向的动态分配的内存。

int *a = new int;

//...

int *x = createInt();

在此语句之后,

delete x;

指针 x 将处于活动状态,但将具有无效值。由于指针本身具有自动存储期限,因此它会在定义它的块作用域结束后自动销毁。

您应该始终删除动态分配的内存以避免内存泄漏。

This statement

delete x;

does not delete the pointer x itself. It deletes the dynamically allocated memory pointed to by the pointer x

int *a = new int;

//...

int *x = createInt();

After this statement

delete x;

the pointer x will be alive but will have an invalid value. As the pointer itself has automatic storage duration then it will be destroyed automatically after the end of the block scope where it is defined.

You should always delete dynamically allocated memory to avoid a memory leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文