C、 大指针数组分配(linux 64位)



我分配了2500000000个这样的单位:

struct test
{
 public:
 test();
 void insert(unsigned int var1, unsigned int var2, unsigned long var3);
      bool isEmpty() const;
      unsigned char * data;
};

test::test()
{
data = NULL;
}
bool test::isEmpty() const
{
  return (data == NULL);
}

unsigned long index = 25000000000;

像这样:

test *something = new (nothrow) test[index];
    if (!something)
     {
     cout << "Error allocating memory for [something]" << endl;
     return ;
    }
    cout << "DONE !" << endl;

则i甚至将其CCD_ 2的CCD_。

unsigned long j=0;
        while (j<index) 
        {
          something[j].data = NULL;
          j++;
        }

这一切都很好,除了当我迭代某物[]时,比如:

test *chk;
unsigned long empty = 0;
unsigned long not_empty = 0;
unsigned long i=0;
for (i=0; i< index; i++ )
{ 
    chk = &something[i];
    if (!chk->isEmpty())
     {
    not_empty++;
    } else
    empty++;
}

cout << "Empty [" << empty << "]" << endl;
cout << "Not Empty [" << not_empty << "]" << endl;
cout << "Total [" << empty + not_empty << "]" << endl;

(上次更新)

原来是硬件问题——内存。一些棍子不能与其他棍子一起正常工作。感谢大家的建议,太糟糕的硬件路径不在答案o/中

(更新)

我得到了一定数量的初始化元素,这些元素不是NULL(not_empty)。仍然不知道为什么。当然,empty+not_empty=索引。

如果我在构造函数中注释掉data = NULL;,如果我在分配数组后立即循环数组,我会得到正确的空/非空数。但在强制指针为NULL后,在再次循环数组后,我仍然会得到相同的常量非空值。

我还使用malloc()将其修改为普通指针,这并没有什么不同,只是这是我第一次注意到指针是正确的(空的),直到我设置了它们。

我用较小的[指数]值(550000000)做了一些测试,但我无法复制这种行为。

我使用谷歌的ltcmalloc和分配的内存大小建议,它使用的是正确的[索引]大小;

与其写2500000000,不如写2500000000ul。我怀疑你的数字变成了32位整数,因此j<2500000000始终为真,因为int(2500000000)=-76980376。

最新更新