[visual-studio]户主减少算法的 C 代码编译中的错误

发布时间: 2014/3/1 2:12:14
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我写了个 C 代码为户主减少 (转换到一个三对角矩阵矩阵),但为编译它给以下错误:

第一次机会异常,在 eig3.exe 0x0139399d: 0xC0000005: 读取位置 0x001ac240 的访问冲突。

未处理的异常,在 eig3.exe 0x0139399d: 0xC0000005: 读取位置 0x001ac240 的访问冲突。

我不知道此错误的原因是什么以及如何处理它。谢谢你的任何帮助。

    #include <stdio.h>
    #include <math.h>

    void tred2(float a [25][25], int n, float d[25], float e[25]);

    int main(void)
    {
    float p [25][25] = {
    {2555.00000000000   ,922.005311398953   ,637.379419667162   ,620.854753485905   ,911.254051182594   ,1014.66270354426   ,812.462795013727   ,1072.60166757533   ,1096.81527844247   ,804.789434963328   ,787.686049787311   ,149.217640419107   ,1566.64395026266   ,1588.48393430107   ,1492.94885424606   ,991.938328585146   ,976.827808746522   ,532.528819818273   ,722.768915905778   ,763.614122733850   ,1579.60048937499   ,1001.71798224119   ,1302.39870957033   ,1166.44619446079   ,1560.95252809953}
    ,{922.005311398953  ,2555.00000000000   ,948.729365770484   ,1252.25139249907   ,626.461731508273   ,1872.91731556186   ,970.579881474055   ,1404.21684991920   ,1697.79261702870   ,1660.75191655532   ,885.912360323321   ,-826.559236092200  ,1064.22706203730   ,1297.73910149212   ,1149.30978063813   ,1432.76348929109   ,1203.23027209744   ,898.925016935900   ,1731.53004042905   ,1501.47080150068   ,1293.89882724464   ,292.711988397453   ,1583.90052201935   ,742.612048219667   ,1412.09079625792}
    ,{637.379419667162  ,948.729365770484   ,2555.00000000000   ,1606.45631882269   ,1205.09485268865   ,1214.09523991542   ,1248.34335302171   ,1326.14561611573   ,1230.85115422645   ,1669.59745355344   ,1752.88432738528   ,494.100155408190   ,1080.00543503926   ,1067.78673262180   ,975.062883004617   ,1405.11899504866   ,1247.48007973090   ,1676.09723626932   ,1491.90674867064   ,1610.28518973263   ,1183.09375133136   ,1273.50753822421   ,1112.17881531081   ,933.065161216677   ,1327.24996811513}
    ,{620.854753485905  ,1252.25139249907   ,1606.45631882269   ,2555.00000000000   ,982.568289011985   ,1334.18632529062   ,1235.54945313756   ,1505.75951183069   ,1336.55410791772   ,1800.29366906675   ,1513.41736418093   ,-150.789903127066  ,1136.34003190369   ,1086.47082456098   ,856.748289849762   ,1527.62678599168   ,1367.39795727282   ,1457.68030807905   ,1843.09328177403   ,1888.54356980410   ,1152.61902697012   ,1148.79986990636   ,1319.86967136771   ,811.630450226485   ,1324.72067320582}
    ,{911.254051182594  ,626.461731508273   ,1205.09485268865   ,982.568289011985   ,2555.00000000000   ,1006.48667459184   ,1436.94802946664   ,1478.68843517829   ,1111.66962056243   ,974.358987124379   ,1682.91304943585   ,1383.69707742754   ,1429.43793210911   ,1086.57958876630   ,1228.44183248830   ,1348.82292814661   ,1325.04121890600   ,1406.19567045305   ,685.498133948134   ,961.858800804619   ,1140.83831352846   ,1741.89044735911   ,705.214461186938   ,1123.73455700641   ,1326.41608064405}
    ,{1014.66270354426  ,1872.91731556186   ,1214.09523991542   ,1334.18632529062   ,1006.48667459184   ,2555.00000000000   ,1102.75907250178   ,1532.03278329145   ,1898.33027069207   ,1743.73722868282   ,1242.26031630962   ,-374.581610868589  ,1288.97322442843   ,1522.17129337367   ,1161.22727923774   ,1473.15439922315   ,1242.38910855661   ,1044.03524006396   ,1737.82499851481   ,1562.82929669660   ,1431.63427086281   ,670.595041163938   ,1669.23104465845   ,856.549829330034   ,1592.91947720379}
    ,{812.462795013727  ,970.579881474055   ,1248.34335302171   ,1235.54945313756   ,1436.94802946664   ,1102.75907250178   ,2555.00000000000   ,1852.32912991805   ,1262.19132114632   ,1071.57700899904   ,1594.42318713543   ,976.023796565991   ,1451.14173122087   ,1011.97236259581   ,1315.19997091247   ,1936.85007229790   ,1970.04581376139   ,1558.86400917758   ,1034.22690523497   ,1174.78286756902   ,1208.69285767467   ,1255.06466912584   ,734.338320013703   ,889.143970486845   ,1232.98445944503}
    ,{1072.60166757533  ,1404.21684991920   ,1326.14561611573   ,1505.75951183069   ,1478.68843517829   ,1532.03278329145   ,1852.32912991805   ,2555.00000000000   ,1590.48563674342   ,1553.73027238155   ,1543.62072395496   ,346.707208870300   ,1671.67461719673   ,1315.57936035923   ,1368.83441512085   ,2016.29646183317   ,1974.85412946931   ,1293.01154780142   ,1487.73944205692   ,1608.85973274544   ,1474.18444486134   ,1128.61280903632   ,1178.35448619270   ,1048.09492091795   ,1524.37188755617}
    ,{1096.81527844247  ,1697.79261702870   ,1230.85115422645   ,1336.55410791772   ,1111.66962056243   ,1898.33027069207   ,1262.19132114632   ,1590.48563674342   ,2555.00000000000   ,1682.64294010254   ,1376.98769886015   ,-101.987854073015  ,1305.92004665249   ,1531.57844271590   ,1265.40439197583   ,1462.99255306792   ,1437.78441328442   ,1113.27311156055   ,1622.34725044000   ,1602.43604551484   ,1438.29417331368   ,783.469603812970   ,1472.34769797847   ,896.479716677738   ,1482.73306220344}
    ,{804.789434963328  ,1660.75191655532   ,1669.59745355344   ,1800.29366906675   ,974.358987124379   ,1743.73722868282   ,1071.57700899904   ,1553.73027238155   ,1682.64294010254   ,2555.00000000001   ,1626.25275793207   ,-420.709178738120  ,1158.29054706898   ,1305.03473704533   ,1006.33223409788   ,1528.75160521552   ,1224.36877733660   ,1490.98324372591   ,2080.17152629728   ,2030.17215288853   ,1380.88302431676   ,770.111243053728   ,1587.56786604855   ,958.393733612288   ,1533.86017999511}
    ,{787.686049787311  ,885.912360323321   ,1752.88432738528   ,1513.41736418093   ,1682.91304943585   ,1242.26031630962   ,1594.42318713543   ,1543.62072395496   ,1376.98769886015   ,1626.25275793207   ,2555.00000000000   ,1097.63026872654   ,1342.56626511327   ,1121.01344153946   ,1229.36072577169   ,1599.79042770094   ,1424.31811308624   ,1882.73938717439   ,1368.97989321395   ,1595.13452677113   ,1292.17310482938   ,1578.82248605974   ,1032.67146970819   ,968.266803228347   ,1413.73060072601}
    ,{149.217640419107  ,-826.559236092200  ,494.100155408190   ,-150.789903127066  ,1383.69707742754   ,-374.581610868589  ,976.023796565991   ,346.707208870300   ,-101.987854073015  ,-420.709178738120  ,1097.63026872654   ,2555.00000000001   ,454.968552737453   ,-15.1135182216903  ,506.850419956902   ,383.190817312292   ,564.386594524708   ,890.220013496502   ,-697.131970750369  ,-294.984935131726  ,79.3672216013743   ,1281.22039761639   ,-677.737670255204  ,194.388865612795   ,53.2942878203829}
    ,{1566.64395026266  ,1064.22706203730   ,1080.00543503926   ,1136.34003190369   ,1429.43793210911   ,1288.97322442843   ,1451.14173122087   ,1671.67461719673   ,1305.92004665249   ,1158.29054706898   ,1342.56626511327   ,454.968552737453   ,2555.00000000000   ,1645.43953157881   ,1551.51256668275   ,1656.32956655138   ,1449.67830906168   ,1035.28509796355   ,1059.71892915082   ,1142.41232626779   ,1717.55677745606   ,1489.41331600837   ,1568.01635257960   ,1322.25766815038   ,1960.69572270888}
    ,{1588.48393430107  ,1297.73910149212   ,1067.78673262180   ,1086.47082456098   ,1086.57958876630   ,1522.17129337367   ,1011.97236259581   ,1315.57936035923   ,1531.57844271590   ,1305.03473704533   ,1121.01344153946   ,-15.1135182216903  ,1645.43953157881   ,2555.00000000000   ,1633.85577074972   ,1309.83850890225   ,1116.37634369325   ,894.420409878297   ,1239.77893642942   ,1179.54818574932   ,1831.26976322878   ,1095.41625715418   ,1555.44977811449   ,1379.27681797320   ,1845.12969224029}
    ,{1492.94885424606  ,1149.30978063813   ,975.062883004617   ,856.748289849762   ,1228.44183248830   ,1161.22727923774   ,1315.19997091247   ,1368.83441512085   ,1265.40439197583   ,1006.33223409788   ,1229.36072577169   ,506.850419956902   ,1551.51256668275   ,1633.85577074972   ,2555.00000000000   ,1399.89569672702   ,1292.78494194694   ,1071.57348440573   ,919.438634412043   ,984.063775832769   ,1664.54317998763   ,1216.01431762331   ,1117.89982383136   ,1209.44972060098   ,1552.76541019022}
    ,{991.938328585146  ,1432.76348929109   ,1405.11899504866   ,1527.62678599168   ,1348.82292814661   ,1473.15439922315   ,1936.85007229790   ,2016.29646183317   ,1462.99255306792   ,1528.75160521552   ,1599.79042770094   ,383.190817312292   ,1656.32956655138   ,1309.83850890225   ,1399.89569672702   ,2555.00000000000   ,1907.39582900906   ,1533.90610292659   ,1529.08890780899   ,1528.36173717935   ,1451.45792249781   ,1185.87635311023   ,1245.92379463711   ,994.335556646288   ,1565.15016994369}
    ,{976.827808746522  ,1203.23027209744   ,1247.48007973090   ,1367.39795727282   ,1325.04121890600   ,1242.38910855661   ,1970.04581376139   ,1974.85412946931   ,1437.78441328442   ,1224.36877733660   ,1424.31811308624   ,564.386594524708   ,1449.67830906168   ,1116.37634369325   ,1292.78494194694   ,1907.39582900906   ,2555.00000000000   ,1308.07259306803   ,1290.86467986442   ,1440.81003563023   ,1356.20292784065   ,1138.03298890447   ,882.908847334965   ,880.320469711113   ,1293.91232138750}
    ,{532.528819818273  ,898.925016935900   ,1676.09723626932   ,1457.68030807905   ,1406.19567045305   ,1044.03524006396   ,1558.86400917758   ,1293.01154780142   ,1113.27311156055   ,1490.98324372591   ,1882.73938717439   ,890.220013496502   ,1035.28509796355   ,894.420409878297   ,1071.57348440573   ,1533.90610292659   ,1308.07259306803   ,2555.00000000001   ,1350.47553889761   ,1425.02183656120   ,1068.66070773005   ,1403.89403478196   ,995.052158755041   ,1033.42238484679   ,1256.14314008640}
    ,{722.768915905778  ,1731.53004042905   ,1491.90674867064   ,1843.09328177403   ,685.498133948134   ,1737.82499851481   ,1034.22690523497   ,1487.73944205692   ,1622.34725044000   ,2080.17152629728   ,1368.97989321395   ,-697.131970750369  ,1059.71892915082   ,1239.77893642942   ,919.438634412043   ,1529.08890780899   ,1290.86467986442   ,1350.47553889761   ,2555.00000000000   ,2004.23504874421   ,1315.13784792259   ,604.706253997176   ,1597.97962863405   ,817.161486914232   ,1437.88230857763}
    ,{763.614122733850  ,1501.47080150068   ,1610.28518973263   ,1888.54356980410   ,961.858800804619   ,1562.82929669660   ,1174.78286756902   ,1608.85973274544   ,1602.43604551484   ,2030.17215288853   ,1595.13452677113   ,-294.984935131726  ,1142.41232626779   ,1179.54818574932   ,984.063775832769   ,1528.36173717935   ,1440.81003563023   ,1425.02183656120   ,2004.23504874421   ,2555.00000000000   ,1293.05860991331   ,896.871006849127   ,1354.09744749331   ,820.004005040133   ,1350.99003077704}
    ,{1579.60048937499  ,1293.89882724464   ,1183.09375133136   ,1152.61902697012   ,1140.83831352846   ,1431.63427086281   ,1208.69285767467   ,1474.18444486134   ,1438.29417331368   ,1380.88302431676   ,1292.17310482938   ,79.3672216013743   ,1717.55677745606   ,1831.26976322878   ,1664.54317998763   ,1451.45792249781   ,1356.20292784065   ,1068.66070773005   ,1315.13784792259   ,1293.05860991331   ,2555.00000000000   ,1239.52387671990   ,1550.41892957873   ,1551.16127349026   ,1845.12091395182}
    ,{1001.71798224119  ,292.711988397453   ,1273.50753822421   ,1148.79986990636   ,1741.89044735911   ,670.595041163938   ,1255.06466912584   ,1128.61280903632   ,783.469603812970   ,770.111243053728   ,1578.82248605974   ,1281.22039761639   ,1489.41331600837   ,1095.41625715418   ,1216.01431762331   ,1185.87635311023   ,1138.03298890447   ,1403.89403478196   ,604.706253997176   ,896.871006849127   ,1239.52387671990   ,2555.00000000000   ,911.178694367549   ,1125.41110335920   ,1384.35828777648}
    ,{1302.39870957033  ,1583.90052201935   ,1112.17881531081   ,1319.86967136771   ,705.214461186938   ,1669.23104465845   ,734.338320013703   ,1178.35448619270   ,1472.34769797847   ,1587.56786604855   ,1032.67146970819   ,-677.737670255204  ,1568.01635257960   ,1555.44977811449   ,1117.89982383136   ,1245.92379463711   ,882.908847334965   ,995.052158755041   ,1597.97962863405   ,1354.09744749331   ,1550.41892957873   ,911.178694367549   ,2555.00000000000   ,1223.23805006692   ,1911.50640631402}
    ,{1166.44619446079  ,742.612048219667   ,933.065161216677   ,811.630450226485   ,1123.73455700641   ,856.549829330034   ,889.143970486845   ,1048.09492091795   ,896.479716677738   ,958.393733612288   ,968.266803228347   ,194.388865612795   ,1322.25766815038   ,1379.27681797320   ,1209.44972060098   ,994.335556646288   ,880.320469711113   ,1033.42238484679   ,817.161486914232   ,820.004005040133   ,1551.16127349026   ,1125.41110335920   ,1223.23805006692   ,2555.00000000000   ,1512.05329660110}
    ,{1560.95252809953  ,1412.09079625792   ,1327.24996811513   ,1324.72067320582   ,1326.41608064405   ,1592.91947720379   ,1232.98445944503   ,1524.37188755617   ,1482.73306220344   ,1533.86017999511   ,1413.73060072601   ,53.2942878203829   ,1960.69572270888   ,1845.12969224029   ,1552.76541019022   ,1565.15016994369   ,1293.91232138750   ,1256.14314008640   ,1437.88230857763   ,1350.99003077704   ,1845.12091395182   ,1384.35828777648   ,1911.50640631402   ,1512.05329660110   ,2555.00000000000}
    };

    int myLength = sizeof(p);
    float dd [25] = { 0.0 };
    float ee [25] = { 0.0 };
    tred2(p,myLength,dd,ee);
    }

    void tred2(float a [25][25], int n,float d[25], float e[25])
    //void tred2(void)
    {
    int l,k,j,i;
    float scale,hh,h,g,f;
    for (i=n;i>=2;i--) {
    l=i-1;
    h=scale=0.0;
    if (l > 1) {
    for (k=1;k<=l;k++)
    scale += fabs(a[i][k]);
    if (scale == 0.0) /*Skip transformation.*/
    e[i]=a[i][l];
    else {
    for (k=1;k<=l;k++) {
    a[i][k] /= scale; /*Use scaled a’s for transformation.*/
    h += a[i][k]*a[i][k]; /*Form ? in h.*/
    }
    f=a[i][l];
    g=(f >= 0.0 ? -sqrt(h) : sqrt(h));
    e[i]=scale*g;
    h -= f*g; /*Now h is equation*/
    a[i][l]=f-g; /*Store u in the ith row of a.*/
    f=0.0;
    for (j=1;j<=l;j++) {
    /* Next statement can be omitted if eigenvectors not wanted */
    a[j][i]=a[i][j]/h; /*Store u/H in ith column of a.*/
    g=0.0; /*Form an element of A · u in g.*/
    for (k=1;k<=j;k++)
    g += a[j][k]*a[i][k];
    for (k=j+1;k<=l;k++)
    g += a[k][j]*a[i][k];
    e[j]=g/h; /*Form element of p in temporarily unused element of e.*/
    f += e[j]*a[i][j];
    }
    hh=f/(h+h); /*Form K*/
    for (j=1;j<=l;j++) { /*Form q and store in e overwriting p.*/
    f=a[i][j];
    e[j]=g=e[j]-hh*f;
    for (k=1;k<=j;k++) /*Reduce a*/
    a[j][k] -= (f*e[k]+g*a[i][k]);
    }
    }
    } else
    e[i]=a[i][l];
    d[i]=h;
    }
    /* Next statement can be omitted if eigenvectors not wanted */
    d[1]=0.0;
    e[1]=0.0;
    /* Contents of this loop can be omitted if eigenvectors not wanted except for statement d[i]=a[i][i]; */
    for (i=1;i<=n;i++) { /*Begin accumulation of transformation matrices.*/
    l=i-1; 
    if (d[i]) { /*This block skipped when i=1.*/
    for (j=1;j<=l;j++) {
    g=0.0;
    for (k=1;k<=l;k++) /*Use u and u/H stored in a to form P·Q.*/
    g += a[i][k]*a[k][j];
    for (k=1;k<=l;k++)
    a[k][j] -= g*a[k][i];
    }
    }
    d[i]=a[i][i]; /*This statement remains.*/
    a[i][i]=1.0; /*Reset row and column of a to identity*/
    for (j=1;j<=l;j++) a[j][i]=a[i][j]=0.0; /*matrix for next iteration.*/
    }
    }

解决方法 1:

至少,看起来你会需要这个:

int myLength = 25;

而不是:

int myLength = sizeof(p);

sizeof(p)将不会返回 25 ,而是 25 * 25 * sizeof(float)

此外,你必须意识到数组索引去从 0n - 1 (凡 n 是数组中的项的数量),而不是从 1n 。因此,当您循环访问数组的内容,要确保在启动 0 ,并停止在 n - 1 。例如:

for (i = 0; i < n; ++i) {
    /* do something with array[i] */
}
官方微信
官方QQ群
31647020