首页 > 系统相关 >struct和malloc内存互转例子

struct和malloc内存互转例子

时间:2024-05-17 14:30:56浏览次数:17  
标签:malloc struct unsigned char 8bit mallocdata 互转 sizeof memcpy

#include <iostream>

typedef struct DS1001 {
    unsigned int a;
    unsigned char b;
    unsigned int c;

    unsigned char d;
    unsigned short e;
} DS1001;

void TestMallocToStruct()
{
    int nsize = sizeof(DS1001);
    unsigned char* mallocdata = (unsigned char*)malloc(sizeof(unsigned char) * nsize);
    memset(mallocdata, 0, sizeof(unsigned char) * nsize);

    unsigned char* _8bit = (unsigned char*)malloc(sizeof(unsigned char));
    //a
    *_8bit = 1;
    memcpy(mallocdata + 0, _8bit, sizeof(unsigned char));
    *_8bit = 0;
    memcpy(mallocdata + 1, _8bit, sizeof(unsigned char));
    memcpy(mallocdata + 2, _8bit, sizeof(unsigned char));
    memcpy(mallocdata + 3, _8bit, sizeof(unsigned char));
    //b
    *_8bit = 2;
    memcpy(mallocdata + 4, _8bit, sizeof(unsigned char));
    *_8bit = 0;
    memcpy(mallocdata + 5, _8bit, sizeof(unsigned char));
    memcpy(mallocdata + 6, _8bit, sizeof(unsigned char));
    memcpy(mallocdata + 7, _8bit, sizeof(unsigned char));


    //c
    *_8bit = 3;
    memcpy(mallocdata + 8, _8bit, sizeof(unsigned char));
    *_8bit = 0;
    memcpy(mallocdata + 9, _8bit, sizeof(unsigned char));
    memcpy(mallocdata + 10, _8bit, sizeof(unsigned char));
    memcpy(mallocdata + 11, _8bit, sizeof(unsigned char));
    //d
    *_8bit = 4;
    memcpy(mallocdata + 12, _8bit, sizeof(unsigned char));
    *_8bit = 0;
    memcpy(mallocdata + 13, _8bit, sizeof(unsigned char));

    //e
    *_8bit = 5;
    memcpy(mallocdata + 14, _8bit, sizeof(unsigned char));
    *_8bit = 0;
    memcpy(mallocdata + 15, _8bit, sizeof(unsigned char));
    //强转成DS1001进行数据打印
    DS1001* convertdata = (DS1001*)mallocdata;
    printf("a:%d\n", convertdata->a);
    printf("b:%d\n", convertdata->b);
    printf("c:%d\n", convertdata->c);
    printf("d:%d\n", convertdata->d);
    printf("e:%d\n", convertdata->e);
}
//拷贝内存后用结构体输出值
void StructToMallocValue()
{
    unsigned char* mallocdata = (unsigned char*)malloc(sizeof(DS1001));
    memset(mallocdata, 0, sizeof(DS1001));
    printf("DS1001:%d\n", sizeof(DS1001));
    unsigned int* e = (unsigned int*)malloc(sizeof(unsigned int));
    *e = 1;
    DS1001 ds1001 = { 1,2,3,4,5 };
    //    printf("&ds1001.a:%x\n",&ds1001.a);
    //    printf("&ds1001:%x\n",&ds1001);
    memcpy(mallocdata, (unsigned char*)&ds1001, sizeof(DS1001));
    int nsize = sizeof(ds1001);
    for (int j = 0; j < nsize; j++)
    {
        printf("mallocdata:%d\n", *(mallocdata + j));
    }
    free(mallocdata);
}
int main()
{
    TestMallocToStruct();
    StructToMallocValue();
}

代码如上:

输出结果

 

标签:malloc,struct,unsigned,char,8bit,mallocdata,互转,sizeof,memcpy
From: https://www.cnblogs.com/bang20221103/p/18197722

相关文章

  • 数组和List互转
    数组转List数组转List,使用JDK中java.util.Arrays工具类的asList方法。importjava.util.Arrays;importjava.util.List;publicclassMain{publicstaticvoidmain(String[]args){String[]strs={"aaa","bbb","ccc"};List......
  • Structures Or Why Don't Things Fall Down (Reading)
    1BentmasonrycolumninSalisburyCathendral2Stressconcentrationatcracktip3'Aneurism'incylindricalballoon4Sectionofarterywalltissue5CorbelledvaultatTiryns6Simi-corbelledposterngateatTiryns7Clarebridge,Cambride(c......
  • C#实现时间互转毫秒时间戳
    ///<summary>///DateTime类型转换为时间戳(毫秒值)///</summary>///<paramname="dateTime">时间</param>///<returns>13位时间戳</returns>staticlongDateToTicks(DateTime?dateTime){//北京时间相差8小时DateTimestartTime=......
  • img2img-turbo-素描转图片,黑夜白天互转。。。。
    源码地址https://github.com/GaParmar/img2img-turbo在线体验地址https://huggingface.co/spaces/gparmar/img2img-turbo-sketch安装gitclonehttps://github.com/GaParmar/img2img-turbocdimg2img-turbocondaenvcreate-fenvironment.yaml经过测试,triton这个库pip......
  • StarCoder2-Instruct: 完全透明和可自我对齐的代码生成
    指令微调是一种技术,它能让大语言模型(LLMs)更好地理解和遵循人类的指令。但是,在编程任务中,大多数模型的微调都是基于人类编写的指令(这需要很高的成本)或者是由大型专有LLMs生成的指令(可能不允许使用)。我们推出了一个叫做StarCoder2-15B-Instruct-v0.1的模型,这是第......
  • 深入探索JavaScript中的structuredClone:现代深拷贝的解密指南
    在JavaScript中,实现深拷贝的方式有很多种,每种方式都有其优点和缺点。今天介绍一种原生JavaScript提供的structuredClone实现深拷贝。下面列举一些常见的方式,以及它们的代码示例和优缺点:1.使用JSON.parse(JSON.stringify(obj))代码示例:functiondeepClone(obj){re......
  • Xml和Json在线互转工具
    1、支持将Xml数据转为Json字符串格式2、支持将Json字符串转换为Xml格式的数据3、Json转为Xml数据时,提供自动检测Json格式是否正确,确保转换的Xml数据是准确的4、提供详细的Xml数据和Json字符串数据5、提供示例xml和json示例,供大家测试使用免费使用工具地址:http://tools.linu......
  • Java面试题:@PostConstruct、init-method和afterPropertiesSet执行顺序?
    在Spring框架中,@PostConstruct注解、init-method属性、以及afterPropertiesSet()方法通常用于初始化Bean的逻辑。它们都提供了在Bean创建和初始化完成后执行的方法,但执行顺序有所不同。想要知道@PostConstruct、init-method、afterPropertiesSet()的执行顺序,只要搞明白它们各自在......
  • LLaMA-Factory 训练 Llama3-Chinese-8B-Instruct 相关报错问题解决
    模型路径up主为llama中文社区模型地址https://www.modelscope.cn/models/FlagAlpha/Llama3-Chinese-8B-Instruct/summarysysinfov10032gnvcc--versioncuda11.8pythonimporttorchprint(torch.version)13.11pipinstallflash_attntimeout2下载whl报这个错......
  • struct:Python二进制数据结构
    在C/C++语言中,struct被称为结构体。而在Python中,struct是一个专门的库,用于处理字节串与原生Python数据结构类型之间的转换。本篇,将详细介绍二进制数据结构struct的使用方式。函数与Struct类struct库包含了一组处理结构值得模块级函数,以及一个Struct类。格式指示符将由字符串格......