gpt4 book ai didi

c - 为什么动态调整字符串会导致崩溃?

转载 作者:行者123 更新时间:2023-12-04 11:35:22 25 4
gpt4 key购买 nike

考虑代码:

char *word = NULL;                                      // Pointer at buffered string.
int size = 0; // Size of buffered string.
int index = 0; // Write index.

char c; // Next character read from file.

FILE *file = fopen(fileDir, "r");
if (file)
{
while ((c = getc(file)) != EOF)
{
printf("Current index: %d, size: %d, Word: %s\n", index, size, word);
if (isValidChar(c))
{
appendChar(c, &word, &size, &index);
}
else if (word) // Any non-valid char is end of word. If (pointer) word is not null, we can process word.
{
// Processing parsed word.
size = 0; // Reset buffer size.
index = 0; // Reset buffer index.
free(word); // Free memory.
word = NULL; // Nullify word.
// Next word will be read
}
}
}
fclose(file);

/* Appends c to string, resizes string, inceremnts index. */
void appendChar(char c, char **string, int *size, int *index)
{
printf("CALL\n");
if (*size <= *index) // Resize buffer.
{
*size += 1; // Words are mostly 1-3 chars, that's why I use +1.
char *newString = realloc(*string, *size); // Reallocate memory.

printf("REALLOC\n");

if (!newString) // Out of memory?
{
printf("[ERROR] Failed to append character to buffered string.");
return;
}

*string = newString;
printf("ASSIGN\n");
}

*string[*index] = c;
printf("SET\n");
(*index)++;
printf("RET\n");
}

对于输入:

血腥

输出:

Current index: 0, size: 0, Word: <null>
CALL
REALLOC
ASSIGN
SET
RET
Current index: 1, size: 1, Word: B** // Where * means "some random char" since I am NOT saving additional '\0'. I don't need to, I have my size/index.
CALL
REALLOC
ASSIGN
CRASH!!!

所以基本上 - *string[*index] = 'B' 有效,当索引是第一个字母时,它会在第二个字母处崩溃。为什么?我可能搞砸了分配或指针,我真的不知道(新手):C

谢谢!

编辑我还想问 - 我的代码还有什么问题吗?

最佳答案

这个表达式是错误的:

*string[*index] = c;

因为 [] 的优先级高于 * 的,代码试图将双指针 string 解释为一个数组指针。当 *index 为零时,您将获得正确的地址,因此第一次迭代成功纯属巧合。

您可以通过使用括号强制执行正确的操作顺序来解决此问题:

(*string)[*index] = c;

关于c - 为什么动态调整字符串会导致崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com