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 (i.e adjacent characters) and return a new allocated string without multiple adjacent characters. This solution works only for strings with length != 0; but if string is "" (i.e an empty string), when I free the memory, I have an error that blocks the program. (i.e A breakpoint instruction (__debugbreak() statement or a similar call) was executed ).
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.
and the other warning is about "reading invalid data from s, and it’s related to this for-loop block:
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?
>Solution :
If you passed an empty string that 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
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'
If the memory reallocation was not successfull
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. You need to use an intermediate pointer as for example
char *tmp = realloc(s, j+1);
if (tmp == NULL) {
free( s );
return NULL;
}