-3

我为矩阵乘法编写了一个简单的 C++ 代码。该代码给出了 glib c 错误。将两个矩阵相乘是一个简单的矩阵乘法代码。

#include <iostream>
 using std::cerr;
 using std::cout;
 using std::endl;
#include<stdio.h>
#include <string.h>
 using std::string;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include<stdlib.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include <vector>
using namespace std;

int main()
{ 
int i,j,k;
vector<int> mult;
vector<float> dist_shell;
std::string skip;
std::string empty_line;
std::string num_elements[3];
int cluster_num;
vector<int> inter_type; //Interaction type (2-2body;3-3body etc.)
int c_mult;
float c_dist_shell;
int c_inter_type; // c stands for cin type variables frm reading file 
float unit_cell[3][3];
float factor;
int l,m,n;
float sum;

ifstream poscar("POSCAR");


float unit_cell[3][3];

    int total_atom=32;
    float frac[total_atom][3];
    float real_pos[total_atom][3];
    for (int count=0;count<total_atom;count++)
        {
    poscar>>frac[count][1]>>frac[count][2]>>frac[count][3]>>true_false[count][1]>>true_false[count][2]>>true_false[count][3];
                }     
        for (l=0;l<total_atom;l++)
                {
                for (m=0;m<3;m++)
                    {
                        sum=0;
                    for (n=0;n<3;n++)
                        {
                            sum+=frac[l][n]*unit_cell[n][m];
                        }
                    real_pos[l][m]=sum;
                    }
                }

return 0;
}

我的错误是:

*** glibc detected *** ./a.out: free(): invalid pointer: 0x00002aaaaadb8b88 ***
======= Backtrace: =========
/lib64/libc.so.6[0x2aaaab0b676e]
/lib64/libc.so.6(__libc_free+0x6c)[0x2aaaab0b7ebc]
/usr/intel/pkgs/gcc/4.2.2/lib64/libstdc++.so.6(_ZNSs7reserveEm+0x9e)[0x2aaaaac657fe]
/usr/intel/pkgs/gcc/4.2.2/lib64/libstdc++.so.6(_ZNSs6appendEPKcm+0x66)[0x2aaaaac659c6]
/usr/intel/pkgs/gcc/4.2.2/lib64/libstdc++.so.6(_ZStrsIcSt11char_traitsIcESaIcEERSt13basic_ist
reamIT_T0_ES7_RSbIS4_S5_T1_E+0x1a9)[0x2aaaaac3fdb9]
./a.out[0x4022ec]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x2aaaab068304]
./a.out(__gxx_personality_v0+0x69)[0x401019]
======= Memory map: ========
00400000-00405000 r-xp 00000000 00:109 14247450                          /nfs/site/disks/summ
er11/counting/counting_main/a.out
00505000-00506000 rw-p 00005000 00:109 14247450                          /nfs/site/disks/summ
er11/counting/counting_main/a.out
00506000-00527000 rw-p 00506000 00:00 0                                  [heap]
2aaaaaaab000-2aaaaaac6000 r-xp 00000000 68:02 80782                      /lib64/ld-2.4.so
2aaaaaac6000-2aaaaaac7000 r-xp 2aaaaaac6000 00:00 0 
2aaaaaac7000-2aaaaaac9000 rw-p 2aaaaaac7000 00:00 0 
2aaaaabc6000-2aaaaabc8000 rw-p 0001b000 68:02 80782                      /lib64/ld-2.4.so
2aaaaabc8000-2aaaaacb0000 r-xp 00000000 00:18 2295681                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libstdc++.so.6.0.9
2aaaaacb0000-2aaaaadaf000 ---p 000e8000 00:18 2295681                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libstdc++.so.6.0.9
2aaaaadaf000-2aaaaadb6000 r--p 000e7000 00:18 2295681                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libstdc++.so.6.0.9
2aaaaadb6000-2aaaaadb8000 rw-p 000ee000 00:18 2295681                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libstdc++.so.6.0.9
2aaaaadb8000-2aaaaadcd000 rw-p 2aaaaadb8000 00:00 0 
2aaaaade8000-2aaaaae3c000 r-xp 00000000 68:02 80502                      /lib64/libm-2.4.so
2aaaaae3c000-2aaaaaf3b000 ---p 00054000 68:02 80502                      /lib64/libm-2.4.so
2aaaaaf3b000-2aaaaaf3d000 rw-p 00053000 68:02 80502                      /lib64/libm-2.4.so
2aaaaaf3d000-2aaaaaf3e000 rw-p 2aaaaaf3d000 00:00 0 
2aaaaaf3e000-2aaaaaf4b000 r-xp 00000000 00:18 1094438                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libgcc_s.so.1
2aaaaaf4b000-2aaaab04a000 ---p 0000d000 00:18 1094438                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libgcc_s.so.1
2aaaab04a000-2aaaab04b000 rw-p 0000c000 00:18 1094438                    /nfs/sc/itools/em64t
_SLES10/pkgs/gcc/4.2.2/lib64/libgcc_s.so.1
2aaaab04b000-2aaaab182000 r-xp 00000000 68:02 80494                      /lib64/libc-2.4.so
2aaaab182000-2aaaab282000 ---p 00137000 68:02 80494                      /lib64/libc-2.4.so
2aaaab282000-2aaaab285000 r--p 00137000 68:02 80494                      /lib64/libc-2.4.so
2aaaab285000-2aaaab287000 rw-p 0013a000 68:02 80494                      /lib64/libc-2.4.so
2aaaab287000-2aaaab28e000 rw-p 2Abort

我想弄清楚我的代码的另一部分是否有错误。更改数据类型也无济于事。感谢所有可以帮助我发现错误的人。谢谢。

4

1 回答 1

0

解决方案是,在从文件中读取某些行时,getline 有时会读取行尾而不是下一行。所以检查跳行部分到底包含什么是很重要的。这最终导致了这个错误。该错误不是此代码的一部分。

于 2012-11-05T23:43:40.683 回答