-1

I am getting segmentation fault: 11

Now,

char array[10000000] reserves 10000000 bytes or around 9.53 mb so that should not be a problem. What am I 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 ; 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;
  }
}

I ran the above code with g++ -O2 -g -v option. The following is the 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 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)
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
kalpa
  • 657
  • 2
  • 11
  • 22
  • 1
    This should not be tagged C, it's C++, and both **are** different language. – Sourav Ghosh Jul 16 '15 at 13:57
  • *"char array[10000000] reserves 10000000 bytes or around 9.53 mb so that should not be a problem."* Except you're declaring it on the stack (which is generally ~1 MB) so you'll probably get a stack overflow. – Cory Kramer Jul 16 '15 at 13:59
  • @CoryKramer Ah, here you are sir. Can you please confirm that `new char[16]();` is valid in C++ or not? – Sourav Ghosh Jul 16 '15 at 14:03
  • It appears to me that your *program* did not crash; rather, the *linker* crashed. However, I cannot reproduce that crash with the slightly newer version of Apple's compiler on this computer (clang-602.0.53). Try updating Xcode. – zwol Jul 16 '15 at 14:04
  • @CoryKramer That's not on the stack, it's a global. (The default stack limit on OSX is 8MB, so that definitely *would* be the problem if it were on the stack, but it isn't.) – zwol Jul 16 '15 at 14:05
  • Addendum: the *program* does, in fact, crash when I run it, but that's not what your error messages say. – zwol Jul 16 '15 at 14:07
  • so whats the problem @zwol – kalpa Jul 16 '15 at 14:14
  • Oh, probably what Sourav said. – zwol Jul 16 '15 at 14:19

1 Answers1

1

In your code, you're calling store_output() before init_array(). This will eventually lead to the use of uninitialized memory.

You need to call init_array() before you can actually use frequency[a][b].

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261