Should I use a pointer when dealing with lists?

Davlog

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; . 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. So it's the address, right? But when 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; on the heap or is this fine? What is better style?

LihO

"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 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?"

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

When should I use val x = fn as opposed to fun x

来自分类Dev

Should I use copy or strong with arrays?

来自分类Dev

what port number should I use in mongodb

来自分类Dev

Spark - Which language should I use?

来自分类Dev

Should I use `__setattr__`, a property or...?

来自分类Dev

How should I use vendor in Go 1.6?

来自分类Dev

python use lazy assignment when passing lists to methods

来自分类Dev

Problems running JerseyTest when dealing with HttpServletResponse

来自分类Dev

Is this a Chef/ohai bug when dealing with FQDNs?

来自分类Dev

What happens when I call "delete" on an uninitialized pointer in C++?

来自分类Dev

I want to change the mouse pointer from text to pointer when particular values in a dropdown are selected

来自分类Dev

Should I use asynchronous methods within a background thread?

来自分类Dev

Should I use big switch statements in JavaScript without performance problems?

来自分类Dev

Why should I use Using block to connect to the EntityFramework Model?

来自分类Dev

What version manager should I use to manage multiple Scala versions?

来自分类Dev

Entity Framework 6 , Should I use repository pattern?

来自分类Dev

When should I set layer.shouldRasterize to YES

来自分类Dev

what things i should know when using mailx command

来自分类Dev

ObjectDisposedException when I try to use an Image source

来自分类Dev

Alternative to eval() when dealing with d3 animation queues

来自分类Dev

Should a void* pointer passed in by a callback be deleted

来自分类Dev

Why EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when callback pointer to function?

来自分类Dev

Should I use Browserify or Webpack for lazy loading of dependencies in angular 1.x

来自分类Dev

Moving away from Repository pattern. How should I use DBContext?

来自分类Dev

Is it okay to use negative pixels in css margin value or should I do something else?

来自分类Dev

NodeJS & Socket.IO: Emit a request event and get the response, when/where should I bind the listener?

来自分类Dev

UITableView is being reset when I use Custom Class

来自分类Dev

Safely storing credentials when I need to retrieve the password for use

来自分类Dev

Should I always complete a TaskCompletionSource?

Related 相关文章

  1. 1

    When should I use val x = fn as opposed to fun x

  2. 2

    Should I use copy or strong with arrays?

  3. 3

    what port number should I use in mongodb

  4. 4

    Spark - Which language should I use?

  5. 5

    Should I use `__setattr__`, a property or...?

  6. 6

    How should I use vendor in Go 1.6?

  7. 7

    python use lazy assignment when passing lists to methods

  8. 8

    Problems running JerseyTest when dealing with HttpServletResponse

  9. 9

    Is this a Chef/ohai bug when dealing with FQDNs?

  10. 10

    What happens when I call "delete" on an uninitialized pointer in C++?

  11. 11

    I want to change the mouse pointer from text to pointer when particular values in a dropdown are selected

  12. 12

    Should I use asynchronous methods within a background thread?

  13. 13

    Should I use big switch statements in JavaScript without performance problems?

  14. 14

    Why should I use Using block to connect to the EntityFramework Model?

  15. 15

    What version manager should I use to manage multiple Scala versions?

  16. 16

    Entity Framework 6 , Should I use repository pattern?

  17. 17

    When should I set layer.shouldRasterize to YES

  18. 18

    what things i should know when using mailx command

  19. 19

    ObjectDisposedException when I try to use an Image source

  20. 20

    Alternative to eval() when dealing with d3 animation queues

  21. 21

    Should a void* pointer passed in by a callback be deleted

  22. 22

    Why EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when callback pointer to function?

  23. 23

    Should I use Browserify or Webpack for lazy loading of dependencies in angular 1.x

  24. 24

    Moving away from Repository pattern. How should I use DBContext?

  25. 25

    Is it okay to use negative pixels in css margin value or should I do something else?

  26. 26

    NodeJS & Socket.IO: Emit a request event and get the response, when/where should I bind the listener?

  27. 27

    UITableView is being reset when I use Custom Class

  28. 28

    Safely storing credentials when I need to retrieve the password for use

  29. 29

    Should I always complete a TaskCompletionSource?

热门标签

归档