c - When -lm is linked statically, segmentation fault on run time - dumps core on 14.04.1 LTS on 64 bit AMD -
the codes in c has been running fine on ubuntu 14.04.1 lts on 64 bit amd c50x2. when "-lm" linked statically , ran same test on same environment, dumps core on run time. passed ldd test. only thing changed "-lm" statically linked:
gcc .... -static -lm later tried full path "-lm" library - dumped core again.
tried trace command:
execve("./mypro", ["./mypro"], [/* 61 vars */]) = 0 uname({sys="linux", node="acer", ...}) = 0 brk(0) = 0x2668000 brk(0x26691c0) = 0x26691c0 arch_prctl(arch_set_fs, 0x2668880) = 0 readlink("/proc/self/exe", "/home/owner/wfiles/mypro", 4096) = 23 brk(0x268a1c0) = 0x268a1c0 brk(0x268b000) = 0x268b000 access("/etc/ld.so.nohwcap", f_ok) = -1 enoent (no such file or directory) write(2, "expecting 2 argume"..., 35expecting 2 argument ) = 35 exit_group(1) = ? +++ exited 1 +++
update: 1) had 1 library. also, order compiled:
gcc a.c b.c -o myprogramexe -static -lm
2) ran gdb , bactrace - issue possibly linux , malloc. part of code taken numerical recipie in c (nrc) used -
void *malloc(int);
it incompatible linux , in lieu of it, added include file. segmentation fault occurs on function below nrc, says free():
void free_vector(v,nl,nh) float *v; int nl, nh; /* frees float vector allocated vector(). */ { free((char*) (v+nl)); }
the following function used nrc create vector:
float *vector (nl,nh) int nl, nh; { float *v; v=(float *)malloc((unsigned) (nh-nl+1)*sizeof(float)); if (!v) nrerror("allocation failure in vector()"); return v-nl; }
how can fix issue - why happens when there static link on same build environ?
update2: found revised codes on nrc web site - prob not resolved. http://www.nr.com/pubdom/nrutil.c.txt
void free_vector(float *v, long nl, long nh) /* free float vector allocated vector() */ { free((char*) (v+nl-1)); } float *vector(long nl, long nh) /* allocate float vector subscript range v[nl..nh] */ { float *v; v=(float *)malloc((size_t) ((nh-nl+1+1)*sizeof(float))); if (!v) nrerror("allocation failure in vector()"); return v-nl+1; }
return v-nl;
causes undefined behaviour.
pointers may point element of array (or 1 past last element). writing v - nl
tries form pointer middle of nowhere.
it idea redesign code not rely on undefined behaviour.
you mention void *malloc(int);
, bug. proper signature void *malloc(size_t);
.
in case should write #include <stdlib.h>
instead, avoid possibility of error.
Comments
Post a Comment