释放内存时堆损坏
我有一个类如下
struct CliHandler {
CliHandler(int argc, char** argv);
~CliHandler();
int doWork();
int argc_;
char** argv_;
private:
CliHandler(const CliHandler&){}
CliHandler& operator=(const CliHandler&){}
};
//构造函数
CliHandler::CliHandler(int argc,
char** argv) {
//set command line parameters
argc_ = argc;
argv_ = (char**) malloc(argc_ * sizeof(char*));
for(int i=0; i<argc_; ++i)
{
std::cout<<sizeof(argv[i]);
argv_[i] = (char*) malloc(strlen(argv[i]) *
sizeof(char));
StrCpy(argv_[i], argv[i]);
} }
// 析构函数
CliHandler::~CliHandler() {
for(int i=0; i<argc_; ++i)
free(argv_[i]);
free(argv_); }
当我调试时,我收到错误“检测到堆损坏。CRT 检测到应用程序在堆缓冲区结束后写入内存。”我的问题 id“我到底在哪里犯了错误我该如何解决它”。我正在使用 Visual stdio 2008。
编辑:我做了类似的事情来添加 1
argv_[i] = (字符*) malloc(strlen(argv[i] + 1) * sizeof(char));
这很糟糕,因为它将指针 argv[i] 加一。我的同事指出了这个微妙的问题。应该是
argv_[i] = (字符*) malloc( (strlen(argv[i]) + 1) * sizeof(char));
I have a class as follows
struct CliHandler {
CliHandler(int argc, char** argv);
~CliHandler();
int doWork();
int argc_;
char** argv_;
private:
CliHandler(const CliHandler&){}
CliHandler& operator=(const CliHandler&){}
};
//Constructor
CliHandler::CliHandler(int argc,
char** argv) {
//set command line parameters
argc_ = argc;
argv_ = (char**) malloc(argc_ * sizeof(char*));
for(int i=0; i<argc_; ++i)
{
std::cout<<sizeof(argv[i]);
argv_[i] = (char*) malloc(strlen(argv[i]) *
sizeof(char));
StrCpy(argv_[i], argv[i]);
} }
// destructor
CliHandler::~CliHandler() {
for(int i=0; i<argc_; ++i)
free(argv_[i]);
free(argv_); }
While I debug, I get an error " Heap corruption detected. CRT detected that application wrote to memory after end of heap buffer. " My question id "Where exactly am i making a mistake ? How do I fix it". I am using visual stdio 2008.
Edit:I did something like this to add 1
argv_[i] = (char*)
malloc(strlen(argv[i] + 1) *
sizeof(char));
Which is terrible as it increments the pointer argv[i] by one. My co-worker pointed out that subtle issue. It should be
argv_[i] = (char*)
malloc( (strlen(argv[i]) + 1) *
sizeof(char));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要复制 C 字符串,则需要比 C 字符串的 strlen 多分配一个字符。这是因为 strlen 不计算终止空字符。
You need to allocate one character more than the strlen of a C-string if you want to copy it. This is because strlen does not count the termination null-character.
请使用 strdup() - 它会分配适量的内存并为您复制字符。
Please use strdup() - it allocates the right amount of memory and copies characters for you.
如果 StrCpy 与 strcpy 类似,它将比 strlen() 返回的内容多写入一个字节,以零终止字符串。
If StrCpy is anything like strcpy, it will write one byte more than strlen() returns, to zero terminate the string.
将代码更改为:
这是因为您的 StrCpy 可能会破坏您的内存。您还必须考虑终止 nul 字节,前提是您的 StrCpy 与标准 strcpy 一样工作(它必须如此才能有用,最好只使用标准 strcpy() ,除非您有充分的理由不这样做)。
sizeof(char) 根据定义为 1,因此也可以省略。
Change the code to:
It's because your StrCpy likely trashes your memory. You have to account for the terminating nul byte as well, provided your StrCpy works like the standard strcpy (which it has to in order to be useful, better just use the standard strcpy() unless you have a good reason not to).
sizeof(char) is by definition 1, so that can be omitted too.