delete and delete[] on oop
发布日期:2021-06-30 22:17:00 浏览次数:2 分类:技术文章

本文共 4827 字,大约阅读时间需要 16 分钟。

前言

前段时间,在工程中用delete[]删除单个对象指针,引起崩溃。被同事找出来了。

我一直以为工程中的那个类发生了越界,引起的崩溃, 而不是因为delete[]引起的崩溃.
在忙别的,没深究。
今天想起这事,做个实验(linux and windows),果然单个对象指针,只能用delete来删除,否则崩溃。
以前没注意过这个点 :)

实验

// @file main.cpp// @brief test delete obj and delete obj array, on debian#include 
#include
#include
#ifndef WIN32#include
#endif // #ifndef WIN32#include "const_define.h"#include "class_to_test.h"void case_delete_one_obj_as_delete_ary();void case_delete_obj_ary();int main(int argc, char** argv){ printf(">> main()\n"); case_delete_one_obj_as_delete_ary(); case_delete_obj_ary(); return EXIT_SUCCESS;}/** run resultroot@debian750devmin:/home/dev# ./test_cpp_delete >> main()tip is one objclass_to_test::~class_to_test() : one objp_obj_ary[0]'s tip is class index = 0p_obj_ary[1]'s tip is class index = 1p_obj_ary[2]'s tip is class index = 2class_to_test::~class_to_test() : class index = 2class_to_test::~class_to_test() : class index = 1class_to_test::~class_to_test() : class index = 0*/void case_delete_one_obj_as_delete_ary(){ class_to_test* p_obj = NULL; p_obj = new class_to_test(); if (NULL != p_obj) { p_obj->save_tip("one obj"); printf("tip is %s\n", p_obj->get_tip());#ifndef WIN32 SAFE_DEL_OBJ(p_obj); // delete ONE object, only use delete! else crash#else SAFE_DEL_OBJ(p_obj); // on windows, same too.#endif // #ifndef WIN32 }}void case_delete_obj_ary(){ int i = 0; class_to_test* p_obj_ary = NULL; int i_ary_cnt = 3; char sz_buf[0x100] = {
'\0'}; p_obj_ary = new class_to_test[i_ary_cnt]; if (NULL != p_obj_ary) { for (i = 0; i < i_ary_cnt; i++) { sprintf(sz_buf, "class index = %d", i); (p_obj_ary + i)->save_tip(sz_buf); printf("p_obj_ary[%d]'s tip is %s\n", i, (p_obj_ary + i)->get_tip()); } SAFE_DEL_OBJ_ARY(p_obj_ary); }}
// @file const_define.h// @brief ...#ifndef __CONST_DEFINE_H__#define __CONST_DEFINE_H__#ifndef SAFE_DEL_OBJ#define SAFE_DEL_OBJ(p) \{ \    if (NULL != (p)) { \        delete (p); \        (p) = NULL; \    } \}#endif // #ifndef SAFE_DEL_OBJ#ifndef SAFE_DEL_OBJ_ARY#define SAFE_DEL_OBJ_ARY(p) \{ \    if (NULL != (p)) { \        delete[] (p); \        (p) = NULL; \    } \}#endif // #ifndef SAFE_DEL_OBJ#endif // #define __CONST_DEFINE_H__
// @file class_to_test.h// @brief ...#ifndef __CLASS_TO_TEST_H__#define __CLASS_TO_TEST_H__class class_to_test {public:    class_to_test();    virtual ~class_to_test();    void save_tip(const char* psz_tip);    const char* get_tip();private:    void init();    void uninit();    void delete_tip();private:    char* m_psz_buf;    int m_i_len_buf;};#endif // #define __CLASS_TO_TEST_H__
// @file class_to_test.cpp#include 
#include
#include
#ifndef WIN32#include
#endif // #ifndef WIN32#include "class_to_test.h"class_to_test::class_to_test(){ init();}class_to_test::~class_to_test(){ printf("class_to_test::~class_to_test() : %s\n", (NULL != this->get_tip()) ? this->get_tip() : "NULL"); uninit();}void class_to_test::save_tip(const char* psz_tip){ delete_tip(); if (NULL != psz_tip) { m_i_len_buf = strlen(psz_tip); m_psz_buf = new char[m_i_len_buf + 1]; m_psz_buf[m_i_len_buf] = '\0'; strcpy(m_psz_buf, psz_tip); }}const char* class_to_test::get_tip(){ return m_psz_buf; }void class_to_test::init(){ m_psz_buf = NULL; m_i_len_buf = 0;}void class_to_test::uninit(){ delete_tip();}void class_to_test::delete_tip(){ if (NULL != m_psz_buf) { delete [] m_psz_buf; m_psz_buf = NULL; } m_i_len_buf = 0;}
# ==============================================================================# @file makefile# ==============================================================================BIN = test_cpp_deleteLINE80 = --------------------------------------------------------------------------------CC = g++ -std=c++98CFLAGS = -Wall -gINC = -I.LIBS = -lstdc++ -pthreadLIBPATH = ./ /usr/local/libDEPEND_CODE_DIR = ./empty_dir \DEPEND_CODE_SRC = $(shell find $(DEPEND_CODE_DIR) -name '*.cpp')DEPEND_CODE_OBJ = $(DEPEND_CODE_SRC:.cpp=.o)ROOT_CODE_SRC = $(shell find ./ -name '*.cpp')ROOT_CODE_OBJ = $(ROOT_CODE_SRC:.cpp=.o)SUB_CODE_DIR = ./empty_dirSUB_CODE_SRC = $(shell find $(SUB_CODE_DIR) -name '*.cpp')SUB_CODE_OBJ = $(SUB_CODE_SRC:.cpp=.o)clean:    clear    @echo $(LINE80)    @echo make clean    rm -f $(BIN) $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)all:$(BIN)    @echo $(LINE80)    @echo make all    chmod 777 $(BIN)    find . -name $(BIN)$(BIN) : $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)    $(CC) $(CFLAGS) -o $@ $^.cpp.o:    $(CC) -c $(CFLAGS) $^ -o $@ $(INC) -L$(LIBPATH) $(LIBS)rebuild:    make clean    @echo $(LINE80)    make all

转载地址:https://lostspeed.blog.csdn.net/article/details/79287760 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:linux shell script : check user exist
下一篇:debian7, debian8's syslog config, open all log level

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月22日 03时14分41秒