【问题标题】:memset is setting value incorrectlymemset 设置的值不正确
【发布时间】:2014-04-13 09:48:47
【问题描述】:

这个函数在我的程序中被调用:

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) 

【问题讨论】:

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


    【解决方案1】:

    memset 在char-by-char 基础上运行。您不能像这样使用它来设置ints 的值。 (注意16843009 == 0x01010101。)

    如果要设置ints 的值,则需要使用循环。

    【讨论】:

    • 非常感谢。很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2011-05-28
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2021-05-21
    相关资源
    最近更新 更多