c/c++语言开发共享如何从C中的“原始”内存中读取/写入类型值?

我如何制作这样的作品呢?

void *memory = malloc(1000); //allocate a pool of memory *(memory+10) = 1; //set an integer value at byte 10 int i = *(memory+10); //read an integer value from the 10th byte 

    简单示例:将内存视为unsigned char数组

     void *memory = malloc(1000); //allocate a pool of memory uint8_t *ptr = memory+10; *ptr = 1 //set an integer value at byte 10 uint8_t i = *ptr; //read an integer value from the 10th byte 

    您也可以使用整数,但是您必须注意一次设置的字节数。

    规则很简单:

    由此可以得出结论,如果要执行“原始”指针算法,则必须转换为char *。

    所以,通过“工作”我假设你的意思是“如何取消引用/执行void*指针算术void* ”? 你不能; 如果你只是关心阅读大块的内存,你必须把它强制转换为char* 。 当然,如果是这种情况,只需将其声明为char*即可。

      以上就是c/c++开发分享如何从C中的“原始”内存中读取/写入类型值?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/545898.html

      (0)
      上一篇 2021年1月10日
      下一篇 2021年1月10日

      精彩推荐