c - 确定给定用户内存大小和偏移量的所有可能的 32KB block 数

标签 c

我想确定给定内存块(始终是 32KB 的倍数)中每个 4KB、8KB、16KB(始终是 4KB 的倍数)偏移量的 32 KB block 的数量

For ex: 
1.) Input: memory block size: 128KB
2.) Output: Overall 32KB chunks for every of 8KB : 
             Total number of chunks : 12
               Start            End
    Chunk 1 :    0               32KB 
    Chunk 2 :    8KB             40KB
    Chunk 3 :    16KB            48KB 
    Chunk 4 :    32KB            64KB
    Chunk 5 :    38KB            70KB
    ..................................
    Chunk 12:    64Kb            128KB** 

我的程序

void determine_32Kb_chunks ( UINT64 size, UINT32 offset )

{
   UINT32 num_32KB_blocks = size / 32KB;
   UINT32 num_offset_size_blocks = size /offset;

   // Is this a valid formula ? 
   UINT32 total_numberof_32KB_chunks = num_32KB_blocks- num_offset_size_blocks;

}

对于4KB、16KB等,手工计算和程序公式显示出不同的结果。有人可以帮忙吗?

最佳答案

你的例子很幸运,因为:

size / 32KB = 128/32 = 4
size /offset = 128/8 = 16
num_32KB_blocks- num_offset_size_blocks = 4-16 = -12

与请求的结果 12 足够接近

但是公式应该是:

number_chunks = (block_size - chunk_size) / offset

在您的示例中:

(block_size - chunk_size) / offset = (128 - 32) / 8 = 12

在代码中(假设 32KB 由于某种原因有效):

UINT32 determine_32Kb_chunks ( UINT64 size, UINT32 offset )
{
   UINT32 total_numberof_32KB_chunks = (size - 32KB) / offset;
   return total_numberof_32KB_chunks;
}

关于c - 确定给定用户内存大小和偏移量的所有可能的 32KB block 数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703579/

相关文章:

c - 定义全局变量,在 main 中得到不同的结果

java - jni 中的 ASCII 到 HEX 的转换得到错误的值

c++ - 在 C/C++ 中检测多余的#include?

c++ - Eclipse & C/C++ - 我需要单独安装编译器吗?

c - 在 C 中打印标准输入文本

python - 严格类型会提高 Python 程序性能吗?

c - scanf() 根据变量声明顺序重置第一个结果

c++ - 类似的字符串算法

c - 使用 #define(添加结束大括号)进一步缩短 printf,或缩短 #define C

c - 使用 char *array[] 字符串数组的正确方法是什么?