gpt4 book ai didi

c - 为什么一个困惑的字符会打印(C)?

转载 作者:行者123 更新时间:2023-11-30 15:22:51 25 4
gpt4 key购买 nike

我正在编写一个 strovr 函数,它可以查找两个字符串的所有交集。由于某种原因,在处理 1 个字符长的交集时,它偶尔会打印损坏的字符。

例如,使用字符串 Hey BrotherHey Bro,它会生成:

Bro
He
o
rL?z?//MANGLED
e

我认为这与指针/内存问题有关。

这是我的代码。

#import <stdio.h>
#import <stdlib.h>
#import <string.h>

char **strovr(char c1[], char cmp[], int *len_response)
{
int lenc1 = strlen(c1), lencmp = strlen(cmp);
int len_big = lenc1 > lencmp ? lenc1 : lencmp;
int len_small = lenc1 < lencmp ? lenc1 : lencmp;
char **both[2] = {&c1, &cmp};
char **returned = (char **)malloc(sizeof(char *));
int size_returned = 0;
int indlonger = lenc1 > lencmp ? 0 : 1, indshorter = !indlonger, i = len_small;
char A, B;

while (i >=(0-len_big))
{
int i_incr = i >= 0 ? i : 0;
int j = i >= 0 ? 0 : 0-i;
int until = i >= 0 ? len_small : i +len_big;
char A = (*both[indshorter])[i_incr];
B = (*both[indlonger])[j];

int currently_in = 0;
int ind_begin;

while (i_incr <until)
{

if (B==A && B!=32 && A!=32)
{
if (!currently_in)
{
ind_begin = j;
currently_in = 1;
}

if (((*both[indshorter])[i_incr+1] != (*both[indlonger])[j+1]) || (i_incr+1 == len_small) )
{
currently_in = 0;
int ind_end = j;
char *match = malloc(sizeof(char) * (ind_end-ind_begin));
if (match != NULL ) //DIAGNOSIS SHOULD START HERE.
{
int curr_ind = 0;
if (ind_end-ind_begin > 0)
{
for (int c = ind_begin; c<=ind_end; c++)
{
match[curr_ind] = (*both[indlonger])[c];
curr_ind++;
}
// printf("%s ",match);
}
else
{
match[0] = A;
// match[1] = "c";
printf("%s: STRLEN(%lu)\n",match,strlen(match));
}

if (size_returned == 0) {}
else returned = realloc(returned, sizeof(char*)*(size_returned+1));

if (match == NULL)
{
printf("\nError::Return string not properly initialized\n");
exit(1);
}

returned[size_returned] = match;
size_returned++;
}
}

}

i_incr++;
j++;
A = (*both[indshorter])[i_incr];
B = (*both[indlonger])[j];
}
i--;
}
*len_response = size_returned;
return returned;
}


int main()
{
int resp;
char **intersections = strovr("Hey Brother", "Hello Bro",&resp);

printf("\nThe intersection of Hey Brother and Hello Bro is:\n");
for(int i = 0; i < resp; i++)
{
for (int j = 0; j < strlen(intersections[i]); j++)
{
printf("%c",intersections[i][j]);
}
printf("\n");
}
}

最佳答案

c 中的数组从 0 开始索引,所以这样

malloc(sizeof(char) * (ind_end-ind_begin));

与此结合

for (int c = ind_begin; c<=ind_end; c++)

是问题所在,因为一旦c == ind_end,您就会调用未定义的行为,而如果ind_begin != 0,您将有更多机会发生奇怪的事情。

我还假设您没有为终止 '\0' 分配空间,c 中的每个字符串都需要 N + 1 个字节来存储,N 个字符 + 一个特殊的标记值 '\0',像 strlen() 这样的函数需要这个值。

您的代码还存在许多潜在的未定义行为,因为在将指针传递给不会像 strlen() 这样进行检查的函数之前,您从不检查指针是否为 NULL

你的realloc()也是错误的,你没有检查它是否返回NULL,我认为它没有返回,但你必须检查,调用 realloc() 的安全方法是这样

void *dontOverwriteOldPointer;
dontOverwriteOldPointer = realloc(oldPointer, newSize);
if (dontOverwriteOldPointer == NULL)
{
free(oldPointer);
return goodThatWeDid_not_OverwriteTheOldPointer_ReturnErrorValue();
}
oldPointer = dontOverwriteOldPointer;

另一个常见错误是这个

for (int j = 0; j < strlen(intersections[i]); j++)
/* ^ not good */

你不应该像这样调用strlen(),因为字符串长度没有存储在任何地方,所以strlen()计算每次迭代的长度,因此你的程序的性能会受到影响,你应该像这样存储值

size_t length = strlen(intersections[i]);
for (int j = 0 ; j < length ; j++)

你甚至不需要这个,只要理解 '\0' 的事情,你就可以做到这一点

for (int j = 0 ; intersections[i][j] != '\0' ; j++)
printf("%c", intersections[i][j]);

这正是为什么如果数组末尾没有 '\0'strlen() 会导致未定义的行为,printf 也会如此("%s", someString);.

关于c - 为什么一个困惑的字符会打印(C)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29014734/

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