[英]what does it mean to have these kind of errors in C?
this is my solution这是我的解决方案
#include <stdlib.h>
#include <string.h>
char* deletemultiple(const char* str) {
if (str == NULL) {
return NULL;
}
size_t length = strlen(str);
if (length == 0) {
return str;
}
length = length + 1u;
char* s = malloc(length);
if (s == NULL) {
return NULL;
}
size_t j = 0;
for (size_t i = 0; s[i] != 0; i++) {
if (str[i] != str[i+1]) {
s[j++] = str[i];
}
}
s = realloc(s, j+1);
if (s == NULL) {
return NULL;
}
return s;
}
int main(void) {
char str[] = "";
char* s = deletemultiple(str);
free(s);
return 0;
}
it's a program that delete multiple characters (ie adjacent characters) and return a new allocated string without multiple adjacent characters.这是一个删除多个字符(即相邻字符)并返回没有多个相邻字符的新分配字符串的程序。 This solution works only for strings with length != 0;
此解决方案仅适用于长度为 != 0 的字符串; but if string is "" (ie an empty string), when I free the memory, I have an error that blocks the program.
但是如果字符串是“”(即一个空字符串),当我释放内存时,我会遇到阻止程序的错误。 (ie A breakpoint instruction (__debugbreak() statement or a similar call) was executed ).
(即执行了断点指令(__debugbreak() 语句或类似调用))。
Moreover, I have 2 warnings: one warning reminds me that "realloc might return a null pointer", but I already know that and in fact I've used an if-block to check if it's either null or not.此外,我有 2 个警告:一个警告提醒我“realloc 可能返回一个空指针”,但我已经知道这一点,事实上我已经使用了一个 if 块来检查它是否为空。
and the other warning is about "reading invalid data from s, and it's related to this for-loop block:另一个警告是关于“从 s 读取无效数据,它与这个 for-loop 块有关:
for (size_t i = 0; s[i] != 0; i++) {
if (str[i] != str[i+1]) {
s[j++] = str[i];
}
}
can somebody explains what does these errors/warnings mean, and how to solve them?有人可以解释这些错误/警告的含义,以及如何解决它们吗? in similar exercises, if I'll have these errors again, what should I do?
在类似的练习中,如果我再次出现这些错误,我该怎么办?
If the length is zero, your function is returning its argument, so your code is the same as:如果长度为零,则您的函数将返回其参数,因此您的代码与以下内容相同:
int main(void) {
char str[] = "";
char* s = str;
free(s); /* ERROR: This is wrong!! */
return 0;
}
but you cannot free(str)
, because str
was not allocated with malloc
.但你不能
free(str)
,因为str
没有分配malloc
。
Just remove the special case check against length zero, and fix your bug so that you allocate length + 1
to leave space for the null terminatorr.只需删除针对长度为零的特殊情况检查,并修复您的错误,以便分配
length + 1
为空终止符留出空间。
If you passed an empty string then this empty string is returned如果您传递了一个空字符串,则返回此空字符串
char* deletemultiple(const char* str) {
if (str == NULL) {
return NULL;
}
size_t length = strlen(str);
if (length == 0) {
return str;
}
//...
So you may not call the function free
for such a pointer as you are doing所以你可能不会像你正在做的那样为这样的指针
free
调用函数
char* s = deletemultiple(str);
free(s);
It means that the function interface is broken.就是功能接口坏了。 You need to return a dynamically allocated empty string.
您需要返回一个动态分配的空字符串。
The dynamically allocated array s
does not contain a string because you forgot to append it with the zero terminating character '\0'
动态分配的数组
s
不包含字符串,因为您忘记在它后面附加零终止字符'\0'
If the memory reallocation was not successful如果内存重新分配不成功
s = realloc(s, j+1);
if (s == NULL) {
return NULL;
}
then the function produces a memory leak because the address of the previously allocated memory that will not be freed in this case will be lost due to reassigning the pointer s
.然后该函数会产生内存泄漏,因为在这种情况下不会释放的先前分配的内存的地址将由于重新分配指针
s
而丢失。 You need to use an intermediate pointer as for example例如,您需要使用中间指针
char *tmp = realloc(s, j+1);
if (tmp == NULL) {
free( s );
return NULL;
}
The approach when the memory is allocated twice is unsafe.两次分配内存的方法是不安全的。 Also it is not required to check whether
str
is equal to NULL
.也不需要检查
str
是否等于NULL
。
if (str == NULL) {
return NULL;
}
String functions support the convention according to which if the user passes a null pointer then functions have undefined behavior.字符串函数支持约定,根据该约定,如果用户传递空指针,则函数具有未定义的行为。
The function can look for example the following way as shown in the demonstration program below.该功能可以如下面的演示程序所示,例如下面的方式。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * delete_multiple( const char *s )
{
size_t n = strlen( s );
for ( const char *p = s; *p++; )
{
if ( *p == *( p - 1 ) ) --n;
}
char *result = malloc( n + 1 );
if ( result != NULL )
{
char *p = result;
for ( *p = *s; *s++; )
{
if ( *s != *p ) *++p = *s;
}
}
return result;
}
int main( void )
{
char *p = delete_multiple( "" );
printf( "\"%s\"\n", p );
free( p );
p = delete_multiple( "a" );
printf( "\"%s\"\n", p );
free( p );
p = delete_multiple( "aa" );
printf( "\"%s\"\n", p );
free( p );
p = delete_multiple( "aaa" );
printf( "\"%s\"\n", p );
free( p );
p = delete_multiple( "abaca" );
printf( "\"%s\"\n", p );
free( p );
}
The program output is程序输出为
""
"a"
"a"
"a"
"abaca"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.