char *string = (char *) malloc(sizeof(char) * sz);
在此之前的代码->void insert_word(word *root, char string1[], int linenumber) { int sz=strlen(string1)<=MAX_WORD_LENGTH?strlen(string1):MAX_WORD_LENGTH;代码块3具有整个上下文
有时malloc()在使用时返回一个填充的内存位置。
困扰我的是,这不是随机的。(这个程序包括从文件中提取单词并将它们传递给这个函数。对于同一个词,函数行为(特别是malloc())是不同的。
对于输入string1=0x7fffffffdf10 "lol" root=BST,sz获取3的值。
malloc()分配给string的值是0x55555555c510 "\340\305UUUU",为什么malloc没有指向空内存位置?(这不是随机行为,它是可预测的和可重复的)此外,由于某种原因,这个循环运行了无限长的时间。
while(strcmp(string1,string)!=0)
    {
        free(string);
        string=NULL;
        string = (char *) malloc(sizeof(char) * sz);
        strncpy(string,string1,sz);
    }更多RELAVANT代码
#define MAX_WORD_LENGTH 203)功能
void insert_word(word *root, char string1[], int linenumber) {
    int sz=strlen(string1)<=MAX_WORD_LENGTH?strlen(string1):MAX_WORD_LENGTH;
    char *string = (char *) malloc(sizeof(char) * sz);
    strncpy(string,string1,sz);
    if (root==NULL) {
        return;
    } else if (strcmp(string, root->string) < 0) {
        if (root->left == NULL) {
            root->left = createword(string, linenumber);
        } else {
            insert_word(root->left, string, linenumber);
        }
    } else if (strcmp(string, root->string) > 0) {
        if (root->right == NULL) {
            root->right = createword(string, linenumber);
        } else {
            insert_word(root->right, string, linenumber);
        }
    } else {
        append_list(linenumber, root->linenumbers);
    }
    free(string);
}int main() {
    char path[MAX_PATH_LENGTH];
    FILE *fp;
    fgets(path, MAX_PATH_LENGTH, stdin);
    if (strlen(path) > 0 && path[strlen(path) - 1] == '\n')
        path[strlen(path) - 1] = '\0';
    
    fp = fopen(path, "r");
    if (fp == NULL) {
        printf("File not found\n");
        return 0;
    }
    char ch;
    int line_count = 1;
    char current_word[MAX_WORD_LENGTH] = "";
    word *root = NULL;
    while (!feof(fp)) {
        ch = fgetc(fp);
        //printf("%c", ch);
        if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
            if (ch >= 'A' && ch <= 'Z')
                ch = ch - 'A' + 'a';
            strncat(current_word, &ch, 1);
        } else if (ch == '-') {
            continue;
        } else {
            if (strlen(current_word) > 2) {
                if (root == NULL) {
                    root = createword(current_word, line_count);
                } else {
                    insert_word(root, current_word, line_count);
                }
            }
            memset(current_word, 0, sizeof(current_word));
            if (ch == '\n') {
                line_count++;
            }
        }
    }
    if (strlen(current_word) > 2) {
        if (root == NULL) {
            root = createword(current_word, line_count);
        } else {
            insert_word(root, current_word, line_count);
        }
    }
    fclose(fp);
    // print_tree(root);
    //printf("\n");
    //print_tree(root);
    int status=delete_low_ocurrence(root, NULL, 3);
    if (status == -1)root = NULL;
    print_tree(root);
    freetree(root);
    return 0;
}5)此函数所使用的辅助函数
word* createword(char string[], int linenumber)
{
    word *newword = (word*)malloc(sizeof(word));
    int sz=strlen(string)<=MAX_WORD_LENGTH?strlen(string):MAX_WORD_LENGTH;
    newword->string = (char*)malloc(sizeof(char)*sz);
    strncpy(newword->string, string,sz);
    newword->linenumbers = (list*)malloc(sizeof(list));
    newword->linenumbers->number = linenumber;
    newword->linenumbers->next = NULL;
    newword->left = NULL;
    newword->right = NULL;
    return newword;
}much2f
much3f
lol
lol
lol
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop发布于 2022-02-28 15:50:37
为什么malloc没有指向空内存位置?
因为它可以。未指定通过malloc()分配的内存的内容。
如果代码需要将内存归零,请参见calloc()。
坏码
strncpy(string,string1,sz)不会导致string是一个字符串,因为它可能缺少空字符终止。下面的(strcmp(string...是未定义的行为。相反,不要使用strncpy(),使用strcpy()并确保先前的分配有足够的空间来终止空字符。
strncpy(string,string1,sz);
...
} else if (strcmp(string, root->string) < 0) { // bad修复码
word* createword(const char string[], int linenumber) {
  word *newword = calloc(1, sizeof *newword);
  size_t length = strlen(string);
  if (length > MAX_WORD_LENGTH) {
    length = MAX_WORD_LENGTH;
  }
  char *s = malloc(length + 1); // Include room for the \0
  list *linenumbers = calloc(1, sizeof *linenumbers);
  // Test allocation success
  if (newword == NULL || s == NULL || linenumbers == NULL) {
    free(newword);
    free(s);
    free(linenumbers);
    return NULL;
  }
  memcpy(s, string, length);  // Only copy the first 'length' characters.
  s[length] = 0;
  newword->string = s;
  newword->linenumbers = linenumbers;
  newword->linenumbers->number = linenumber;
  newword->linenumbers->next = NULL;
  newword->left = NULL;
  newword->right = NULL;
  return newword;
}为什么“while( !feof (file) )”总是错误的?
feof(fp)在这里使用不当。fgetc()返回257个不同的值。不要使用char ch。
//char ch;
//...
//while (!feof(fp)) {
//    ch = fgetc(fp);
int ch;
...
while ((ch = fgetc(fp)) != EOF) {;发布于 2022-02-28 15:54:37
这是相当正常的行为。“‘malloc”只是做内存分配,它不对内存位置中已经存在的内容做出承诺。您可能需要的是“calloc”,它清除内存,然后将其分配给您的程序。
https://stackoverflow.com/questions/71297544
复制相似问题