c++ - 使用 xlc 编译器在 IBM AIX 中分配内存失败?

标签 c++ c xlc

在我的代码中需要分配几个大数组。

但是,当我尝试使用 IBM xlc_r 时:

xlc_r -g -O -L。 -qarch=pwr7 -qtune=pwr7 -lesslsmp -lm -qsmp -qthreaded -qmaxmem=-1 2.c

int main()
{
     int natom = 5000;
     while(1)
     {
        double *z =(double*) malloc(natom*natom*natom* sizeof(double));
        if(!z)
        {  
           printf("error memory vector z\n\n");
           exit(-1);
        }
     }
}

我收到了内存 vector z 的 Killed 错误。

这是ulimit -a:

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max memory size         (kbytes, -m) unlimited 
open files                      (-n) 102400   
pipe size            (512 bytes, -p) 64   
stack size              (kbytes, -s) unlimited   
cpu time               (seconds, -t) unlimited   
max user processes              (-u) 128  
virtual memory          (kbytes, -v) unlimited

是否需要任何标志来分配更多内存?

最佳答案

不久前我在 POWER775 AIX 机器上遇到了这个问题。编译时需要添加 -q64 标志才能分配超过 2GiB 的内存。请注意,其他人关于使用 int 的评论可能与您的代码相关,这就是我下面的示例使用 size_t 的原因。

我建议您运行一个简单的分配测试来调查这一点。我针对这种情况编写的测试程序如下。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char * argv[])
{
    size_t max = (argc>1) ? (size_t)atol(argv[1]) : ((size_t)256*1024)*((size_t)1024*1024);
    for (size_t n=1; n<=max; n*=2) {
        printf("attempting malloc of %zu bytes \n", n);
        fflush(0);
        void * ptr = malloc(n);
        if (ptr==NULL) {
            printf("malloc of %zu bytes failed \n", n);
            fflush(0);
            return 1;
        } else {
            printf("malloc of %zu bytes succeeded \n", n);
            fflush(0);
            memset(ptr,0,n);
            printf("memset of %zu bytes succeeded \n", n);
            fflush(0);
            free(ptr);
        }
    }
    return 0;
}

相关的构建标志如下。

ifeq ($(TARGET),POWER7-AIX)
# Savba i.e. POWER775 AIX
    CC       = mpcc_r
    OPT      = -q64 -O3 -qhot -qsmp=omp -qtune=pwr7 -qarch=pwr7 -qstrict
    CFLAGS   = $(OPT) -qlanglvl=stdc99
endif

关于c++ - 使用 xlc 编译器在 IBM AIX 中分配内存失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567428/

相关文章:

c++ - 为什么 xlc++ 编译器会提示强制转换右值?

c++ - 没有用于调用 std::unordered_map::insert() 的匹配成员函数

c++ - 静态析构函数被提前调用

c++ - 静态数组访问冲突?

c - 在C中,如何以随机顺序打乱数组索引的指针

c - 我如何将 const 指针发送到 c 中的非 const 指针?

c - 如何检查数组位置是否存在?

c++ - XLC++ 11.1 的奇怪问题

c++ - 将 IBM C++ 编译器从 xlc_R 10.0 升级到 11.1 后出现的问题

c++ - 访客模式添加新功能