繁体   English   中英

将链结列表的大小从'__int64'截断为'size_t

[英]Truncation the size of linked list from '__int64' to 'size_t

我通过创建10个存储桶成功地为基数排序编写了C ++代码。 对于这10个存储桶,我以这种方式创建了它们:

struct node{
    struct node* next;
    long value;
};

struct node*bucket[10];

for (int i=0; i<10; ++i) {  
    bucket[i] = (struct node *) malloc (1000000*sizeof(struct node));
}

这是完美的。

但是现在我需要将存储桶的数量增加到100000。我尝试修改这些存储桶的大小,例如:

struct node*bucket[100000];

for (int i=0; i<100000; ++i) {  
    bucket[i] = (struct node *) malloc (1000000*sizeof(struct node));
}

但是这次我想我什至无法创建这些存储桶。 我正在使用Visual Studio进行编码,而这是C ++。 编译器给我这些警告:

:警告C4305:'参数':从'__int64'到'size_t'的截断

:警告C4309:'参数':截断常数

我在互联网上进行搜索,有人说这个数字太大。 这是我第一次在链接列表中处理这么大的数字。 我是否需要修改任何内容以使此代码再次起作用?

谢谢。 任何想法和帮助,我将不胜感激!

我将您的代码变成了一个小示例程序:

#include <stdlib.h>

struct node {
    int i;
};

int main()
{
    struct node*bucket[100000];

    for (int i=0; i<100000; ++i) { 
        bucket[i] = (struct node *) malloc (1000000*sizeof(struct node));
    }
}

使用Visual Studio 2010可以很好地进行编译。

我想到的是,您正在分配一个包含100000个指针的数组(每个指针可能大约4个字节)。 它让我想起了旧的编译器,该编译器不允许每个变量(或函数?我不记得了。它使用Turbo Pascal或Turbo C ...)占用的堆栈空间超过64kB。

由于这是C ++,因此我建议首先不要使用原始的C数组。 相反,您可以将以上代码替换为:

#include <vector>

struct node {
    int i;
};

int main()
{
    std::vector<node> bucket( 100000 );
}

在使用C数组的所有情况下都可以使用std::vector对象。

问题可能出在循环条件中:

for (int i=0; i<100000; ++i)
              ^^^^^^^^^

您需要具有std::size_t i; 或未unsigned long i; 比较到100000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM