[英]Different results when running in LLVM and gcc
幾天前,筆記本電腦上運行的腳本與在另一個Linux機器上運行的腳本不同(我運行的腳本彼此相同)。 我的猜測是我的編譯器缺少某些內容。 我不知道是什么問題。
運行g ++ -v之后,這是我得到的結果:
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
在另一個Linux機器上運行g ++ -v之后,我得到:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.3/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.3-20140911/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.3-20140911/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)
以下腳本是一個腳本,用於計算從一個源頂點到所有頂點的最短路徑,在本例中為頂點1。此腳本的輸出列出每個單個頂點及其到源的最短距離。 有問題的輸入文件也在下面列出。 這是我有問題的腳本:
#include <iostream>
#include <sstream>
#include <fstream>
#include <list>
#include <limits>
using namespace std;
struct Node{
int dist;
int v;
bool operator==(const Node& a){
return dist == a.dist;
}
bool operator!=(const Node& a){
return (!(dist == a.dist));
}
bool operator<(const Node& a){
return dist < a.dist;
}
bool operator>(const Node& a){
return a.dist < dist;
}
int operator+(const Node& a){
return a.dist+dist;
}
};
struct cheapHeap{
int curSize;
Node * minHeap;
};
class DijkstraSolution{
private:
int size;
int sourceV;
list<Node> *container; //this is the container to hold input data
int *results; //this is the array to hold final results
cheapHeap ch; //this is the array that holds each dist looked at
public:
DijkstraSolution(string fname, int inSourceV, int inSize): size(inSize), sourceV(inSourceV){
//read the file and initialize the list<Node> container
container = new list<Node>[size];
ifstream infile;
infile.open(fname.c_str());
string line = "";
if (infile){
while (getline(infile, line)){
istringstream iss(line);
int vertex;
int v;
int dist;
iss >> vertex;
while(iss >> v >> dist){
Node edges;
edges.dist = dist;
edges.v = v;
container[vertex].push_back(edges);
}
}
}
infile.close();
//initialization of results and minHeap array
int maxVal = numeric_limits<int>::max();
results = new int[size];
ch.curSize = size-1;
ch.minHeap = new Node[size];
for (int i = 0; i < size; i++){
results[i] = maxVal;
ch.minHeap[i].dist = maxVal;
}
results[sourceV] = 0;
ch.minHeap[sourceV].dist = 0;
ch.minHeap[sourceV].v = sourceV;
}
//find min of all nodes inserted into minHeap
Node findMin(){
Node minimum;
minimum.dist = numeric_limits<int>::max();
minimum.v = 0;
for (int i=1; i <= size; i++){
if ((ch.minHeap[i] < minimum) && (ch.minHeap[i].dist != -1)){
minimum = ch.minHeap[i];
}
}
ch.minHeap[minimum.v].dist = -1;
ch.curSize--;
return minimum;
}
//for every min that findMin() spits out, insert this min into the results array
int * dijkstra(){
while (ch.curSize != 0){
Node minimum = findMin();
results[minimum.v] = minimum.dist;
for (list<Node>::iterator it = container[minimum.v].begin(); it != container[minimum.v].end(); it++){
if ((*it)+minimum < ch.minHeap[(*it).v].dist){
Node inserted;
inserted.dist = *it + minimum;
inserted.v = (*it).v;
ch.minHeap[(*it).v] = inserted;
}
}
}
return results;
}
//this prints the contents from the file input
void printContainer(){
for (int i=1; i < size; i++){
cout << i << " ";
list<Node>::iterator iter;
for (iter = container[i].begin(); iter != container[i].end(); iter++){
cout << iter -> v << " " << iter -> dist << " ";
}
cout <<endl;
}
}
//this prints out the contents from the results array
void printResults(){
for (int i = 1; i < size; i++){
cout << i << " -> " << results[i] << endl;
}
}
};
int main(){
DijkstraSolution ds("pa5_test1.txt", 1, 9);
ds.dijkstra();
ds.printResults();
}
這是我的輸入文件:
1 2 1 8 2
2 1 1 3 1
3 2 1 4 1
4 3 1 5 1
5 4 1 6 1
6 5 1 7 1
7 6 1 8 1
8 7 1 1 2
這是我從Mac上獲得的輸出,這是不正確的:
1 -> 0
2 -> 2147483647
3 -> 2147483647
4 -> 2147483647
5 -> 2147483647
6 -> 2147483647
7 -> 2147483647
8 -> 2147483647
這是我從另一個Linux盒中獲得的輸出,這是正確的:
1 -> 0
2 -> 1
3 -> 2
4 -> 3
5 -> 4
6 -> 4
7 -> 3
8 -> 2
顯然,我的編譯器沒有更新results數組。 顯然,它將第一項更新為0,然后停止更新。 這真的很奇怪,因為我在不同的機器上運行了完全相同的代碼,並且結果與Linux機器相同,所以我猜它是我的編譯器。 令人沮喪的是我沒有收到任何錯誤,所以我什至不知道從哪里開始尋找答案。 另外,不確定是否與此相關,但是當我啟動gdb時,它不會調試作為類編寫的腳本。 它只調試具有main和函數的代碼,當我編寫實例化某個類的main函數時會跳閘。 如果我的編譯器確實壞了,關於如何重建它的任何建議(嬰兒步驟)? 抱歉,很長的帖子。
一個問題是:
for (int i=1; i <= size; i++){ // <-- Index goes out of bounds here
if ((ch.minHeap[i] < minimum) && (ch.minHeap[i].dist != -1)){
minimum = ch.minHeap[i];
您正在循環使用size
,但是minHeap[size]
是一個越界訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.