zoukankan      html  css  js  c++  java
  • how does vector work?

    Currently, I am finding a bug related to meory allocated by vector. I have some findings about the mechanism of vector.

    0. vector maintains 3 pointers: _M_start, _M_finish, and _M_end_of_storage. At first, vector is empty, i.e. _M_start= _M_finish= _M_end_of_storage=NULL.

    invariant: elements are stored in [_M_start, _M_finish), while its storage/capacity is [_M_start, _M_end_of_storage).

    1. A vector allocates a large number of memory, n*sizeof(Obj), by calling reserve(n). If its capacity, (char*)_M_end_of_storage-(char*)_M_start,  is less than n*sizeof(Obj), it will malloc another memory, and copy data before free its original storage.

    2. Vector's iterator: a pointer in c++

    vector's begin() points to _M_start; vector's end() points to _M_finish.

    3.Vector's capacity is equal to (char*)_M_end_of_storage - (char*)_M_start

    4. Vector's clear call destructor on each elements in [_M_start, _M_finish), not free memory at all.

    5. ~Vector() frees memory by calling ~_Vector_base().

    For more info, please refer to /usr/include/c++/4.1.2/bits/stl_vector.h

  • 相关阅读:
    Linux免密码登陆
    Java事务的概念
    SpringMVC访问静态资源
    堆排序
    滚动视图 UIScrollView
    HTML数据解析
    同步下载 异步下载
    项目中的小心得(以后慢慢积累起来)
    xcode 中 UIbutton图片的放置
    NSobject的基本方法使用
  • 原文地址:https://www.cnblogs.com/Torstan/p/3104333.html
Copyright © 2011-2022 走看看