[英]memset is setting value incorrectly
這個函數在我的程序中被調用:
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的輸出如下。 當我打印us.usage [202]的元素時,我看到fd值仍然為1,例如它具有這個更奇怪的值。 不是我期望的1
(gdb) p us.usage[202]
$3 = 16843009
(gdb) p fd
$5 = 1
(gdb)
memset
一個上運行char
-by- char
基礎。 您不能使用它來像這樣設置int
的值。 (請注意16843009 == 0x01010101
。)
如果要設置int
的值,則需要使用循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.