这个函数在我的程序中被调用:
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] 的元素,例如它具有这个奇怪的值。不是我期望的 1
(gdb) p us.usage[202]
$3 = 16843009
(gdb) p fd
$5 = 1
(gdb)
最佳答案
memset
在char
-by-char
基础上运行。您不能像这样使用它来设置 int
的值。 (注意 16843009 == 0x01010101
。)
如果要设置 int
的值,则需要使用循环。
关于c - memset 设置值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041225/