我正在写一个strovr
函数,它找到两个字符串的所有交叉点。出于某种原因,它在处理1个字符长的交叉点时偶尔会打印出受损的字符。
例如,使用字符串Hey Brother
和Hey 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");
}
}
答案 0 :(得分:1)
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
字符+特殊的标记值{ {1}},像'\0'
这样的函数在那里期望这个值。
您的代码也有很多潜在的未定义行为,因为在将它们传递给不会像strlen()
那样检查的函数之前,您永远不会检查NULL
的指针。
您的strlen()
也是错误的,并且您没有检查它是否返回realloc()
,我认为它没有,但您必须检查,一种安全的方式来调用{{1}这是
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]);
,strlen()
也是如此。