C++ 如何删除C++中的char *?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5481503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:14:58  来源:igfitidea点击:

How to delete char * in c++?

c++

提问by karthik

In my application, I create a char*like this:

在我的应用程序中,我创建了一个char*这样的:

class sample
{
    public:
        char *thread;
};

sample::sample()
{
    thread = new char[10];
}

sample::~sample()
{
    delete []thread;
}

Am I doing the right thing in the code?

我在代码中做对了吗?

回答by Fred Larson

If you have []after your new, you need []after your delete. Your code looks correct.

如果你有你的[]new,你需要[]后你的delete。您的代码看起来正确。

回答by karthik

List of points to be noted:

需要注意的点列表:

1) You need to allocate room for n characters, where n is the number of characters in the string, plus the room for the trailing null byte.

1) 您需要为 n 个字符分配空间,其中 n 是字符串中的字符数,加上尾随空字节的空间。

2) You then changed the thread to point to a different string. So you have to use delete[]function for the variable you are created using new[].

2) 然后将线程更改为指向不同的字符串。因此,您必须为使用delete[]创建的变量使用函数new[]

But why are you fooling around with newand deletefor character data? Why not just use std::string, instead of 'C' functions? It's amazing why so many don't do the easiest thing:

但是,为什么你鬼混与newdelete字符数据?为什么不直接使用std::string, 而不是 'C' 函数?令人惊讶的是,为什么这么多人不做最简单的事情:

#include <cstdio>
#include <string>

int countWords(const char *p);

int main(int argc, char *argv[])
{
    std::string pString = "The Quick Brown Fox!";

    int numWords1 = countWords(pString.c_str());
    printf("\n\n%d words are in the string %s", numWords1, pString.c_str());

    int numWords2 = countWords(argv[1]);
    printf("\n%d words are in the string %s", numWords2, argv[1]);
}

No need for new[], delete[], strcpy(), etc.

无需new[]delete[]strcpy()等。

Use strlen(). Better yet, don't use char*and use std::stringfor string data.

使用strlen(). 更好的是,不要使用char*std::string用于字符串数据。

回答by GManNickG

It's "right"*, but it's very wrong.

这是“正确的”*,但这是非常错误的。

You should not use new[], but instead use std::vector<char>or std::string. Even if you weren't doing that, you need to respect the rule of three, or your class is broken.

您不应使用new[],而应使用std::vector<char>std::string。即使你没有这样做,你也需要尊重三原则,否则你的班级就被打破了。

*Assuming you meant new char[10]. Also, more orthodox is delete[] thread.

*假设你的意思是new char[10]. 此外,更正统的是delete[] thread

回答by Jerry Coffin

Though it has nothing to do with the memory management, there is one other point to keep in mind. If you're going to define sample::sampleand sample::~sample, you first have to declare them, so your class definition should look something like this:

尽管它与内存管理无关,但还有一点需要牢记。如果要定义sample::sampleand sample::~sample,首先必须声明它们,因此您的类定义应如下所示:

class sample
{
    public:
        char *thread;
        sample();
        ~sample();
};

As @GMan said, though, you really shouldn't be doing this at all...

不过,正如@GMan 所说,你真的不应该这样做......