从另一个函数中释放指针

发布于 2024-10-26 22:55:38 字数 1392 浏览 7 评论 0原文

给定代码:

#include<iostream>
using namespace std;

class String
{
      char *pstr;
      unsigned size;
      public:
             String(){ pstr=0;size=0;}
             String(const char *);
             void show(){ cout << pstr << endl ; }
             ~String () { cout << "In Dtor" << endl; delete [] pstr; }

};

String::String(const char * cptr)
{
    size = strlen (cptr) + 1;
    cout << "String is - " << cptr << " - of size " << size - 1 << endl ;
    pstr = new char [ size ] ;
    for ( int i = 0 ; i < size ; i++)
        pstr[ i ] = cptr [ i ];

}



int main()
{
    String s("Hello World");
    s.show();
    s.~String();
}

输出:

    String is - Hello World - of size 11
    Hello World
    In Dtor
----Debug Assertion Failure----
    In Dtor

为什么析构函数会再次被调用?当我调用析构函数时?

什么是断言失败?

另外这个代码有效吗?

char * ptr=0;    

 void fun()
 {
      const char * p = "Hello World";
      int size = strlen(p )+ 1;
      cout << size << endl;
      ptr = (char *)malloc(size);
      for ( int i = 0 ; i < size ; i++)
      ptr[ i ] = p [ i ];
      cout << p << endl << ptr << endl ;
 }

int main()
{
    fun();   
    free (ptr); --> Note
}

指针可以从另一个函数中释放吗?这是我在这里试图理解的主要内容。

Given the code:

#include<iostream>
using namespace std;

class String
{
      char *pstr;
      unsigned size;
      public:
             String(){ pstr=0;size=0;}
             String(const char *);
             void show(){ cout << pstr << endl ; }
             ~String () { cout << "In Dtor" << endl; delete [] pstr; }

};

String::String(const char * cptr)
{
    size = strlen (cptr) + 1;
    cout << "String is - " << cptr << " - of size " << size - 1 << endl ;
    pstr = new char [ size ] ;
    for ( int i = 0 ; i < size ; i++)
        pstr[ i ] = cptr [ i ];

}



int main()
{
    String s("Hello World");
    s.show();
    s.~String();
}

Output:

    String is - Hello World - of size 11
    Hello World
    In Dtor
----Debug Assertion Failure----
    In Dtor

Why does the Destructor get called again? When i have invoked the destructor?

And what is an Assertion Failure?

Also is this code Valid?

char * ptr=0;    

 void fun()
 {
      const char * p = "Hello World";
      int size = strlen(p )+ 1;
      cout << size << endl;
      ptr = (char *)malloc(size);
      for ( int i = 0 ; i < size ; i++)
      ptr[ i ] = p [ i ];
      cout << p << endl << ptr << endl ;
 }

int main()
{
    fun();   
    free (ptr); --> Note
}

Can the pointer be freed from another function? This is the main thing i am trying to understand here.

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

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

发布评论

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

评论(2

看透却不说透 2024-11-02 22:55:38

您不应该手动调用析构函数 - 当 s 在最后一个 '}' 超出范围时调用它。

断言失败意味着名为 assert(somecondition) 和 somecondition 的东西被调用。错误的。这是一种用于验证您的假设的技术 - 如果您的代码依赖于某些特定条件为真,并且该条件确实应该为真,除非您有错误,那么您插入一个断言。

然后,您可以选择在启用断言的情况下进行编译 - 这意味着如果您的假设错误,您将收到这样的错误。对于发布版本,您经常禁用断言 - 不会为断言语句生成任何代码,并且没有额外的运行时成本。

在某些情况下,手动调用析构函数是正确的 - 在您了解并使用“placement new”之前,您将不需要它。

You should not invoke the destructor manually - it's invoked when s goes out of scope at the final '}'

An assertion failure means that something called assert(somecondition) and somecondition was false. It's a technique used to validate your assumptions - if your code depends on some specific condition being true, and that condition really should be true unless you have a bug, then you insert an assert.

You can then choose to compile with assertions enabled - this means that you'll get such an error if your assumption was wrong. For a release build, you often disable assertions - no code is generated for the assert statement, and there's no extra runtime cost.

There are cases when it's correct to manually invoke the destructor - You won't need this until you learn about and use "placement new".

初见终念 2024-11-02 22:55:38

除了埃里克已经说过的之外:

在删除手动析构函数调用后,您的代码仍然容易出现双重删除:您必须禁用复制构造函数/赋值运算符,或者正确实现它们(如果您坚持,您将需要引用计数)拥有堆分配的内存)。

In addition to what Erik has already said:

Your code would still remain prone to double deletion after you remove manual destructor call: you have to either disable copy constructor/assignment operator, or implement them correctly (you'll need reference counting if you insist on owning heap-allocated memory).

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