I have a function which I give a char** input. Inside the function I am calling realloc. I am calling the function multiple times without any issues, but it always crashes at the same point with
"Exception has occurred. - Unknown signal".
The function is implemented as follows:
int look_and_say(char** input, char** output) {
if (input == NULL) {
return -1; // Handle null input
}
size_t len = strlen(*input);
if (len == 0) {
return -1; // Return -1 as sign for empty input string
printf("Input string empty\n");
}
if( len *2 +1 > MAX_INPUT_SIZE){
return -5; // Handle input too large
printf("Input too large\n");
}
printf("Size to allocate: %zu __ ", len * 2 + 1);
char* temp_storage = NULL;
temp_storage = (char*)realloc(*output, len * 2 + 1); //always crashes here when allocating 6365 bytes
if (temp_storage == NULL) {
return -2; // Handle memory allocation failure
}
*output = temp_storage;
int count = 1;
int current = *input[0];
int new_len = 0;
for(int i = 1; i <= len; i++){
if((*input)[i] == (*input)[i-1]){
count++;
} else {
new_len += sprintf(*output + new_len, "%d%c", count, (*input)[i-1]);
count = 1; // Reset count for the new character
current = (*input)[i];
}
}
// append terminating character
(*output)[new_len++] = '\0';
return 1; // Return 1 as sign for successful processing
}
I tried it first with allocating directly enough for the end result without reallocating, which works, but I am not satisfied with that solution. I also tried compiling with -Wall and -Wextra without success
Edit 1:
- I have removed the unused variable current
- I have removed the typecast For some reason the code crashes a bit later now.
I addition here is my main():
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_SIZE 20000000 //20'000'000
int main(){
// ######################################Task 1######################################
char *input = malloc(12 * sizeof(char)); // Allocate memory for input
if (input == NULL) {
printf("Memory allocation failed\n");
return 1;
}
strcpy(input, "1321131112");
char* result = NULL; // Initialize result to NULL
for(int i = 0; i < 25; i++){
printf("Input length: %zu __ ", strlen(input));
int status = look_and_say(&input, &result);
printf("Output length: %zu \n", strlen(result));
if (status < 0) {
printf("Error processing input: num: %d status:%d\n", i, status);
return 1;
}
strcpy(input, result); // Update input for the next iteration
//free(result); // Free the previous result to avoid memory leaks
}
//printf("Result: %s\n", result);
printf("Final result length: %zu\n", strlen(result));
free(result); // Free the allocated memory for the result
free(input); // Free the allocated memory for the input
return 0;
}
Edit 2:
- added #include and #define
- regarding for loop: I am using i <= len to compare to the final null character on purpose, as in this iteration I then add the final required numbers
*outputdoes not contains an already allocated buffer when you call realloc (the null pointer is the alone other valid case). Use valgrind to check your memory management (and more)countbecomes a double-digit number.strcpy(input, result); // Update input for the next iterationstrlen(input) * 2 + 1characters for theresult. Then copy this possible very much longer string into the12bytes allocated forinput. What happens ifstrlen(result)is equal to or larger than12bytes?