Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中调用函数后,值会发生变化_C - Fatal编程技术网

在C中调用函数后,值会发生变化

在C中调用函数后,值会发生变化,c,C,我正在尝试用C语言创建文件系统。当我在下面的代码中打印我的值时,我的这部分代码遇到了问题: for (int i = 0; i<NUM_POINTERS; i++) { printf("before SB->root[%d]=%d\n", i, SB->root->pointers[i]); } write_blocks(0, 1, SB); for (int i = 0; i&l

我正在尝试用C语言创建文件系统。当我在下面的代码中打印我的值时,我的这部分代码遇到了问题:

for (int i = 0; i<NUM_POINTERS; i++) {
            printf("before SB->root[%d]=%d\n", i, SB->root->pointers[i]);
        }
        write_blocks(0, 1, SB);
        for (int i = 0; i<NUM_POINTERS; i++) {
            printf("after SB->root[%d]=%d\n", i, SB->root->pointers[i]);
        }
这是单线程的吗? 修改后的SB->root[0,1]是否包含您试图写入的数据? 你的积木尺寸是多少

我怀疑问题出在write_blocks()之外。我最好的猜测是你不小心在某处释放了某人,马洛克给了你相同的地址。在malloc检查(打印或调试器)之后,缓冲区和块写入都将被删除,并确保它们不同且有效

无关问题:

  • printf的参数超过%s
  • 你应该检查malloc的退货
  • e永远不会被设定
  • s和我是平等的。也就是多余的
  • 越界错误导致内存泄漏(因为它位于malloc之后)
  • usleep很奇怪也许你想要fsync

usleep和“暂停直到等待时间结束”的评论引起了怀疑。这个特定的文件系统是否有一些是异步的?
e
在哪里设置?您是否打算使用常量参数0和1调用write_blocks()?您有一个指向某个不再有效的变量空间的指针,该变量空间正被您调用的函数重用。我不认为您展示的是MCVE()。您还做过任何其他异步操作,可能在这些指令之间写入内存吗?您如何分配
SB
?进程之间是否共享内存?您还有其他线程吗?问题发生在void*blockWrite=(void*)malloc(BLOCK\u SIZE);如果我把它注释掉就可以了。这行:
printf(“在SB->root[%d]=%d之前,SB->root[%d]=%d\n”,i,SB->root->pointers[i])
有4个格式说明符,但只有2个参数,因此不编译。类似的考虑也适用于这一行:
printf(“在SB->root[%d]=%d之后,SB->root[%d]=%d\n”,i,SB->root->pointers[i])
我在使用函数后立即检查SB,因此不会发生释放。write_blocks函数不是我写的,所以我不关心e没有被设置SB和blockWrite的地址是什么?如果问题是某人被释放了。它必须在调用write_块之前被释放。检查某人并不能证明他没有被释放。在C中,指针在空闲时间后显示为有效。
    int write_blocks(int start_address, int nblocks, void *buffer)
{
    int i, e, s;
    e = 0;
    s = 0;

    void* blockWrite = (void*) malloc(BLOCK_SIZE);

    /*Checks that the data requested is within the range of addresses of the disk*/
    if (start_address + nblocks > MAX_BLOCK)
    {
        printf("out of bound error\n");
        return -1;
    }

    /*Goto where the data is to be written on the disk*/
    fseek(fp, start_address * BLOCK_SIZE, SEEK_SET);

    /*For every block requested*/        
    for (i = 0; i < nblocks; ++i)
    {
        /*Pause until the latency duration is elapsed*/
        usleep(L);

        memcpy(blockWrite, buffer+(i*BLOCK_SIZE), BLOCK_SIZE);
        fwrite(blockWrite, BLOCK_SIZE, 1, fp);
        fflush(fp);
        s++;
    }
    free(blockWrite);

    /*If no failure return the number of blocks written, else return the negative number of failures*/
    if (e == 0)
        return s;
    else
        return e;
}
    typedef struct iNode
{
    int id;
    int size;
    int pointers[NUM_POINTERS];
} iNode;

typedef struct superBlock
{
    int magic_number;
    int block_size;
    int num_blocks;
    int num_inodes;
    iNode *root;
    iNode jNodes[20];
} superBlock;