c++ - Illegal instruction (core dumped) upon compiling with SSE and -O3 options using latest g++ with a custom alignment allocator implementation -
when using g++ (gcc) 4.8.3 20140911 (red hat 4.8.3-7) compile following piece of code using compiling command "g++ -g -fno-omit-frame-pointer -msse2 -mssse3 -o3 memory.cpp", executable raises "illegal instruction (core dumped)" upon execution.
it compiles , runs without problems using same exact compiler flags using older g++ version. compiles , runs without problems when compiler flags "-mssse3 -o3" dropped or replaced lower optimization level such "-o2".
if usage both old , newer g++ compiler, having compiler flags "-msse2 -mssse3 -o3", , having portable aligned memory allocator requirement, options exist? there simple mistake in following piece of code fixed? finally, why did error exist?
when using gdb, line triggers error is: "memory[i] = (unsigned char)i;".
thanks in advance.
#include <iostream> using std::cerr; using std::cout; using std::endl; using std::flush; #include <stdlib.h> void *aligned_alloc(int alignment, int size){ const int pointer_size = sizeof(unsigned char *); const int requested_size = size + alignment - 1 + pointer_size; unsigned char *base = (unsigned char *)malloc(requested_size); if (base == null) return null; unsigned char *start = base + pointer_size; const int trim_offset = (int)(((unsigned long long)(start+alignment-1)) & (alignment-1)); unsigned char *aligned = start + alignment - 1 - trim_offset; *(unsigned char **)(aligned-pointer_size) = base; return aligned; } void aligned_free(void **aligned){ if (*aligned == null) return; unsigned char *base = *(unsigned char **)((unsigned char *)(*aligned) - sizeof(unsigned char *)); free(base); *aligned = null; } int main(){ unsigned char *memory = (unsigned char *)aligned_alloc(16, 120); if (memory == null){ cout<<"error: unable allocate memory."<<endl; exit(1); } (int i=0; i<120; i++){ memory[i] = (unsigned char)i; } aligned_free((void **)&memory); return 0; }
this caused cpu didn't support ssse3 (supplemental streaming simd extensions 3) instruction subset (in particular, 3ghz p4 of flavor) trying run code compiled cpus support instruction subset; dropping -mssse3
flag gcc command line should enough cause offending instructions go away.
Comments
Post a Comment