简体   繁体   English

有没有一种声明和初始化指针变量的方法?

[英]Is there a one line approach to declaring and initializing pointer variables?

When I need to create a pointer variable, I currently use the following approach which works: 当我需要创建一个指针变量时,我目前使用以下可行的方法:

int getIntPointer()
{
    int * intPointer = new int;
    *intPointer = 0;

     return *intPointer;
}

likewise, when returning such a pointer from another function I would use something like this in main() to use it: 同样,当从另一个函数返回这样的指针时,我会在main()中使用类似的东西来使用它:

int main()
{
    int * intPointer = new int;
    *intPointer = getIntPointer();

    delete intPointer;
 }

This all works, but for readability and efficiency I'd like to know is if there are one line equivalents. 所有这些都有效,但是为了提高可读性和效率,我想知道是否有一行等效项。 (and I don't mean to just put them on the same line. I mean a short hand approach.) (我并不是要把它们放在同一行。我的意思是简捷的方法。)

Also: In this above example, I used the same pointer variable in both functions, and it works. 另外:在上面的示例中,我在两个函数中使用了相同的指针变量,并且可以正常工作。 Are they the same pointer in memory? 它们在内存中是否是相同的指针? Adding something like: 添加类似:

delete intPointer; 

Immediately after the return statement doesn't crash the program, but does it even get there? return语句之后不会立即使程序崩溃,但是它甚至到达那里了吗? Or is it safe to just delete it's passed iteration in main when no longer needed? 还是在不再需要时删除它在main中传递的迭代是否安全?

You could do: 您可以这样做:

int *intPointer = new int(0); 

(or new int(100); if you want intPointer to point at the value 100). (或new int(100);如果希望intPointer指向值100)。

And of course, you could shorten that to : 当然,您可以将其缩短为:

return new int(0);

assuming you don't need to do anything else with the pointer. 假设您不需要使用指针做其他任何事情。

Note that: 注意:

int * intPointer = new int;
*intPointer = getIntPointer();

is incorrect. 是不正确的。 Either you mean: 您的意思是:

int * intPointer = new int;
intPointer = getIntPointer();

in which case it's a memory leak, because you are overwriting intPointer from new with the one created by the call. 在这种情况下,这是内存泄漏,因为您intPointer调用创建的new intPointer覆盖intPointer

Or you mean to write: 或者你的意思是写:

int * intPointer = new int;
*intPointer = *getIntPointer();

in which case there is a memory leak because getIntPointer called new and you "lost" that pointer by not saving the return value. 在这种情况下,会发生内存泄漏,因为getIntPointer调用new并且您通过不保存返回值来“丢失”该指针。

For EVERY new you call, you should have exactly one corresponding delete , or it is a memory leak. 对于每个new调用,您都应该有一个相应的delete ,否则它是内存泄漏。 Since both of my examples above does NOT provide that, because both cases will lose one of the pointers returned from new , this is incorrect. 由于我上面的两个示例均未提供上述说明,因为这两种情况都会丢失从new返回的指针之一,所以这是不正确的。

In general, it is best to NOT use "raw pointers", and instead use either std::unique_ptr (if you only ever expect the pointer to "live" in one place at a time) or std::shared_ptr (if you expect multiple objects to have a copy of the pointer). 通常,最好不要使用“原始指针”,而应使用std::unique_ptr (如果您一次只希望一次指向“实时”指针)或std::shared_ptr (如果您期望多个对象具有一个指针副本)。

Edit: My answer above assumes that getIntPointer actually does what the name describes, rather than what the code in the question does, which is rather poor design: allocate memory and then return the pointed-to value. 编辑:我在上面的回答中假设getIntPointer实际上执行了名称描述的操作,而不是问题中的代码执行的操作,这是较差的设计:分配内存,然后返回指向的值。

I could be wrong, but I think you're reaching for std::shared_ptr and its helper function std::make_shared . 我可能是错的,但是我认为您正在寻求std::shared_ptr及其帮助函数std::make_shared

std::shared_ptr< int > intPointer = std::make_shared< int >( 42 );

Rather than repeat all the use cases of smart pointers here, I'll simply link to an answer that goes into more detail on the topic. 我不会在这里重复智能指针的所有用例,而是直接链接到对该主题进行更详细介绍的答案。

inb4 rants about why smart pointers are good or bad. 关于智能指针为何好坏的inb4抱怨。

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

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