c - 缓冲区为空! : Cannot allocate memory

标签 c

我编写了一个程序,它将 MB 作为 3 个参数并给出吞吐时间。

int main(int argc,char* argv[]){
 double kb = 0;
 int time_taken = 0;
 double throughput = 0;
 int i = 0;
 int w_rw = 0;
 int fd = -1;
 int bw = -1;
 unsigned long long nob = 0;
 char ip;
 char* file_name = "myfile";
 char* buff;
 struct stat info;
 struct timeval start_time,end_time;
 int mb = 1024 * 1024;

 /* Check the nos for W/RW and MBs*/
 if(1 == argc){
        printf("\n Missing 2 params R/RW & MB,Eg: execute ./a.out 2 100 \n");
        return 1;
 }
 if(argc < 4){
         w_rw = atoi(argv[1]);
         if(w_rw > 2){
                w_rw = 2;
         }
         nob = (atoi(argv[2]) * mb);
         printf("\n W/RW : %d BYTES : %u \n",w_rw,nob);
 }
 else{
        // Do Nothing
 }
 /* Allocate Buffer */
 buff = (char *)malloc(nob * sizeof(char));
 if(NULL == buff){
        perror("Buffer NULL!");
        return -1;
 }

 printf("\n File - Create,Open,Write,Close \n");

 for(i = 0; i < w_rw; i++){
        if(i == 0){
                printf("\n --- Write IO Performance--- \n");
        }
        else{
                printf("\n --- Re-write IO Performance--- \n");
                printf("\n Press any char to continue : \n");
                ip = getchar();
        }

        /* Open file*/
        if((fd = open(file_name,O_CREAT | O_RDWR))< 0){
                perror("Open failed!");
                return -1;
        }

        /*Calculating the start and end time of write*/
        gettimeofday(&start_time,NULL);
        if((bw = write(fd,buff,nob)) < 0){
                perror("Write failed!");
                return -1;
        }
        gettimeofday(&end_time,NULL);

        /*Calculating the throughput*/
        time_taken = (end_time.tv_sec - start_time.tv_sec);
        kb = bw/1024;
        throughput = kb/time_taken;

        /* Start */
        if(-1 == stat(file_name,&info)){
                perror("STAT Failed");
                return -1;
        }

        printf("\n Inode no: %d \n Blocks : %d \n",info.st_ino,info.st_blocks);
        printf("\n Start sec : %u \n",start_time.tv_sec);
        printf("\n End sec : %u \n",end_time.tv_sec);
        printf("\n Bytes written : %d bytes \n",bw);
        printf("\n Time_taken : %d secs \n",time_taken);
        printf("\n Throughput : %f kb/sec \n",throughput);

        close(fd);
 }

 unlink(file_name);
 return 0;
}

O/P:

[root@rw gfs]# ./a.out 2 76800

W/RW:2 字节:3221225472 Buffer NULL!: 无法分配内存

最佳答案

您正在请求一个巨大内存块(78GB!)并且 malloc 失败,正如在任何标准(台式机/笔记本电脑)计算机上所预期的那样。

事实上,你有:

int mb = 1024 * 1024;
// ...
nob = (atoi(argv[2]) * mb);
// ...
buff = (char *)malloc(nob * sizeof(char));

因此 nob 在您的情况下是 1024 * 1024 * 76800

尝试使用较小的尺寸,它会起作用。

关于c - 缓冲区为空! : Cannot allocate memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351426/

相关文章:

c - 如何从 C 中的字符串中提取各种整数?

c - 努力形成一个链表

c - 为什么sctp_inet_listen的第一个参数是struct socket*而不是int

c - 为什么寄存器不能用于 C 中的变量?

c - 使用静态库链接、编译和运行 C 程序

c - 在堆栈上分配大型数组时出现段错误

应用于配对序列时有关快速 union 算法的困惑 : 1-2, 2-3,3-4

C:当我将一个数组复制到另一个数组时,代码停止工作

c - gprof profiler 说我的 C 程序花费的时间比实际少得多

c - array[] 存储 > char,如何使用