指针 内存应用
来源:互联网 发布:tensorflow 多gpu 编辑:程序博客网 时间:2024/05/22 01:34
#include <iostream>
using namespace std;
struct STU
{
int id;
char name[10];
};
#define NUM 5
#define SIZE sizeof(struct STU)
void setData(char *pdata,struct STU *pStu)
{
int i=0;
char ch='1';
for(i=0;i<NUM;i+=2)
{
memcpy(pdata+i,&ch,sizeof(char));
memcpy(pdata+NUM+i*sizeof(struct STU),pStu+i,sizeof(struct STU));
}
}
void show(char* pdata)
{
int i=0;
for(i=0;i<NUM;i++)
{
if(*(pdata+i)=='1')
{
printf("%d %s",((struct STU*)(pdata+NUM+i*sizeof(struct STU)))->id,((struct STU*)(pdata+NUM+i*sizeof(struct STU)))->name);
printf("\n");
}
}
}
int main()
{
struct STU stu[5]={{1000,"mac"},{0},{1001,"win"},{0},{1002,"linux"}};
void* space=NULL;
space=malloc(NUM+NUM*SIZE);
memset(space,0,NUM+NUM*SIZE);
if(!space)
{
cout<<"memory error"<<endl;
}
setData((char*)space,stu);
show((char*)space);
return 0;
}
using namespace std;
struct STU
{
int id;
char name[10];
};
#define NUM 5
#define SIZE sizeof(struct STU)
void setData(char *pdata,struct STU *pStu)
{
int i=0;
char ch='1';
for(i=0;i<NUM;i+=2)
{
memcpy(pdata+i,&ch,sizeof(char));
memcpy(pdata+NUM+i*sizeof(struct STU),pStu+i,sizeof(struct STU));
}
}
void show(char* pdata)
{
int i=0;
for(i=0;i<NUM;i++)
{
if(*(pdata+i)=='1')
{
printf("%d %s",((struct STU*)(pdata+NUM+i*sizeof(struct STU)))->id,((struct STU*)(pdata+NUM+i*sizeof(struct STU)))->name);
printf("\n");
}
}
}
int main()
{
struct STU stu[5]={{1000,"mac"},{0},{1001,"win"},{0},{1002,"linux"}};
void* space=NULL;
space=malloc(NUM+NUM*SIZE);
memset(space,0,NUM+NUM*SIZE);
if(!space)
{
cout<<"memory error"<<endl;
}
setData((char*)space,stu);
show((char*)space);
return 0;
}
0 0
- 指针 内存应用
- VB.NET中指针和非托管内存的应用
- C语言指针应用——内存分配和释放
- VB.NET中指针和非托管内存的应用
- VB.NET中指针和非托管内存的应用
- 子函数中的内存分配(二级指针的应用)
- 指针 应用
- 指针应用
- 指针应用
- 指针应用
- 指针应用
- C#内存管理-栈堆/回收器托管/非托管资源释放/指针的应用
- C_C++指针指针应用详解
- C_C++指针指针应用详解
- C_C++指针指针应用详解
- C_C++指针指针应用详解
- 关于指针(内存)泄漏
- 指针与内存(一)
- glew,glfw实现最新的opengl-学习笔记4实现纹理
- PostgreSQL之dblink使用
- 线性回归——最小二乘求解
- 简单使用web服务
- 看了这篇博客我才知道我好像不太懂C和Cpp
- 指针 内存应用
- 整数n的二进制形式中1的个数
- CoreStore 问题记录
- 聊聊HTTPS和SSL/TLS协议
- AdapterView(五)
- grok 添加字段
- 百度外卖笔试总结
- 各类游戏系统简述
- 安卓系统开发常用指令大全