memset设置值不正确

时间:2014-04-13 09:48:48

标签: c memory memory-management memory-leaks memset

在我的程序中调用此函数:

int cal_addr(long file_size ,  long* block, file* isfile,unsigned long block_size;) {


        long double tmp = (long double) file_size/block_size;
        *block = ceil (tmp ) ;
        int start = us.alloc_index ;    // us.alloc_index is int

        int fd = fs.tot_alloc_file ;  //  fs.tot_alloc_file is int
        int blk = *((int*)block) ;
        size_t s =  (blk) * sizeof(int) ;
    // us.usage is global array of integers
        memset(& (us.usage[start] ), fd , s);
        us.alloc_index =  us.alloc_index + (*block) ;
        isfile->end_addr_usage = us.alloc_index;
        return 1 ;
}

// gdb的输出如下。当我打印时,我看到fd值仍为1 us.usage [202]的元素,例如它有这个奇怪的价值。不是我期望的那个

(gdb) p us.usage[202]
$3 = 16843009

(gdb) p fd
$5 = 1
(gdb) 

1 个答案:

答案 0 :(得分:3)

memsetchar - by - char为基础运作。您无法使用它来设置int s的值。 (请注意16843009 == 0x01010101。)

如果您想设置int s的值,则需要使用循环。