简体   繁体   English

处理列表时应该使用指针吗?

[英]Should I use a pointer when dealing with lists?

Pointers should be used when an object should live longer, even when it goes out of scope, right? 当对象的寿命更长,甚至超出范围时,也应该使用指针,对吗?

Here I just create a Movie m; 在这里,我只是创建一个电影m; . It will be created on the stack and automatically deleted when it goes out of scope. 它会在堆栈上创建,并在超出范围时自动删除。

//In some header file
typedef struct{
Qstring name;
int id;
//...
} Movie ;


QList<Movie> movieList; //It's the same as the standard list of c++. 

//In a function somewhere else
void doSomething(/*...*/)
{
//Do something...
Movie m = { /* ... */ };
movieList.push_back( m );
}

A list takes a constant reference of type T as argument. 列表将类型T的常量引用作为参数。 So it's the address, right? 是地址,对吧? But when Movie m; 但是当Movie m; goes out of scope it will be deleted. 超出范围将被删除。 Somehow the item in the list stays. 列表中的项目以某种方式保留下来。

However, my question is should I use a pointer and create the Movie m; 但是,我的问题是我应该使用指针并创建Movie m; on the heap or is this fine? 在堆上还是这样? What is better style? 什么是更好的风格?

"Pointers should be used when an object should live longer, even when it goes out of scope, right?" “当对象的寿命更长,甚至超出范围时,也应该使用指针,对吗?”

Although it is true that local objects with automatic storage duration are automatically destructed when the execution goes out of scope, it does not necessarily mean that dynamically allocated object would live longer. 尽管确实会在执行超出范围时自动破坏具有自动存储持续时间的本地对象,但这并不一定意味着动态分配的对象的寿命更长。

In your application, you have global variable: 在您的应用程序中,您具有全局变量:

QList<Movie> movieList;

representing container that lives during the whole time of your program's execution. 表示在程序执行的整个过程中一直存在的容器。

"Somehow the item in the list stays" “以某种方式保留列表中的项目”

When you create an object of type Movie and push it into your container: 创建Movie类型的对象并将其推入容器时:

Movie m;
...
movieList.push_back(m);

the copy of this object is stored in the list. 该对象的副本存储在列表中。 Copies are being created in different situations, mainly when you pass or return by value, yet in most of cases it will have no negative impact on performance of your program. 副本是在不同的情况下创建的,主要是当您按值传递或返回值时,但是在大多数情况下,它不会对程序的性能产生负面影响。 There are many optimization techniques that your compiler will use to elide copies... sometimes so effectively that passing/returning by value might become even faster than passing by reference. 编译器将使用许多优化技术来提取副本...有时效果如此之好,以至于按值传递/返回可能比按引用传递更快。

" should I use a pointer and create the Movie m; on the heap? " 我应该使用指针在堆上创建Movie m;吗?

NO! 没有! Once you start allocating objects dynamically, you take the responsibility for memory management on yourself and many things might become far more complicated that they could be otherwise... including possible and very frequent (usually due to imperfections in error handling) problems with memory leaks , problems with dangling pointers that might make you fall into the pits of undefined behavior and many other kinds of unpleasant stuff. 一旦开始动态分配对象,您就要对自己的内存进行管理,并且许多事情可能变得比原本要复杂得多……包括可能且非常频繁(通常是由于错误处理的不完善)而导致的内存泄漏问题, 悬空指针的问题可能使您陷入未定义行为和许多其他不愉快事物的陷阱。

Avoid dynamic memory allocation always when it is possible. 尽可能避免动态内存分配。 Use objects with automatic storage duration, learn about RAII idiom and take delight in following it. 使用具有自动存储期限的对象,了解RAII习惯用法并乐于跟随它。

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

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