列表和列表元素,在哪里存储? [英] List and list elements, where are stored?

查看:127
本文介绍了列表和列表元素,在哪里存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这段代码:

  #include< list> 
(void)someFunction(void){
list< int> l;
l.push_back(1);
}




  • 列表的元素存储在哪里?堆栈?

  • 这个函数可以返回列表吗?
  • 已编辑如果我将函数声明为列表,则函数可以返回具有问题的列表吗?


示例(返回列表):

  #include< list> 
list< int> someFunction(void){
list< int> l;
l.push_back(1);
}

...
l2 = someFunction();
l2.push_back(2);


解决方案


的列表存储?堆栈? Heap?


列表元素存储在堆上。你可以看到这下面你的调试器上push_back方法调用。最容易看到的是存储对象而不是POD类型,并记录构造函数。你需要一个复制构造函数,因为它们被复制。分配发生在模板参数分配器,您可以指定或不指定它将与默认堆分配。


如何


你可以用栈中的push_back元素来检查这个值:

  std :: list< int> my_list; 
int a = 10
my_list.push_back(a);
a = 11;
assert(* my_list.begin()== 10);




此函数可以返回列表?


在C ++中,有两种方法来传递数据:按引用或按值。如果你的函数看起来像这样,

  list< int& func()
{
list< int> res;
res.push_back(10);
return res;
}

然后你传递列表的值,这意味着编译器调用列表的复制构造函数,它也复制列表中的所有值。随着函数返回,在复制列表之后,将调用res列表的析构函数,释放其所有元素。但是,如果您这样做:

  list< int& func()
{
list< int> res;
res.push_back(10);
return res;
}

您的代码将失败,因为您返回对res列表,它将在其范围的末尾被销毁,因此您的引用将无效。



第一个解决方案的问题可能是性能。你也可以这样做,而不像下面的复制构造函数的调用:

  void func(list< int& 
{
res.push_back(10);
}

list< int> list_to_fill;
func(list_to_fill);

在这种情况下,没有复制,它应该更快,因为只有一个列表创建。 p>

Given this piece of code:

#include <list>
(void) someFunction(void) {
    list <int> l;
    l.push_back(1);
}

  • Where are the elements of the list stored? Stack? Heap?
  • How can I do to empirically check that values are in stack or heap?
  • This function can returns the list? Edited If I declare function as list, can function returns list with out problems?

Sample (returning list):

#include <list>
list<int> someFunction(void) {
    list <int> l;
    l.push_back(1);
}

...
l2 = someFunction();
l2.push_back(2);

解决方案

Where are the elements of the list stored? Stack? Heap?

List elements are stored on the heap. You can see this following your debugger down on the push_back method call. The easiest to see it is to store objects rather than POD type, and log the constructor. You'll need a copy constructor as they are get copied. The allocation happens with the template argument allocator, which you can specify or without specifying it it would go with the default heap allocation.

How can I do to empirically check that values are in stack or heap?

You can check this with push_back elements from the stack:

std::list<int> my_list;
int a = 10;
my_list.push_back(a);
a = 11;
assert(*my_list.begin() == 10);

This function can returns the list?

In C++ there are two ways to pass data around: by reference or by value. If you're function looks like this,

list<int> func()
{
  list<int> res;
  res.push_back(10);
  return res;
}

then you're passing the list by value, which means that the compiler will call the list's copy constructor which also copies all the values in the list. As the function returns, after it copies the list, the "res" list's destructor will be called, releasing all its elements. However if you do this:

list<int>& func()
{
      list<int> res;
      res.push_back(10);
      return res;
}

You're code will fail as you return the reference to your "res" list, which will be already destroyed at the end of its scope, so you're reference will be invalid.

The problem with the first solution can be performance. You can also do that without the call of the copy constructor like this:

void func(list<int>& res)
{
  res.push_back(10);
}

list<int> list_to_fill;
func(list_to_fill);

In this case, there's no copying and it should be faster as there's only one list creation.

这篇关于列表和列表元素,在哪里存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆