分段故障:11

时间:2015-07-16 13:52:18

标签: c++ clang clang++ llvm-clang

我得到了分段错误:11

现在,

char array [10000000]保留10000000字节或大约9.53 mb,这应该不是问题。 我做错了什么?

代码:

#include <iostream>

#define VERSION 0
#define ARRAY_SIZE 10000000
#define HEX_ARRAY  2500000

//variables
char array[ARRAY_SIZE]={};
char *frequency[16]={};
int T,N,freq;

//function declarations
inline void flip(int start, int end);
inline void store_output();
inline void init_array();
inline void show_output();

int main(int argc, char *argv[]){
  int x,y;
  //input
  std::cin >> T; // number of test cases
  for(freq =0 ; freq <T ; freq++){
    std::cin >> N; // number of operations
    for(int i=0 ; i < N-1 ; i++){
      flip(std::cin >>x,std::cin >>y); //check arg if prob
    }
    store_output();
    init_array();
  }
  show_output();
  return 0;
}

inline void flip(int start, int end){ // DONE
  for (int i=start-1; i<end; i++){
  // start-1 array starts from 1 index not 0
  // ->given condition
    (array[i]==0?array[i] =1 : array[i] =0);
  }
}

inline void store_output(){ //DONE
  for(int i=0; i < HEX_ARRAY;i+=4){
    int hex= array[i] + 2*array[i+1] + 4*array[i+2]
    + 8*array[i+3];
    frequency[freq][hex]++;
  }
}    

inline void init_array(){ // DONE
for(int i=0;i < ARRAY_SIZE;i++){
    array[i] = 0;
  }
 *frequency = new char[16](); //// XXXX    
}

inline void show_output(){ // DONE
for (int i=0; i < T ; i++){
  for(int j=0; j < 15 ; j++){
    std::cout << frequency[i][j] << " ";
    }
    std::cout << frequency[i][15] << std::endl;
  }
}

我使用g ++ -O2 -g -v选项运行上面的代码。 以下是输出

Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -disable-free -disable-llvm-verifier -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 242 -v -dwarf-column-info -resource-dir /Library/Developer/CommandLineTools/usr/bin/../lib/clang/6.1.0 -stdlib=libc++ -O2 -fdeprecated-macro -fdebug-compilation-dir /Users/calpo/Desktop -ferror-limit 19 -fmessage-length 204 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/60/zg6r9xqx0r3113lp2j6w4kqm0000gn/T/test-aacf88.o -x c++ test.cpp
clang -cc1 version 6.1.0 based upon LLVM 3.6.0svn default target x86_64-apple-darwin14.1.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/bin/../lib/clang/6.1.0/include
 /Library/Developer/CommandLineTools/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o -g test /var/folders/60/zg6r9xqx0r3113lp2j6w4kqm0000gn/T/test-aacf88.o -lc++ -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/6.1.0/lib/darwin/libclang_rt.osx.a
ld: can't link with a main executable file 'test' for architecture x86_64
clang: error: unable to execute command: Segmentation fault: 11
clang: error: linker command failed due to signal (use -v to see invocation)

1 个答案:

答案 0 :(得分:1)

在您的代码中,您在 store_output()之前呼叫init_array() 。这最终将导致使用未初始化的内存。

在实际使用init_array()之前,您需要致电frequency[a][b]