gpt4 book ai didi

c - 为什么这个 C 程序会在这个位置出现段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:42 25 4
gpt4 key购买 nike

我正在编写一个 C 程序,基本上可以接受一个句子并计算每个单词在其中出现的次数。我制作了一个精简版,可以准确地重现这个问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct testStrut{
char * string;
int uses;
}word;

void grow();

int main(){
int i;
int count = 1;
word ** words;

words = (word **) malloc(count * sizeof(word *));
words[0] = (word *) malloc(sizeof(word));

for(i = 0; i < 10; i++){
printf("Re-looping i: %d \n", i);
printf("words[0]->string = %s \n", words[0]->string);
grow("TEST", words, &count);
}

printf("Done.");
return 0;
}

void grow(char * str, word ** words, int * count){
word ** tmp;

tmp = realloc(words, (*count) * sizeof(word *));

if(tmp){
tmp[(*count)-1] = malloc(sizeof(word));
tmp[(*count)-1]->string = malloc(strlen(str)+1); /*+1 for null terminator as pointed out */
strcpy(tmp[(*count)-1]->string, str);
tmp[(*count)-1]->uses = 1;
words = tmp;
(*count)++;
} else{
printf("Failure to allocate. \n");
exit(0);
}
printf("Count: %d and word[0] %s \n", (*count), str);
}

以及运行的输出:

Re-looping i: 0
words[0]->string = (null)
Count: 2 and word[0] TEST
Re-looping i: 1
words[0]->string = TEST
Count: 3 and word[0] TEST
Re-looping i: 2
words[0]->string = TEST
Count: 4 and word[0] TEST
Re-looping i: 3
words[0]->string = TEST
Count: 5 and word[0] TEST /*Prints it fine? */
Re-looping i: 4
Segmentation fault (core dumped) /*Suddenly unable to print it? */

我不明白为什么在结束 grow 函数和重新执行循环之间,words[0]->str 的值突然丢失了。有什么我想念的吗?

[我知道我应该释放我 malloc 的任何东西。我也意识到我的方法原型(prototype)不正确,但我只是想制作一个快速程序来证明我的问题]

最佳答案

在 for 循环的第一次迭代中,以下行正在访问未初始化的内存。

printf("words[0]->string = %s \n", words[0]->string);

你也声明过

void grow();

但实际的签名即

void grow(char * str, word ** words, int * count)

您首先需要在该行之前调用 grow ,即。您也是 realloc 并假设 main 中的指针 words 指向原始指针。

试试这个。我简化了一点...

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct testStrut{
char * string;
int uses;
} word;

void grow(const char *str, word *words, int count);

int main(){
int i;
word * words;
printf("sizeof word == %zu\n", sizeof(word));
assert(sizeof(word) == 16);
words = malloc(sizeof(word));
for(i = 0; i < 10; i++){
printf("Re-looping i: %d \n", i);
grow("TEST", words, i);
printf("words[0]->string = %s \n", words[0].string);
}
printf("Done.");
return 0;
}
void grow(const char * str, word *words, int count){
word ** tmp;
int idx = count - 1;
printf("size == %zu\n", count * sizeof(word));
tmp = realloc(words, count * sizeof(word));
size_t str_len = strlen(str);
if(tmp != NULL) {
tmp[idx] = malloc(sizeof(word*));
tmp[idx]->string = malloc(str_len + 1);
strcpy(tmp[idx]->string, str);
tmp[idx]->string[4] = '\0';
tmp[idx]->uses = 1;
} else{
printf("Failure to allocate. \n");
exit(0);
}
printf("Count: %d and word[0] %s \n", count, str);
}

关于c - 为什么这个 C 程序会在这个位置出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467502/

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