我之前已在Code Review中实施了一些建议。我还通过使用指针改进了我的代码。但是,squeezed_str++
下面的地址增量部分有什么问题?看来地址没有递增。请指教。
PS。 substring()函数正在工作。 :)
char *squeeze (char *str, int start_index, int end_index, char *ref_str) {
char *substr;
substr = malloc (sizeof (*substr));
if (substr == NULL) {
printf ("Unable to allocate memory.\n");
exit (EXIT_FAILURE);
}
char *squeezed_str;
squeezed_str = malloc (sizeof (*squeezed_str));
if (squeezed_str == NULL) {
printf ("Unable to allocate memory!\n");
exit (EXIT_FAILURE);
}
substr = substring (str, start_index, end_index);
int substr_len = strlen (substr);
int refstr_len = strlen (ref_str);
char chr1, chr2; chr1 = chr2 = '\0';
for (int i = 0; i < substr_len; i++) {
chr1 = *(substr+i);
for (int j = 0; j < refstr_len; j++) {
chr2 = *(ref_str + j);
if (chr1 == chr2) {
break;
}
}
if (chr1 != chr2) {
*squeezed_str = *(substr+i);
squeezed_str++;
}
}
return squeezed_str;
} /* end of squeeze() */
答案 0 :(得分:2)
内联建议的一些改进,未经测试:
char *squeeze (char *str, int start_index, int end_index, char *ref_str) {
char *substr = substring (str, start_index, end_index);
//do not malloc here!, you are doing an assignment later on, so memory leak
//infact might as well move assignment right here
char *squeezed_str;
squeezed_str = malloc (strlen(str)+1); //+ 1 one for null terminator
if (squeezed_str == NULL) {
printf ("Unable to allocate memory!\n");
exit (EXIT_FAILURE);
}
int substr_len = strlen (substr);
int refstr_len = strlen (ref_str);
int squeezeStrIdx = 0;
for (int i = 0; i < substr_len; i++) {
char chr1 = substr[i]; //using index accessing is nicer
//moved the scope of the variable in
char chr2 = '\0'; //reduced variable scope, benefit is now resets each loop
for (int j = 0; j < refstr_len; j++) {
chr2 = ref_str[j];
if (chr1 == chr2) {
break;
}
}
if (chr1 != chr2) {
squeezed_str[squeezeStrIdx] = chr1;
squeezeStrIdx++; //if you modify squeezed_str,
//how do you expect to return it at end of method?
}
}
//this is to null terminate the squeezed_str
squeezed_str[squeezeStrIdx] = '\0';
return squeezed_str;
} /* end of squeeze() */
答案 1 :(得分:1)
完全有缺陷的是完全修改squeezed_str
。这是malloc
返回的地址。如果您修改它,以后将无法free
该存储区域。您构建了内存泄漏,甚至更糟糕的是,当您最后调用free
时,您发生了崩溃。
编辑:
char *squeeze (const char *str, size_t start_index, size_t end_index, const char *ref_str)
/* You don't modify `str` and `ref_str` so it is a good idea to make it visible in the signature of the function, whence the const qualifiers */
{
char *substr;
/* Snip the memory leak */
char *squeezed_str = malloc (sizeof (*squeezed_str));
if (!squeezed_str) {
fprintf (stderr, "Unable to allocate memory!\n"); /* Error are to be written on stderr */
exit (EXIT_FAILURE); /* Exitting the app in a function is not good style */
}
substr = substring (str, start_index, end_index);
size_t substr_len = strlen (substr); /* strlen returns size_t not int */
size_t refstr_len = strlen (ref_str);
char chr1 = 0, chr2 = 0;
char *squeezed_copy = squeezed_str; /* We use a second pointer to not lose the malloc'ed area */
for (size_t i = 0; i < substr_len; i++) {
chr1 = substr[i]; /* This syntax is simpler (you'll see when you have nested structures */
for (size_t j = 0; j < refstr_len; j++) {
chr2 = ref_str[j];
if (chr1 == chr2) {
break;
}
}
if (chr1 != chr2)
*squeezed_copy++ = substr[i];
}
/* Add the NUL sentinel */
*squeezed_copy++ = 0;
return squeezed_str;
} /* end of squeeze() */
这就是说,我没有检查算法是否符合你的意图。在预感中,我会说使用strstr
会更好。