void replace(char *str) {
unsigned int len = 0;
unsigned int no_of_spaces = 0;
while (*str) {
if ((char)*str == SPACE)
no_of_spaces++;
str++;
len++;
}
unsigned int new_len = len + 2 * no_of_spaces;
str = (char*) realloc(str, new_len * sizeof(char));
str[new_len] = '
void replace(char *str) {
unsigned int len = 0;
unsigned int no_of_spaces = 0;
while (*str) {
if ((char)*str == SPACE)
no_of_spaces++;
str++;
len++;
}
unsigned int new_len = len + 2 * no_of_spaces;
str = (char*) realloc(str, new_len * sizeof(char));
str[new_len] = '\0';
}
I use function like replace("random string");.
我使用像replace(“随机字符串”)这样的函数;
Here I am trying to increase the size of the string so that spaces can be replaced with another string. For this I need to count the no of spaces and also get the length of the original string. I have been able to do that.
这里我尝试增加字符串的大小,这样可以用另一个字符串替换空格。为此,我需要计算空格的no,并获得原始字符串的长度。我已经做到了。
For resizing I am using realloc but when I run it, it gives Aborted (core dumped)?
对于调整大小,我使用realloc,但是当我运行它时,它会放弃(核心转储)?
4 个解决方案
#1
7
Was your original string allocated using malloc? or realloc? Perhaps you are trying to increase the size of a static string (a string literal):
您的原始字符串是使用malloc分配的吗?还是realloc ?也许您正在尝试增加静态字符串(字符串文本)的大小:
char sStatic[256]; // cannot realloc
char *sNonStatic = NULL; // can / must realloc
replace("random string") // cannot realloc within replace
EDIT: after reading your comment, you should take a copy of your incoming string, increase the size, then output a copy/ new string. You cannot increase the size of a constant string (a string literal).
编辑:在阅读你的评论后,你应该复制你的输入字符串,增加大小,然后输出一个拷贝/新字符串。不能增加常量字符串(字符串文本)的大小。
#2
5
The only pointers that can be passed to realloc are null pointers and those that have been returned by calloc, malloc or realloc previously!
唯一可以传递给realloc的指针是空指针,以及之前由calloc、malloc或realloc返回的指针!
This is important because you mentioned that you've called your function like replace("random string")... Is "random string" a null pointer, or returned by one of those *alloc functions? No. Perhaps you meant to use strdup or something (e.g. char *foo = strdup("random string"); replace(foo); free(foo);)? strdup is a POSIX function (e.g. not C-standard like the *alloc functions) but it should return something returned by the *alloc functions.
这很重要,因为您提到您已经调用了函数replace(“random string”)……“随机字符串”是一个空指针,还是由一个*alloc函数返回?不。也许你打算使用strdup或其他东西(例如char *foo = strdup(“随机字符串”);替换(foo);免费(foo);)?strdup是POSIX函数(例如,不是c标准的*alloc函数),但它应该返回由*alloc函数返回的东西。
Following this code:
下面这段代码:
unsigned int new_len = len + 2 * no_of_spaces;
str = (char*) realloc(str, new_len * sizeof(char)); /* NOTE there's a potential memory leak
* when realloc returns NULL here, though
* that's the least of your problems */
... you must check str to ensure realloc succeeded, and only then the only valid indexes for str are between 0 and new_len - 1. This is either a null pointer dereference or a buffer overflow:
…您必须检查str以确保realloc成功,只有这样,str的唯一有效索引才在0和new_len - 1之间。这要么是空指针引用,要么是缓冲区溢出:
str[new_len] = '\0';
Perhaps you meant the following:
也许你的意思是:
size_t new_len = len + 2 * no_of_spaces;
void *temp = realloc(str, new_len + 1); /* <--- NOTE +1 HERE! */
if (temp == NULL) {
/* XXX: Bomb out due to allocation failure */
}
str = temp;
... and now the valid indexes are between 0 and new_len + 1 - 1, so this is valid:
…现在有效的索引在0和new_len + 1 - 1之间,所以这是有效的:
str[new_len] = '\0';
#3
1
This line is incorrect, since valid indices range from 0 .. new_len - 1:
这一行是不正确的,因为有效索引从0开始。new_len - 1:
str[new_len] = '\0';
It should probably be:
它应该是:
str[new_len - 1] = '\0';
You also have a couple of other potential problems:
你还有其他一些潜在的问题:
realloc can return NULL - you should check for this
realloc可以返回NULL——您应该检查一下
in the case that realloc fails you lose your original str pointer and get a memory leak - you should use a temp pointer for the result, test this for NULL, and then only if realloc has succeeded should you set str equal to temp:
在realloc失败的情况下,您丢失了原来的str指针并获得了内存泄漏——您应该使用一个temp指针来测试结果,测试这个为NULL,然后只有当realloc成功时,您应该设置str等于temp:
char * temp = realloc(str, new_len);
if (temp == NULL)
{
// handle error here...
}
else
{
str = temp; // success...
str[new_len - 1] = '\0';
}
- not a bug as such, but you have a lot of unnecessary casts which are potentially dangerous as they can masks bugs that would otherwise generate compiler errors or warnings. You can safely remove all the casts in your code above.
- 不是这样的错误,但是您有许多不必要的强制类型转换,这些类型转换可能是危险的,因为它们可以掩盖错误,否则会生成编译器错误或警告。您可以安全地删除上面代码中的所有类型转换。
#4
0
You also move the pointer
你也移动指针
while (*str) {
if ((char)*str == SPACE)
no_of_spaces++;
str++;
len++;
}
and when you are at the end you try to reallocate it. But you are already away from where the array is. Use a temp variable here. And as the others said. The string is hopefully created with malloc and not as an array.
当你在最后的时候,你试着重新分配它。但是你已经不在数组的位置了。在这里使用一个临时变量。就像其他人说的那样。字符串应该是用malloc创建的,而不是作为数组。
And str[new_len] = '\0'; is out of bounds.
和str(new_len)= ' \ 0 ';是界外。
';
}
void replace(char *str) {
unsigned int len