char *buf = malloc(123)
再访问buf
在嵌入式或者单片机中,有时会访问绝对地址
unsigned char * p = (unsigned char *)0xB1234456/和辩/内存地慧棚历址值
然后通过*p访问该地址。前搜
分配内存空宏袜间然后读取
#include<stdio.h>
#include<stdlib.h>
intfilelength(FILE*fp)
char*readfile(char*path)
intmain(void)
{
FILE*fp
char*string
string=readfile("c:/c.c")
printf("读入完毕\n按任意键释放内存资源\n")
//printf("%s\n",string)
system("pause")
return0
}
char*readfile(char*path)
{
FILE*fp
intlength
char*ch
if((fp=fopen(path,"r"))==NULL)
{
printf("谈拦openfile%serror.\n",path)
exit(0)
}
length=filelength(fp)
ch=(char*)malloc(length)
fread(ch,length,1,fp)
*(ch+length-1)='\0'
returnch
}
intfilelength(FILE*fp)
{
intnum
fseek(fp,0,SEEK_END)
num=ftell(fp)
fseek(fp,0,SEEK_SET)
returnnum
}
扩展资料
cLinux进程间通信共享内存:
#include"comm.h"
staticintcommShm(intsize,intflags)//创建共享内存
{
key_tkey=ftok(PATHNAME,PROJ_ID)
if(key<0){
perror("ftok")
return-1
}
intshmid=shmget(key,size,flags)
if(shmid<0){
perror("shmget"含绝胡)
return-2
}
returnshmid
}
intcreateShm(intsize)
{
returncommShm(size,IPC_CREAT|IPC_EXCL)
}
intgetShm(intsize)//获取共享内存
{
returncommShm(size,IPC_CREAT)
}
intdestoryShm(intshmid)//销毁共享内存
{
if(shmctl(shmid,IPC_RMID,NULL)<0){
perror("shmctl")
return-3
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)