i getting segmentation fault: 11
now,
char array[10000000] reserves 10000000 bytes or around 9.53 mb should not problem. doing wrong?
code:
#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 ; < 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 (int i=start-1; i<end; i++){ // start-1 array starts 1 index not 0 // ->given condition (array[i]==0?array[i] =1 : array[i] =0); } } inline void store_output(){ //done for(int i=0; < 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 (int i=0; < t ; i++){ for(int j=0; j < 15 ; j++){ std::cout << frequency[i][j] << " "; } std::cout << frequency[i][15] << std::endl; } } i ran above code g++ -o2 -g -v option. following output
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 main executable file 'test' architecture x86_64 clang: error: unable execute command: segmentation fault: 11 clang: error: linker command failed due signal (use -v see invocation)
in code, you're calling store_output() before init_array(). lead use of uninitialized memory.
you need call init_array() before can use frequency[a][b].
Comments
Post a Comment