Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类方法/函数内的指针 我想知道C++是如何处理指针在类方法或函数内部创建的“对象”的内存的。 例如类example的example方法 void Example::methodExample() { ExampleObject *pointer = new ExampleObject("image.jpg"); }_C++_Memory_Pointers - Fatal编程技术网

类方法/函数内的指针 我想知道C++是如何处理指针在类方法或函数内部创建的“对象”的内存的。 例如类example的example方法 void Example::methodExample() { ExampleObject *pointer = new ExampleObject("image.jpg"); }

类方法/函数内的指针 我想知道C++是如何处理指针在类方法或函数内部创建的“对象”的内存的。 例如类example的example方法 void Example::methodExample() { ExampleObject *pointer = new ExampleObject("image.jpg"); },c++,memory,pointers,C++,Memory,Pointers,我应该删除它还是自动删除? 对不起,如果我的问题很愚蠢,但我是初学者:你应该 delete pointer; 当你不再需要它的时候。指针超出了函数末尾的作用域,但内存仍在堆上分配。您应该 delete pointer; 当你不再需要它的时候。指针在函数末尾超出范围,但内存仍在堆上分配。您有两个选项 如果您使用原始指针,就像您在示例中使用的那样,必须手动删除使用新建创建的对象 如果不这样做,则表示已创建内存泄漏 void Example::methodExample() { Example

我应该删除它还是自动删除? 对不起,如果我的问题很愚蠢,但我是初学者:你应该

delete pointer;
当你不再需要它的时候。指针超出了函数末尾的作用域,但内存仍在堆上分配。

您应该

delete pointer;

当你不再需要它的时候。指针在函数末尾超出范围,但内存仍在堆上分配。

您有两个选项

如果您使用原始指针,就像您在示例中使用的那样,必须手动
删除使用
新建
创建的对象

如果不这样做,则表示已创建内存泄漏

void Example::methodExample()
{
  ExampleObject *pointer = new ExampleObject("image.jpg");

  // Stuff

  delete pointer;
}
或者您可以使用智能指针,例如
boost::scoped_ptr
或C++11的
std::unique_ptr

删除这些对象时,这些对象将自动删除其指向的内容

有些人(比如我)会说这种方法是首选的,因为即使抛出异常且未到达函数结尾,您的
ExampleObject
也会被正确删除

void Example::methodExample()
{
  boost::scoped_ptr<ExampleObject> pointer( new ExampleObject("image.jpg") );

  // Stuff

}
void示例::methodExample()
{
作用域指针(新的ExampleObject(“image.jpg”);
//东西
}

您有两个选择

如果您使用原始指针,就像您在示例中使用的那样,必须手动
删除使用
新建
创建的对象

如果不这样做,则表示已创建内存泄漏

void Example::methodExample()
{
  ExampleObject *pointer = new ExampleObject("image.jpg");

  // Stuff

  delete pointer;
}
或者您可以使用智能指针,例如
boost::scoped_ptr
或C++11的
std::unique_ptr

删除这些对象时,这些对象将自动删除其指向的内容

有些人(比如我)会说这种方法是首选的,因为即使抛出异常且未到达函数结尾,您的
ExampleObject
也会被正确删除

void Example::methodExample()
{
  boost::scoped_ptr<ExampleObject> pointer( new ExampleObject("image.jpg") );

  // Stuff

}
void示例::methodExample()
{
作用域指针(新的ExampleObject(“image.jpg”);
//东西
}

> p>在现代C++中,你不应该做你自己的内存管理。使用<代码> UNQuyJPPT/<代码>或 SimeDePPTR < /C> >,当它们超出范围时,将自动删除指针。

< P>在现代C++中,您根本不应该进行自己的内存管理。使用
unique_ptr
scoped_ptr
,当指针超出范围时,将自动删除指针。

如果对象在函数范围内,则正确的构造是根本不使用指针,而是使用自动对象,应该这样创建

ExampleObject example("image.jpg"); 
例如,您可以在
if
构造中使用指针,此时
else
条件不会构造对象,然后您希望稍后使用该对象

在这种情况下,请使用自动指针对象,如果可用,最好是唯一的\u ptr;如果不可用,请使用boost::scoped \u ptr
,但即使是不推荐使用的std::auto \u ptr也比原始对象好。例如:

std::unique_ptr<ExampleObject> example;
if( usingAnExample )
{
     example.reset( new ExampleObject("image.jpg") );
}
else
{
   // do stuff
}
// I still need example here if it was created
std::唯一的ptr示例;
如果(使用示例)
{
重置(新的ExampleObject(“image.jpg”);
}
其他的
{
//做事
}
//我仍然需要这里的例子,如果它是创建的

如果对象的作用域在函数中,那么正确的构造是根本不使用指针,而是使用自动对象,应该这样创建

ExampleObject example("image.jpg"); 
例如,您可以在
if
构造中使用指针,此时
else
条件不会构造对象,然后您希望稍后使用该对象

在这种情况下,请使用自动指针对象,如果可用,最好是唯一的\u ptr;如果不可用,请使用boost::scoped \u ptr
,但即使是不推荐使用的std::auto \u ptr也比原始对象好。例如:

std::unique_ptr<ExampleObject> example;
if( usingAnExample )
{
     example.reset( new ExampleObject("image.jpg") );
}
else
{
   // do stuff
}
// I still need example here if it was created
std::唯一的ptr示例;
如果(使用示例)
{
重置(新的ExampleObject(“image.jpg”);
}
其他的
{
//做事
}
//我仍然需要这里的例子,如果它是创建的

我认为处理原始指针(如您举例所示)的合适方法是将指针存储为类的成员。然后,您可以使用任何想要的方法为该指针分配内存,并让类的析构函数释放内存。大致如下:

class Example
{
public:
   Example();
   ~Example();

   void methodExample();

private:
   ExampleObject* pointer;
};

void Example::Example()
: pointer(NULL)
{
}

void Example::~Example()
{
  if (pointer) // release memory only if it was allocated
    delete pointer;
}


void Example::methodExample()
{
  pointer = new ExampleObject("image.jpg");
  // add a safety check to verify if the allocation was successful 
}

我认为处理原始指针的合适方法(如您举例所示)是将指针存储为类的成员。然后,您可以使用任何想要的方法为该指针分配内存,并让类的析构函数释放内存。大致如下:

class Example
{
public:
   Example();
   ~Example();

   void methodExample();

private:
   ExampleObject* pointer;
};

void Example::Example()
: pointer(NULL)
{
}

void Example::~Example()
{
  if (pointer) // release memory only if it was allocated
    delete pointer;
}


void Example::methodExample()
{
  pointer = new ExampleObject("image.jpg");
  // add a safety check to verify if the allocation was successful 
}

如果这确实是OP想要的,那么(a)在构造函数中将
指针初始化为null;(b) 修复
methodExample
以分配给成员;(c) 根据添加非默认复制构造函数和复制赋值运算符。但是我强烈怀疑OP只是想要一个自动对象,没有新的
;(b) 修复
methodExample
以分配给成员;(c) 根据添加非默认复制构造函数和复制赋值运算符。但是我强烈怀疑OP只是想要一个自动对象,没有新的
。那么我可以完全忘记原始指针而开始使用boost智能指针吗?我的意见是,你可以选择在任何时候都使用智能指针,只有在必要时才使用原始指针。根据您的工作内容,您可能永远不需要使用原始指针。@fex:您应该忘记原始指针。除非你在C++库上工作,否则你永远不需要它们。