c - memset 设置值不正确

标签 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] 的元素,例如它具有这个奇怪的值。不是我期望的 1

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

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

最佳答案

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

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

关于c - memset 设置值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041225/

相关文章:

c - 使用递增的 ASCII 值处理字符串中的多个 '\000'

Mac 上的 C++ 内存清理

python - 在不同机器上分配大 ndarray 时出现 MemoryError 与 "ValueError: array is too big"

c++ - x64 进程在 4GB RAM 上可以占用多少内存

c++ - 动态字符数组没有给出正确的字符串长度?

c - 两位数指针?这怎么发生的?

c - 管道未发送准确信息

c++ - 从 C/C++ 中的函数返回不带静态整数的数组指针

c - 从文件中正确读取

c# - 如何获取任务管理器中显示的应用程序内存使用情况?