[英]array of structures for 1M elements in C
I have the code bellow that is working the way I intended to work for a data relatively small say 10000 lines from a text file. 我有下面的代码可以按照我打算为文本文件中相对较小的数据(例如10000行)工作的方式工作。 I need it to work for a file with 1M lines.
我需要它来处理1M行的文件。 It gives seg fault and I don't know how to go around it.
它导致段错误,我不知道如何解决。 Can you help me?
你能帮助我吗?
This is called in main: 这在主要方面称为:
playersInfo player[size];
.......more code here .......更多代码在这里
then for this part of program I have this code: (it reads a file and puts the info in the array then calculates the mvp of a player and prints the player with the highest mvp) 那么对于程序的这一部分,我有以下代码:(它读取文件并将信息放入数组中,然后计算播放器的mvp并打印具有最高mvp的播放器)
if(choice == 'D')
{
printf("\n");
while(i < size && fscanf(myFile, "%d %s %f %f %f %f %f %d %d %d \n", &player[i].id, player[i].name,
&player[i].ppg, &player[i].apg, &player[i].rpg, &player[i].spg, &player[i].mpg,
&player[i].vote1, &player[i].vote2, &player[i].vote3) != EOF)
{
player[i].mvp = 0;
i++;
}
for(i = 0; i < size; i++)
{
int current = i;
if(player[current].vote1 != player[current].id)
{
i = findIndex(player, size, player[current].vote1);
player[i].mvp += 3;
}
if((player[current].vote2 != player[current].id) && (player[current].vote2 != player[current].vote1))
{
i = findIndex(player, size, player[current].vote2);
player[i].mvp += 2;
}
if((player[current].vote3 != player[current].id) && (player[current].vote3 != player[current].vote1) && (player[current].vote3 != player[current].vote2))
{
i = findIndex(player, size, player[current].vote3);
player[i].mvp += 1;
}
i = current;
}
qsort((void *) &player, size, sizeof(playersInfo), (compfn)comparePlayersByMVP);
printf("The MVP is %s (%d), with %d point(s).\n", player[0].name, player[0].id, player[0].mvp);
}
If you are declaring your array in stack, then it may be too large for that. 如果要在堆栈中声明数组,则该数组可能太大。 Consider using the heap.
考虑使用堆。
playersInfo *player = (playersInfo*) malloc(size * sizeof(playersInfo));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.