1. 使用malloc申请一块空间,模拟KV存储的一个节点存储数据信息。
#include<stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/time.h> #include <sys/resource.h> typedef struct v_block *p_block; struct v_block{ p_block next; int free; char *key; char *value; char data[]; }; int main() { p_block pmem=(p_block)malloc(40); pmem->next=0; pmem->free=0; pmem->key=pmem->data; int i; char *s="one"; for(i=0;i<4;i++) { *(pmem->key+i)=s[i]; } pmem->value=pmem->key+4; char *va="this is a value"; for(i=0;i<15;i++) { *(pmem->value+i)=va[i]; } printf("%s ",pmem->value); printf("%s ",pmem->key); return 0; }
时间: 2024-10-29 01:29:45