C中的malloc()返回填充的内存



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"根=BST,sz得到3的值

malloc((分配给string的值是0x55555555c510 "340305UUUU"为什么malloc不指向空内存位置?(这不是随机行为,它是可预测和可重复的(此外,由于某种原因,这个循环运行了无限长的时间

while(strcmp(string1,string)!=0)
{
free(string);
string=NULL;
string = (char *) malloc(sizeof(char) * sz);
strncpy(string,string1,sz);
}

更多相关代码

  1. #define MAX_WORD_LENGTH 20

  2. 结构的定义

    typedef struct linkedList
    {
    int number;
    struct linkedList *next;
    }list;
    typedef struct word_with_count
    {
    char* string;
    list *linenumbers;
    struct word_with_count *left;
    struct word_with_count *right;
    }word;```
    

[3](函数

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);
}
  1. main((,调用此函数
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] = '';

fp = fopen(path, "r");
if (fp == NULL) {
printf("File not foundn");
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;
}
  1. 作为输入给出的文本文件
much2f
much3f
lol
lol
lol
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop
qwertyuiopasdfghjklzxcvbnmqwertyuiop

为什么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 
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)。CCD_ 16返回257个不同的值。请勿使用char ch

//char ch;
//...
//while (!feof(fp)) {
//    ch = fgetc(fp);
int ch;
...
while ((ch = fgetc(fp)) != EOF) {;

这是非常正常的行为。'malloc只是进行内存分配,它对该内存位置中已经存在的内容不做任何承诺。您可能需要的是"calloc",它清除内存,然后将其分配给程序。

最新更新