简体   繁体   English

在构造函数中初始化const char * - 会有内存泄漏吗?

[英]Initializing const char * in constructor - will there be a memory leak?

Here is the code which I got: 这是我得到的代码:

struct ChoiceLine
{
    const char *prompt;
    ChoiceLine(const char *pr):
        prompt(pr)   //can this cause a memory leak?
    {
        ;
    }
};

... ...

ChoiceLine choiceLine("hello world");

So is it OK to initialize a const char* with another const char* ? 那么用另一个const char*初始化一个const char*可以?

PS: I know about std::string , which unfortunately does not fit my purposes. PS:我知道std::string ,遗憾的是它不符合我的目的。

There is no dynamic memory allocation, so there is no memory leak. 没有动态内存分配,因此没有内存泄漏。 Your data member just points to a string literal. 您的数据成员只指向字符串文字。 It is the equivalent of doing this: 它相当于这样做:

const char* c = "Hello, World!";

Yes that's fine if a little unsafe: the memory associated with prompt is not owned by the class instance. 是的,如果有点不安全则没问题:与prompt相关联的内存不属于类实例。

(In your particular case ChoiceLine choiceLine("hello world"); all will be well since the string literal will last the life of the program). (在你的特殊情况下, ChoiceLine choiceLine("hello world");一切都会很好,因为字符串文字将持续程序的生命周期)。

Hence it would have to be kept in scope for as long as the class instance was in scope. 因此,只要类实例在范围内,就必须将其保留在范围内。

If I were you I'd use a std::string as your class member and suffer a deep copy. 如果我是你,我会使用std::string作为你的班级成员并遭受深层复制。

No there's no memory leak, because nothing is being dynamically allocated. 没有内存泄漏,因为没有动态分配任何东西。

The space required for 'prompt' is just a pointer, and that's effectively allocated by the compiler. 'prompt'所需的空间只是一个指针,并且由编译器有效地分配。

No memory leak. 没有内存泄漏。 Untill you allocate some memory dynamically. 直到您动态分配一些内存。

It depends on why you cannot use std::string , and what your class is supposed to do. 这取决于你为什么不能使用std::string ,以及你的类应该做什么。 As you've written it, it basically leaves the memory management to the caller; 正如你所写的那样,它基本上将内存管理留给了调用者; typically, such classes are used when the only reasonable arguments would be string literals (which have static lifetime). 通常,当唯一合理的参数是字符串文字(具有静态生存期)时,使用这样的类。 In other contexts, I've used such classes to manage pointers to strings (or other things) returned by libraries using a C interface; 在其他情况下,我使用这样的类来管理使用C接口的库返回的字符串(或其他东西)的指针; in such cases, the whole purpose of the class is to free the string in its destructor (often by calling a specific function in the library interface, rather than delete[] or free ). 在这种情况下,类的整个目的是在析构函数中释放字符串(通常通过调用库接口中的特定函数,而不是delete[]free )。

The names in your example suggest that you are in the first case: a prompt is almost certainly a string literal or a string looked up in a dictionary with static lifetime (for internationalization purposes); 您的示例中的名称表明您处于第一种情况: prompt几乎肯定是字符串文字或字符串在具有静态生命周期的字典中查找(出于国际化目的); in either case, it would be an error to attempt to delete or free it. 在任何一种情况下,尝试删除或释放它都是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM