问题:
struct s
{
char * name;
std::vector<Class A *> myVector;
}
struct s *str = (struct s *) malloc (sizeof (struct s));
原因出在最后一句。
因为vector 的capacity 是 auto reserve 的,即动态变化的,所以为 str 申请一个固定size 的heap space 是不合适的。之后的vector 操作会导致 segmentation fault。
解决方法之一:
struct s str;
条款14:使用reserve来避免不必要的重新分配
http://stl.winterxy.com/html/item_14.html
条款17:使用“交换技巧”来修整过剩容量
http://stl.winterxy.com/html/item_17.html
class Contestant {...};
vector<Contestant> contestants;
vector<Contestant>(contestants).swap(contestants); //收缩到合适
vector<Contestant>().swap(v); // 清除v而且最小化它的容量
struct s
{
char * name;
std::vector<Class A *> myVector;
}
struct s *str = (struct s *) malloc (sizeof (struct s));
原因出在最后一句。
因为vector 的capacity 是 auto reserve 的,即动态变化的,所以为 str 申请一个固定size 的heap space 是不合适的。之后的vector 操作会导致 segmentation fault。
解决方法之一:
struct s str;
条款14:使用reserve来避免不必要的重新分配
http://stl.winterxy.com/html/item_14.html
条款17:使用“交换技巧”来修整过剩容量
http://stl.winterxy.com/html/item_17.html
class Contestant {...};
vector<Contestant> contestants;
vector<Contestant>(contestants).swap(contestants); //收缩到合适
vector<Contestant>().swap(v); // 清除v而且最小化它的容量