s*****k 发帖数: 604 | 1 比如一个object array, 要再加长点,
C里面用realloc,但是realloc不调用构造函数 | c*****t 发帖数: 1879 | 2 Can't use realloc, because if resizing isn't successful, the old
memory would be freed before you have any chances calling dtors.
So, you will just have to use malloc/new to create a new memory
space, and then call placement new with copy constructor on each
and individual array element.
You can read std::vector code yourself to see how that gets
implemented.
【在 s*****k 的大作中提到】 : 比如一个object array, 要再加长点, : C里面用realloc,但是realloc不调用构造函数
| a****l 发帖数: 8211 | 3 the idea of realloc is against the physical design of digital computers, so
it is something you should avoid using in the first place.
【在 s*****k 的大作中提到】 : 比如一个object array, 要再加长点, : C里面用realloc,但是realloc不调用构造函数
| s*****k 发帖数: 604 | 4 读了stl_alloc.h,读不懂啊
【在 c*****t 的大作中提到】 : Can't use realloc, because if resizing isn't successful, the old : memory would be freed before you have any chances calling dtors. : So, you will just have to use malloc/new to create a new memory : space, and then call placement new with copy constructor on each : and individual array element. : You can read std::vector code yourself to see how that gets : implemented.
|
|