为啥这个 c++ 代码在 Linux 中可以正常工作,但在 Windows 中却不能[关闭]
Posted
技术标签:
【中文标题】为啥这个 c++ 代码在 Linux 中可以正常工作,但在 Windows 中却不能[关闭]【英文标题】:Why does this c++ code work properly in Linux but not in Windows [closed]为什么这个 c++ 代码在 Linux 中可以正常工作,但在 Windows 中却不能[关闭] 【发布时间】:2015-04-28 21:47:54 【问题描述】:我是一名陷入困境的 C++ 学生。我的代码似乎在功能上可以工作(嗯,我已经完成的部分)但是内存分配中的某些东西在 Windows 7 中搞砸了,但在我运行 ubuntu 的古老笔记本电脑中却没有。当然,代码是在 Windows 7 上编译和测试的。
我将发布整个代码,以便您可以编译它并尝试突出显示卡住的相关位。该代码旨在打开一个文件,将文件解析为一个二维数组,然后使用该数组填充一些其他所需的数组,以按年查找最大值,依此类推。
代码后面是用于填充 mainArray 的文本文件,称为“Energy.dat”。如有必要,我还可以上传 .doc 文件以充分解释所有这些背后的目的。要点是,文件中的不同列是不同类型的能量,列索引对应数组“types[]”中的值。我需要将多个列加到单独的数组“fossilArray”、“renewableArray”和“totalArray”中。其中,在 Windows 7 中,偶尔会充满垃圾值(即 4.3e206),以及不同计算机上的不同值。发生这种情况的位置显示在 cmets 中。
我问过我的教授,他认为这是操作系统问题。这似乎是真的,但我缺乏解决它的知识和理解。任何意见都会有所帮助。
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
//#include <windows.h>
using namespace std;
void drawLine();
int returnMaxIndex(double mainArray[][14], int j);
int returnMaxIndex(double array[]);
int main()
string yearString; char answer = '@'; char Eanswer = '@'; //initialize to a value to get inside while loop
long int year = 1979; //same thing here
int i, j;
double temp;
double mainArray[29][14]; //EVERY VALUE LOADS PERFECTLY
double fossilArray[29]; //index 9, 12, 13 get junk values
double renewableArray[29]; //also gets occasional junk values
double totalArray[29]; //same here
string types[14] = "Year", "Coal", "Petroleum", "Natural Gas", "Other Gases", "Nuclear",
"Hydroelectric (pumped storage)", "Hydroelectric", "Biomass-Wood",
"Biomass-Other", "Geothermal", "Solar", "Wind", "Other";
ifstream file("Energy.dat");
if(file.fail())
return -1;
for(i = 0; i < 29; i++) //load main array
for(j = 0; j < 14; j++)
file >> mainArray[i][j];
for(i = 0; i < 29; i++) //load fossil sum array
for(j = 1; j < 5; j++)
fossilArray[i] += mainArray[i][j];
for(i = 0; i < 29; i++) //load renewable energy array
for(j = 6; j < 13; j++)
renewableArray[i] += mainArray[i][j];
for(i = 0; i < 29; i++) //load total energy array
for(j = 1; j < 14; j++)
totalArray[i] += mainArray[i][j];
// for(int i = 0; i < 29; i++)
// for(int j = 0; j < 14; j++)
// cout << fixed << setprecision(0) << setw(10) << mainArray[i][j] << " "; if(j == 6) cout << endl;
// cout << endl << endl;
// for(i = 0; i < 29; i++)
cout << setw(20) << std::right << fixed << fossilArray[12] << endl;
cout << "A. Shows the energy output for a year you select between 1980 and 2008"<< endl
<< "B. Gives you the year in which each form of energy has its peak output" << endl
<< "C. Create an excel compatible output file for the total of a type of energy \n\tOR as a percentage by year" << endl;
while (static_cast<int>(answer) < 65 || (static_cast<int>(answer) > 67 && static_cast<int>(answer) < 97) || static_cast<int>(answer) > 99)
cout << "Please choose an option A, B, or C: ";
cin.get(answer);
drawLine();
switch(answer)
case 'A': case 'a':
cout << "Enter the year in which you want to see the energy output(1980 - 2008): ";
while(year < 1980 || year > 2008)
cout << endl;
getline(cin, yearString);
year = strtol(yearString.c_str(), NULL, 10);
drawLine();
if(year == 1980) i = 0; if(year == 1981) i = 1; if(year == 1982) i = 2; if(year == 1983) i = 3; if (year == 1984) i = 4; if(year == 1985) i = 5;
if(year == 1986) i = 6; if(year == 1987) i = 7; if(year == 1988) i = 8; if(year == 1989) i = 9; if (year == 1990) i = 10; if(year == 1991) i = 11;
if(year == 1992) i = 12; if(year == 1993) i = 13; if(year == 1994) i = 14; if(year == 1995) i = 15; if (year == 1996) i = 16; if(year == 1997) i = 17;
if(year == 1998) i = 18; if(year == 1999) i = 19; if(year == 2000) i = 20; if(year == 2001) i = 21; if (year == 2002) i = 22; if(year == 2003) i = 23;
if(year == 2004) i = 24; if(year == 2005) i = 25; if (year == 2006) i = 26; if(year == 2007) i = 27; if(year == 2008) i = 28;
cout << "The energy produced in the year " << year << " was...(in megawatt hours) % of total" << endl;
for (j = 1; j < 14; j++)
double temp = mainArray[i][j]; double temp2 = totalArray[i];
cout << setw(36)<< std::left << types[j] << fixed << setprecision(3) << std::right << setw(16) << mainArray[i][j] << " " << setprecision(3) << (temp*1.0/temp2)*100.0 << endl;
//(abs(mainArray[i][j]))/(totalArray[j])
drawLine();
break;
case 'B': case 'b':
cout << "A. Coal B. Petroleum C. Natural Gas D. Other Gas E. Nuclear" << endl
<< "F. Hydro Pumped Storage G. Hydroelectric H. Biomass-wood" << endl;
drawLine();
cout << "I. Biomass-waste J. Geothermal K. Solar L. Wind M. Other energy" << endl
<<"N. All fossil fuels O. All renewable P. Grand total" <<endl;
drawLine();
cout << "Enter the type of energy you want to see the the peak output year: ";
while(static_cast<int>(Eanswer) < 65 || (static_cast<int>(Eanswer) > 80 && static_cast<int>(Eanswer) < 97) || static_cast<int>(Eanswer) > 112)
cin.get(Eanswer);
switch(Eanswer)
case 'A': case 'a': j = 1; break; case 'B': case 'b': j = 2; break; case 'C': case 'c': j = 3; break; case 'D': case 'd': j = 4; break;
case 'E': case 'e': j = 5; break; case 'F': case 'f': j = 6; break; case 'G': case 'g': j = 7; break; case 'H': case 'h': j = 8; break;
case 'I': case 'i': j = 9; break; case 'J': case 'j': j = 10; break; case 'K': case 'k': j = 11; break; case 'L': case 'l': j = 12; break;
case 'M': case 'm': j = 13; break; case 'N': case 'n': j = 14; break; case 'O': case 'o': j = 15; break; case 'P': case 'p': j = 16; break;
if(j > 0 && j < 14)
i = returnMaxIndex(mainArray, j);
cout << "The year of maximum energy production for " << types[j] << " is " << mainArray[i][0];
if(j == 14)
i = returnMaxIndex(fossilArray);
cout << "The year of maximum energy production for fossil fuels is " << mainArray[i][0];
if(j == 15)
i = returnMaxIndex(renewableArray);
cout << "The year of maximum energy production for renewable energy is " << mainArray[i][0];
if(j == 16)
i = returnMaxIndex(totalArray);
cout << "The year of maximum energy production for total energy production is " << mainArray[i][0];
break;
case 'C': case 'c':
break;
int returnMaxIndex(double array[])
double temp = array[0];
for(int i = 0; i < 29; i++)
if(temp < array[i])
temp = array[i];
cout << temp << endl;
for(int i = 0; i < 29; i++)
if (temp == array[i])
return i;
int returnMaxIndex(double mainArray[][14], int j)
int k = 0;
double temp = labs(mainArray[k][j]);
for(int i = 1; i < 29; i++)
if(temp < labs(mainArray[i][j]))
temp = labs(mainArray[i][j]);
for(int i = 0; i < 29; i++)
if (temp == labs(mainArray[i][j]))
return i;
void drawLine()
cout << "---------------------------------------------------------------------------" << endl;
Energy.dat 的内容
1980 1161562368 245994189 346239900 0 251115575 0 276020970 275366 157797 5073079 0 0 0
1981 1203203232 206420775 345777173 0 272673503 0 260683544 245201 122628 5686163 0 0 0
1982 1192004204 146797490 305259749 0 282773248 0 309212893 195940 124979 4842865 0 0 0
1983 1259424279 144498593 274098458 0 293677119 0 332129735 215867 162745 6075101 0 2668 0
1984 1341680752 119807913 297393596 0 327633549 0 321150245 461411 424540 7740504 5248 6490 0
1985 1402128125 100202273 291945965 0 383690727 0 281149418 743294 639578 9325230 10630 5762 0
1986 1385831452 136584867 248508433 0 414038063 0 290844099 491509 685234 10307954 14032 4189 0
1987 1463781289 118492571 272620803 0 455270382 0 249694973 783088 693941 10775461 10497 3541 0
1988 1540652774 148899561 252800704 0 526973047 0 222939683 935986 738258 10300079 9094 871 0
1989 1562366197 159004961 297295127 454066 529354717 0 269189209 5582109 7742914 14593443 250601 2112043 282046
1990 1572108922 118863929 309486351 621112 576861678 -3507741 289753124 7032446 11499927 15434271 367087 2788600 11913
1991 1568845635 112798164 317773359 719074 612565087 -4541435 286019443 7735675 13853928 15966444 471765 2950951 402581
1992 1597713819 92237912 334274122 1212475 618776263 -4176582 250015684 8491095 15923885 16137962 399640 2887523 479806
1993 1665464154 105425325 342221829 966508 610291214 -4035572 277523663 9151852 16223338 16788565 462452 3005827 407651
1994 1666276091 98676618 385689325 1092023 640439832 -3377825 254004826 9232281 16983843 15535453 486622 3447109 239129
1995 1686056319 68145851 419178592 1926832 673402123 -2725131 305410435 7596774 17985777 13378258 496821 3164253 213275
1996 1771972991 74782864 378757294 1341140 674728546 -3088078 341158836 8386379 17816200 14328684 521205 3234069 201222
1997 1820761761 86479050 399595822 1533366 628644171 -4039905 350647962 8680229 18484565 14726102 511168 3288035 62807
1998 1850193304 122211090 449292578 2314896 673702104 -4467280 317866620 8608130 19233174 14773918 502473 3025696 158942
1999 1858617724 111539127 472995956 1606583 728254124 -6096899 314663058 8960705 19493050 14827013 495082 4487998 138942
2000 1943111290 105192123 517977999 2027956 753892940 -5538860 271337693 8916073 20307087 14093158 493375 5593261 124885
2001 1882826136 119148891 554939683 585791 768826308 -8823445 213749291 8293796 12944430 13740501 542755 6737332 6541565
2002 1910612813 89733266 607683246 1969851 780064087 -8742928 260491387 9009328 13145020 14491310 554831 10354279 9091465
2003 1952713826 113697198 567303392 2647093 763732695 -8535065 271511659 9527678 13807633 14424231 534001 11187467 8607470
2004 1957187710 114678307 627171620 3568233 788528387 -8488210 265063848 9736404 13061787 14810975 575155 14143741 8322440
2005 1992053878 116481854 683828924 3777156 781986365 -6557788 267039777 10569886 13031085 14691745 550294 17810549 6928168
2006 1969737146 59708237 734416872 4253528 787218636 -6557842 286253922 10341481 13927432 14568029 507706 26589137 7112762
2007 1998390297 61306315 814751904 4042131 806424753 -6896352 245842714 10711289 14294304 14637213 611793 34449927 6776960
2008 1976173298 42301486 798574077 3195712 806181935 -6238403 246100140 10901875 14872266 14859238 843054 52025898 6879905
【问题讨论】:
很可能您遇到了一些未定义的行为。 99.9999999999999% 这不是操作系统问题。 (叹气) 一条不请自来的建议:如果你责怪你的操作系统或编译器,你可能错了。Which appears to be true
不,这不是真的。问题是您有未初始化的变量。一位 C++ 教授几乎应该立即发现这一点。
提示:i = year - 1980
去掉了一些分支。
【参考方案1】:
fossilArray
似乎没有在fossilArray[i] += mainArray[i][j];
行之前初始化。
同样的情况也适用于其他数组。
问题出在这一行:
double fossilArray[29];
C++ 标准不保证初始化局部变量。 Visual Studio 将未初始化的局部变量设置为某个非零值以帮助定位此问题。
【讨论】:
我在质疑这是否与学校计算机中使用的 Dev C++ 安装版本有关。我会将我的所有数组初始化为零,看看这是否会改变任何东西,直到明天早上我回到学校才能检查。感谢所有的帮助和反对票。【参考方案2】:正如其他人所指出的,您正在使用多个数组 fossilArray
、renewableArray
和 totalArray
,而从未初始化它们的值。他们不一定默认为0
,尽管他们可能更有可能在Debug
中默认为0
而不是Release
。您可以使用以下方式默认初始化您的数组:
double fossilArray[29] = ; // construct all doubles with their default value (which is 0.0).
【讨论】:
以上是关于为啥这个 c++ 代码在 Linux 中可以正常工作,但在 Windows 中却不能[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
为啥这个 C++ 程序在 Windows 上比在 Linux 上慢?