为什么我会出现分段故障?当我使用SANITIZE=ADDRESS进行编译时,我得到了一个释放后堆使用,这是我不太理解的(原因). 我在地址xyz上得到了释放后的堆使用.
阅读尺寸为8英寸的……Test.c:22(打印部件的行[i])
...位于此处释放的8字节区域内的0个字节(字符串c:175,这是reallocarray行)
...以前在这里分配的(在main test.c:9中,它是char**Parts=calloc.线路)
此示例编译独立的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum EXPLODE_FLAGS {
NO_FLAGS=0,
FLAG_TRIM=1, // trim each line of the output
};
typedef enum RESULT {
E_SUCCESS=0, // not a failure but a SUCCESS
E_ERROR=1, // failure due to generic error
E_ARGS=2, // failed due to arguments
E_MALLOC=3
} RESULT;
enum EXP_RESULT {
EXP_ERROR=-E_ERROR, // generic error
EXP_ARGS=-E_ARGS, // generic error with the arguments
EXP_MALLOC=-E_MALLOC, // failure due to generic error
EXP_SEP=-4, // separator is null
EXP_INPUT=-5, // input is a null pointer
EXP_OUTPUT=-6, // output is a null pointer
};
int str_explode(char *input, char **parts, const char separator) {
int partCounter = 0;
int currentPartLength = 0;
char *currentPart = NULL;
// Check for input validity
if (!input) return EXP_INPUT;
if (!parts) return EXP_OUTPUT;
if (separator == '\0') return EXP_SEP;
char *start = input;
char *currentPartStart = input;
char *end = input + strlen(input);
fprintf(stdout,"Inside the function\n");
for (char *thischar = start; thischar <= end; thischar++) {
if (*thischar == separator || *thischar == '\0') {
printf("Inside check; current char is: %c\n",*thischar);
// Allocate memory for the length of the current part + null terminator
currentPart = calloc(1, currentPartLength + 1);
if (!currentPart) {
// Use goto for cleanup
goto cleanup;
}
// Copy the current part into the allocated memory
if (currentPartLength > 0) {
strncpy(currentPart, currentPartStart, currentPartLength);
currentPart[currentPartLength] = '\0'; // Null-terminate the string
} else {
currentPart[0] = '\0'; // Empty string for the case of consecutive separators
}
// Reallocate memory for another char pointer
parts=reallocarray(parts,partCounter+1,sizeof(char*));
if (!parts) {
// Use goto for cleanup
goto cleanup_current_part;
}
printf("About to add current part (%s) to the pile\n",currentPart);
// Add the new string part
parts[partCounter++] = currentPart;
printf("About to check current part from the pile: %s\n",parts[partCounter-1]);
// Reset variables for the next part
currentPart = NULL;
currentPartStart = thischar + 1; // Skip the separator
currentPartLength = 0;
if('\0'==*thischar)
break;
} else {
++currentPartLength;
}
}
free(currentPart);
return partCounter;
// Label for cleanup
cleanup_current_part:
fprintf(stderr,"Unable to allocate memory for another part\n");
free(currentPart);
cleanup:
fprintf(stderr,"Unable to allocate memory for current part\n");
// Free previously allocated memory before returning error
for (int i = 0; i < partCounter; i++) {
free(parts[i]);
}
free(parts);
return EXP_MALLOC;
}
int main(void) {
char *input = "apple;orange;banana;grape";
char **parts = calloc(1,1*sizeof(char*));
parts[0]="\0";
int partCount = str_explode(input, parts, ';');
if (partCount < 0) {
printf("Error code #%d\n", -partCount);
return 1;
}
printf("Original string: %s\n", input);
printf("Number of parts: %d\n", partCount);
for (int i = 0; i < partCount; i++) {
printf("About to print part #%d:\n",i+1);
printf("Part %d: %s\n", i + 1, parts[i]);
free(parts[i]);
}
free(parts);
return 0;
}
请记住,我不是一个有经验的C程序员.我有一个指针的工作知识,但我只是不能把我的头周围我在这里做错了. 这个小程序的目的是提高我对C中字符数组的理解.