【发布时间】:2016-04-17 07:15:06
【问题描述】:
在我的函数中替换一个子字符串。如果输入的子字符串比原来的长,它会将输入字符串的一部分移出,为输入的子字符串腾出空间。
我了解这会导致未定义的行为。我认为我应该能够使用 realloc() 分配所需的空间,但没有成功。
我尝试在 memmove() 之前添加这个:
char *newspc = (char*)realloc(in,len+sublen);
in = newspc;
这是一个合理的策略吗?为该操作腾出空间的正确方法是什么?
这是没有使用 realloc() 的程序:
#include <iostream>
#include <string>
#include <string.h>
void replc(char* in, char* subin);
int main()
{
char stmt[] = "replacing this $string ok";
std::cout << stmt << "\n";
replc(stmt, "longerstring"); //<<<4 characters longer breaks the program
std::cout << stmt << "\n";
}
void replc(char* in, char* subin){
uint8_t len = strlen(in);
uint8_t aftok = strchr(strchr(in, '$'), ' ')-in;
uint8_t dollar = strchr(in, '$')-in;
uint8_t tklen = aftok - dollar;
uint8_t sublen = strlen(subin);
if(sublen <= tklen){
//enough room for substring
memmove(in+aftok-(tklen-sublen), in+aftok, (tklen-sublen)+1);
memcpy(in+dollar, subin, sublen);
in[len-(tklen-sublen)] = '\0';
}
else{
//not enough room for substring
// memory allocation should take place here?
memmove(in+aftok+(sublen-tklen), in+aftok, (sublen-tklen)+1);
memcpy(in+dollar, subin, sublen);
in[len+(sublen-tklen)] = '\0';
}
}
【问题讨论】:
-
我认为字符串之外的任何索引都是未分配的内存。如果我将输入子字符串的长度增加几个字符,程序确实会失败。 -- 未定义的行为,简单明了。越界访问数组,任何事情都可能发生,包括“工作”。
-
我根据该链接更改了问题。
-
为什么不直接使用
std::string呢?我知道你最终会想出“魔法代码”来解决这个问题,因为它所做的只是用缓冲区和指针做一些事情。但是,除了修复错误之外,您能从中获得什么?如果您珍惜时间,请使用std::string。哎呀,你甚至包括<string>,但没有使用任何东西。 -
您只能在
malloc(或null)之前分配的空间上调用realloc -
...或
realloc,或calloc。
标签: c++ c string memory-management memmove