使用 C++ 复制而不是 memcpy 的分段错误
segmentation fault using c++ copy instead of memcpy
我正在使用C++复制算法来复制字符串文字,(而不是memcpy),但是我遇到了分段错误,我不知道为什么。 这是代码:
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[]) {
// if using copy with regular pointers, there
// is no need to get an output iterator, ex:
char* some_string = "this is a long stringn";
size_t some_string_len = strlen(some_string) + 1;
char* str_copy = new char(some_string_len);
copy( some_string, some_string + some_string_len, str_copy);
printf("%s", str_copy);
delete str_copy;
return 0;
}
修复 :
char* str_copy = new char[some_string_len];
^ notice square bracket
使用释放内存:
delete [] str_copy;
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- C++ - 程序接收信号SIGSEGV,分段错误.In msvcrt!memcpy () (C:\Windows\S
- memcpy局部静态变量时出现分段错误
- 带有structs和memcpy的C++分段错误
- 使用 C++ 复制而不是 memcpy 的分段错误
- Memcpy导致分段故障
- std::copy的分段错误,memcpy 工作