云栖问答

找不到答案?去提问题

linux 2016-06-03 09:07:03

linux下c语言大内存分配

远程登录工作站(64G)c语言分配12G内存失败
代码如下

 #include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
    char *x=NULL;
    x=(char *)calloc(3001*3001*1200,sizeof(char));
    if(x==NULL)
    printf("failed");
    else printf("success");
    return 0;
}

1个回答

0

小旋风柴进

 #include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc,char**argv)
{
    char *x=NULL;
    int fd;

    if((fd=open(argv[1],O_RDWR))==-1)
    {
        printf("failed to open\n");
    }

    x=mmap(0,3001*3001*1200*sizeof(char),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

    if(x==NULL)
        printf("failed\n");
    else 
        printf("x=0x%x\n",x);

     return 0;
}

1
GO