C无法正确读取struct中的最后一个属性

时间:2019-12-03 19:20:12

标签: c

我无法在结构中获取最后一个属性。

这是我的结构

CurrentDb.Execute ("Update Fee Set AmountPaid=" & Nz(Me.Paid.Value, 0) & ", PaidDate='" & Format(Me.CollectionDate.Value, "dd/mmm/yyyy") & "' Where Empid=" & Me.empid.Value)

假设代码更新了i = 0处的目录信息

#define MAX_FILE_NUM 64
#define MAX_FILE_NAME_LENGTH 15

typedef struct 
{
    char file_name[MAX_FILE_NAME_LENGTH]; //file name
    int size; //size of the file
    int used; //0 represents the file is not used and 1 represents the file is used
    int block; //the index of data block that stores the file
    int block_count; //the number of blocks the file used
    int file_descriptor_count; //the number of file descriptors are referring to the file
}file_detail;

但是,当我尝试在for循环之外访问file_decsriptor_count时,会得到像1868958256这样的随机数。

file_detail * directory_block_ptr = (file_detail *)malloc(4096);

int create(char *name){
    if(name == NULL || find_file(name) != -1 || strlen(name) > MAX_FILE_NAME_LENGTH || super_block_ptr->files >= MAX_FILE_NUM){
        printf("%s\n", "ERROR: file create condition not satified");
        return -1;
    }

    //create the file
    for(int i = 0; i < MAX_FILE_NUM; i++){
        //update directory info
        if((directory_block_ptr+i)->used == 0){
            strcpy((directory_block_ptr+i)->file_name, name);
            (directory_block_ptr+i)->size = 0;
            (directory_block_ptr+i)->file_descriptor_count = 0;
            (directory_block_ptr+i)->used = 1;
            (directory_block_ptr+i)->block = -1;
            (directory_block_ptr+i)->block_count = 0;
            printf("%d\n", (directory_block_ptr+i)->file_descriptor_count);//correct output 0   
            break;
        }
    }


    printf("%d\n", (directory_block_ptr+0)->file_descriptor_count);//incorrect output
    //open the file
    int result = fs_open(name);

    if(result != -1)
    {
        printf("%s: %s %s\n", "File", name,  "created successfully");
        return 0;
    }

    return -1;
}

1 个答案:

答案 0 :(得分:1)

我要在这里走出去,建议malloc的内存未初始化,这就是这里的情况。

在循环中(并按照@Gerhardh的建议重写为正确的数组格式):

for (int i = 0; i < MAX_FILE_NUM; i++) {
        //update directory info
        if( directory_block_ptr[i].used == 0) {
            // assign stuff
            printf("%d\n", directory_block_ptr[i].file_descriptor_count);//correct output 0   
            break;
        }
    }

OP表示他们获得了正确的值,但是我们不知道它是[0]索引,对吗?如果位置directory_block_ptr[0].used处有垃圾,那么该位置将不会被触摸,循环将移至下一个位置。我们只是不知道找到了哪一个。

推荐给OP:将循环中的printf更改为:

printf("%d set in position [%d]\n", directory_block_ptr[i].file_descriptor_count, i);

,看看是否告诉您[0][1]或其他位置。我的猜测不是零。

推荐使用OP,将顶部的初始化更改为:

#define DIRECTORY_BLOCK_SIZE  4096

file_detail *directory_block_ptr = malloc(DIRECTORY_BLOCK_SIZE);

memset(directory_block_ptr, 0, DIRECTORY_BLOCK_SIZE);

这会分配相同数量的内存,但要确保将其全部设置为零,然后再次运行。

此外,这更是样式问题,而不是实质问题,我将通过定义一个更简单的变量名称来遍历整个循环来进行循环,这样更容易理解。

    for(int i = 0; i < MAX_FILE_NUM; i++){
        file_detail *fd = &(directory_block_ptr[i]); // shorthand

        //update directory info
        if(fd->used == 0){
            strcpy(fd->file_name, name);
            fd->size = 0;
            fd->file_descriptor_count = 0;
            fd->used = 1;
            fd->block = -1;
            fd->block_count = 0;
            printf("%d set in position [%d]\n", fd->file_descriptor_count, i);
            break;
        }
    }