gpt4 book ai didi

C++ vector.push_back() 使 m 程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:27 27 4
gpt4 key购买 nike

我正在编写一个 shell 程序并为传送带编写了一个测试。每次我启动它时,它都会在第三个 vector.push_back() 上崩溃,没有产生任何异常并写了很多难以理解的单词。请告诉我我做错了什么。

#include <stdio.h>
#include <vector>
#include "Program.cpp"
#include "Conveyor.cpp"
#include <stdlib.h>

using namespace std;

int main(){
vector <Program> programs;
char *argv[2];
argv[0] = "./increaser";
argv[1] = NULL;
Program program1(argv[0], argv);
Program program2(argv[0], argv);
Program program3(argv[0], argv);

printf("conveyor_test - PUSH 1\n");
programs.push_back(program1);
printf("conveyor_test - PUSH 2\n");
programs.push_back(program2);
printf("conveyor_test - PUSH 3\n");
try{
programs.push_back(program3);
printf("conveyor_test - PUSHED 3\n");
}
catch (...){
printf("Wild exception was caught.\n");
exit(1);
}
printf("conveyor_test - pushed programs into vector\n");
fflush(stdout);

printf("---------START-----------\n");
fflush(stdout);
conveyor(programs);
printf("---------END-------------\n");
return 0;
}

这是写入输出的内容:

conveyor_test - PUSH 1
conveyor_test - PUSH 2
conveyor_test - PUSH 3
*** glibc detected *** ./conveyor_test: free(): invalid pointer: 0x0000000001c75040 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f357c463b96]
./conveyor_test[0x401806]
./conveyor_test[0x40388a]
./conveyor_test[0x4034c0]
./conveyor_test[0x402e57]
./conveyor_test[0x4024c5]
./conveyor_test[0x402718]
./conveyor_test[0x401dce]
./conveyor_test[0x40125d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f357c40676d]
./conveyor_test[0x400d69]
======= Memory map: ========
00400000-00406000 r-xp 00000000 00:15 265077 /home/crabman/Dropbox/Projects/C++/shell/conveyor_test
00605000-00606000 r--p 00005000 00:15 265077 /home/crabman/Dropbox/Projects/C++/shell/conveyor_test
00606000-00607000 rw-p 00006000 00:15 265077 /home/crabman/Dropbox/Projects/C++/shell/conveyor_test
01c75000-01c96000 rw-p 00000000 00:00 0 [heap]
7f357c0e9000-7f357c1e4000 r-xp 00000000 08:01 790941 /lib/x86_64-linux-gnu/libm-2.15.so
7f357c1e4000-7f357c3e3000 ---p 000fb000 08:01 790941 /lib/x86_64-linux-gnu/libm-2.15.so
7f357c3e3000-7f357c3e4000 r--p 000fa000 08:01 790941 /lib/x86_64-linux-gnu/libm-2.15.so
7f357c3e4000-7f357c3e5000 rw-p 000fb000 08:01 790941 /lib/x86_64-linux-gnu/libm-2.15.so
7f357c3e5000-7f357c59a000 r-xp 00000000 08:01 790899 /lib/x86_64-linux-gnu/libc-2.15.so
7f357c59a000-7f357c799000 ---p 001b5000 08:01 790899 /lib/x86_64-linux-gnu/libc-2.15.so
7f357c799000-7f357c79d000 r--p 001b4000 08:01 790899 /lib/x86_64-linux-gnu/libc-2.15.so
7f357c79d000-7f357c79f000 rw-p 001b8000 08:01 790899 /lib/x86_64-linux-gnu/libc-2.15.so
7f357c79f000-7f357c7a4000 rw-p 00000000 00:00 0
7f357c7a4000-7f357c7b9000 r-xp 00000000 08:01 790924 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c7b9000-7f357c9b8000 ---p 00015000 08:01 790924 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c9b8000-7f357c9b9000 r--p 00014000 08:01 790924 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c9b9000-7f357c9ba000 rw-p 00015000 08:01 790924 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c9ba000-7f357ca9f000 r-xp 00000000 08:01 536749 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357ca9f000-7f357cc9e000 ---p 000e5000 08:01 536749 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357cc9e000-7f357cca6000 r--p 000e4000 08:01 536749 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357cca6000-7f357cca8000 rw-p 000ec000 08:01 536749 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357cca8000-7f357ccbd000 rw-p 00000000 00:00 0
7f357ccbd000-7f357ccdf000 r-xp 00000000 08:01 790877 /lib/x86_64-linux-gnu/ld-2.15.so
7f357cec2000-7f357cec7000 rw-p 00000000 00:00 0
7f357cedb000-7f357cedf000 rw-p 00000000 00:00 0
7f357cedf000-7f357cee0000 r--p 00022000 08:01 790877 /lib/x86_64-linux-gnu/ld-2.15.so
7f357cee0000-7f357cee2000 rw-p 00023000 08:01 790877 /lib/x86_64-linux-gnu/ld-2.15.so
7fff2df40000-7fff2df61000 rw-p 00000000 00:00 0 [stack]
7fff2df6e000-7fff2df6f000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted

程序类重要代码如下:

class Program{
private:
char *path;
int argc;
char **argv;
public:
Program(char *path, char **argv) {
printf("Program::Program start\n");
// вычисляем argc
argc = 0;
while (argv[argc] != NULL){
argc++;
}
if (argc < 1){
throw "Program::Program - argc < 1";
}
printf("\targc is calculated\n");
// копируем path
if (path == NULL){
throw "Program::Program - path is NULL";
}
this -> path = new char[strlen(path) + 1];
strcpy(this -> path, path);
printf("\tpath is calculated\n");
// копируем argv
if (argv == NULL){
throw "Program::Program - argv is NULL";
}
this -> argv = new char*[argc];
for (int i = 0; i < argc; i++){
this -> argv[i] = new char[strlen(argv[i]) + 1];
strcpy(this -> argv[i], argv[i]);
}
printf("Program::Program end\n");
}
~Program(){
// printf("Program::~Program start\n");
delete[] path;
// printf("\tpath deleted\n");
size_t size = sizeof(argv) / sizeof(argv[0]);
// printf("\tsizeof(argv) = %d\n", size);
for (size_t i = 0; i < size; i++){
delete[] argv[i];
// printf("\t\targv[%d] deleted\n", i);
}
// printf("\tall argv[i] deleted\n");
delete[] argv;
// printf("Program::~Program end\n");
}

最佳答案

当您使用push_back 时,实际放入 vector 中的是一个拷贝。此拷贝是浅拷贝,意味着编译器只复制指针,而不是它们指向的内容。

实际上创建了几个拷贝,当其中一个拷贝超出范围时,将调用拷贝析构函数,从而释放指针中的内存。但是由于所有拷贝都有指向完全相同内存的指针,该内存现在被标记为空闲,因此您无法访问它们。

在您的问题的评论部分中有指向“三原则”的链接,这意味着如果您有一个析构函数、一个复制构造函数或一个赋值运算符,那么您应该实现所有三个。这是为了确保在复制实例时执行深复制,同时复制实际数据。

关于C++ vector.push_back() 使 m 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com