内存地址指系统 RAM 中的特定位置,通常以十六进制的数字表示。
在8086的实模式下,把某一段寄存器左移4位,然后与地址ADDR相加后被直接送到内存总线上,这个相加后的地址就是内存单元的物理地址,而程序中的这个地址就叫逻辑地址(或叫虚地址)。在80386的保护模式下,这个逻辑地址不是被直接送到内存总线,而是被送到内存管理单元(MMU)。MMU由一个或一组芯片组成,其功能是把逻辑地址映射为物理地址,即进行地址转换。
分配内存空间然后读取
#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
}
}