incompatible pointer type - warnings
물리학관련 계산 프로그램중에 사용되는 라이브러리 화일을 만드는 과정에
아래와 같은 경고 메세지가 나오고, 어쨌거나 컴파일된 libgk.a 를 링크시
켜 필요한 프로그램을 gdb에서 돌렸을때 아래와 같은 결과가 나왔습니다.
gcc 2.7.2.3 과 g77 2.91.57 버전에서는 잘 돌아갔다는데...
지금 사용하는 오에스는 레드헷7.1입니다. 고수님들 아래의 메세지들만
보시고도 무엇이 문제인지 알아낼 수 있나요? 아니면 화일들을 직접 봐야
만 하나요??? 많은 조언 부탁합니다.
[0532pm libgk]$ls
deriv.c gaussdist.c Makefile newruku.c
run_coeffs.c
dgauss.c getrange.c malloc.c newton.c solve.c
dgmlt.c hbook2maple.c malloc.h powint.c stuetz.c
diag.c hbook.c minimize.c recursive_zero.c test.c
falsepos2.c hplot.c mkmatrix.c ruku.c testeisp.c
feispac.c koeffs.dat mops.c rukutest.c
[0532pm libgk]$pwd
/home/snowman/photino/NLSM/libgk
[0532pm libgk]$make
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" diag.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" deriv.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" hbook.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" hplot.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" mkmatrix.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" mops.c
mops.c In function `Rotate'
mops.c86 warning passing arg 1 of `freematrix' from incompatible
pointer type
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" minimize.c
minimize.c In function `linmin'
minimize.c625 warning passing arg 7 of `mnbrak' from incompatible
pointer type
minimize.c In function `dlinmin'
minimize.c650 warning passing arg 7 of `mnbrak' from incompatible
pointer type
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" newton.c
newton.c In function `newton_n'
newton.c338 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c348 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c405 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c In function `dnewton_n'
newton.c479 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c489 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c522 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c536 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c547 warning passing arg 1 of `freematrix' from
incompatible pointer type
newton.c554 warning passing arg 1 of `freematrix' from
incompatible pointer type
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" powint.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" ruku.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" solve.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" getrange.c
gcc -c -O2 -I/home/snowman/photino/NLSM/include -
DKOEFF="\"/usr/local/lib/koeffs.dat\"" gaussdist.c
ar cr libgk.a diag.o deriv.o hbook.o hplot.o mkmatrix.o mops.o
minimize.o
newton.o powint.o ruku.o solve.o getrange.o gaussdist.o
ranlib libgk.a
[0532pm libgk]$ls
deriv.c gaussdist.o libgk.a mops.o rukutest.c
deriv.o getrange.c Makefile newruku.c
run_coeffs.c
dgauss.c getrange.o malloc.c newton.c solve.c
dgmlt.c hbook2maple.c malloc.h newton.o solve.o
diag.c hbook.c minimize.c powint.c stuetz.c
diag.o hbook.o minimize.o powint.o test.c
falsepos2.c hplot.c mkmatrix.c recursive_zero.c testeisp.c
feispac.c hplot.o mkmatrix.o ruku.c
gaussdist.c koeffs.dat mops.c ruku.o
[0533pm libgk]$su -c "make install"
Password
cp libgk.a /usr/local/lib
cp koeffs.dat /usr/local/lib
ldconfig
[0533pm libgk]$
[0535pm nlsm]$pwd
/home/snowman/photino/NLSM/nlsm
[0535pm nlsm]$ls
cross.f Makefile nmax.c rge.c sigmap.f
fderivoftrace.c mini.c nmu.c rgen.c
sm_higgs.c
feispack.f minimize.c nmuc2.c ruku.c solve.c
hybrd_c.c newton.c nmuc.c sigma1000.f zeroin.c
hybrd.f nlsm.h nmuc_tree.c sigma500.f
lep_bound.f nlsmn.h npot.c sigma.c
logfuns.c nlsmn.hbook radcorr.c sigma.f
main.c nmain.c recursive_zero.c sigmamin.c
[0535pm nlsm]$make nmuc
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c nmuc.c
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c mini.c
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c rgen.c
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c solve.c
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c hybrd_c.c
g77 -O2 -I/home/snowman/photino/NLSM/include -I. -c hybrd.f
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c radcorr.c
gcc -O2 -I/home/snowman/photino/NLSM/include -I. -c
fderivoftrace.c
g77 -O2 -I/home/snowman/photino/NLSM/include -I. -c feispack.f
g77 -O2 -I/home/snowman/photino/NLSM/include -I. -c cross.f
g77 -o nmuc -O2 -I/home/snowman/photino/NLSM/include -I. nmuc.o
mini.o rgen.o solve.o hybrd_c.o hybrd.o radcorr.o fderivoftrace.o
feispack.o cross.o -L/usr/local/lib -L/cern99/99/lib -
L/usr/X11R6/lib -lgk -lmathlib -lgraflib -lgrafX11 -lpacklib -
lkernlib -lgk -lX11 -lm -lc /cern99/99/lib/g77_5_21_libf2c.a
[0535pm nlsm]$
[0535pm nlsm]$./nmuc -f -v -n 1000
lambda0=99999 lambda1=99999 lambda2=99999 k=99999
0 samples
0 samples
Segmentation fault (core dumped)
[0536pm nlsm]$
[0536pm nlsm]$gdb nmuc
GNU gdb 5.0rh-5 Red Hat Linux 7.1
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.This GDB was configured as "i386-redhat-linux"...
(gdb) run -f -v -n 1000
Starting program /home/snowman/photino/NLSM/nlsm/nmuc -f -v -n 1000
lambda0=99999 lambda1=99999 lambda2=99999 k=99999
0 samples
0 samples
Program received signal SIGSEGV, Segmentation fault.
chunk_alloc (ar_ptr=0x40259f00, nb=41) at malloc.c2832
2832 malloc.c No such file or directory.
in malloc.c
(gdb) where
#0 chunk_alloc (ar_ptr=0x40259f00, nb=41) at malloc.c2832
#1 0x401b013a in __libc_malloc (bytes=36) at malloc.c2714
#2 0x080598dc in mkmatrix ()
#3 0x08053836 in get_radcorr_S ()
#4 0x08054b94 in get_radcorr ()
#5 0x0804c164 in vev2_eqs ()
#6 0x0804cc34 in find_init ()
#7 0x0804d84b in get_vev ()
#8 0x0804df9b in scale_mu_get_vevs ()
#9 0x0804a8d5 in smass ()
#10 0x0804a80c in roll_dice ()
#11 0x0804a5d2 in main ()
#12 0x4014d177 in __libc_start_main (main=0x804a1e8 , argc=5,
ubp_av=0xbffff7fc, init=0x8049800 <_init>, fini=0x80af010
<_fini>,
rtld_fini=0x4000e184 <_dl_fini>, stack_end=0xbffff7ec)
at ../sysdeps/generic/libc-start.c129
(gdb)
Re: incompatible pointer type - warnings
freematrix() 라는 함수의 원형과
전달되는 변수의 형을 한번 살펴보세여.
아니면 그쪽 관련 코드를 올리시면 다른 분들이 봐 주시겠구여..
훔... 아마 다차원배열로 쓰는 변수를 잘못 전달하신 것이 아닌가 생각
이 드는데여.. 일차원배열을 다차원처럼 쓰거나 포인터를 이용한 배열을
포인터를 이용해서 배열을 만들거나... 여러가지 가능성이 있을 듯 하네
여..
훔.. 죽은 곳이 mkmatrix()네여? 그쪽 코드만 간단히 올려보셔여..
관련코듭니다
mkmatrix.c----------------------------------------------------
/* creates a matrix as a **char variable. Elements can be accessed
like elements
of an ordinary 2 dim array after appropriate type conversion has
been
performed.
sccsID = "@(#)mkmatrix.c 1.3\t4/19/94"
*/
#include
#include
#include
char** mkmatrix(int n,int m, size_t size)
{
char *work, **workptr;
int i;
if ((work = malloc(n*m*size))==NULL)
{
sprintf(_libgk_errmsg,"mkmatrix>> cannot malloc\n");
return(NULL);
}
if ((workptr = (char**)malloc(n*sizeof(char*)))==NULL)
{
sprintf(_libgk_errmsg,"mkmatrix>> cannot malloc\n");
return(NULL);
}
for (i=0; i workptr[i] = work + i*m*size;
return (workptr);
}
void freematrix(void **A)
{
free(A[0]);
free(A);
}
-------------------------------------------------------------
warning 메세지가 뜨는 라인들 (일부)
minimize.c 에서 라인 625
#define ISNAN(x) checkfpe()
#else
#define ISNAN isnan
#endif
/*
sccsID = "@(#)minimize.c @(#)minimize.c 1.16\t10/5/95"
*/
#define ITMAX 200
#define BR_ITMAX 100
#define EPS 1.e-15
#define SQRT_EPSILON 1.e-15
#define LINMINEPS 1.e-4
#define GOLD 1.618034
#define GLIMIT 100.
#define TINY 1.e-20
#define SQR(x) ((x)*(x))
#define _SIGN(x,y) ( (y)>0 ? fabs((x)) -fabs((x)))
#define _MAX(x,y) ( (x)>(y) ? (x) (y))
/*#define fabs(x) ( (x)>0 ? (x) -(x))*/
static double (*_func)(double *x);
static double (*_dfunc)(double *x, double *y);
double dlinmin (double *p, double *xi, int n, double tol, int *ierr);
double linmin (double *p, double *xi, int n, double tol, int *ierr);
static int _n;
static int _mode;
static double *_p;
static double *_xi;
static double *_xt,*_df;
static double f1dim(double x);
static double df1dim (double x, double *g);
int mnbrak(double *ax, double *bx, double *cx, double *fa, double
*fb,
double *fc, double func(double x,...))
..............
double linmin (double *p, double *xi, int n, double tol, int *ierr)
{
double ax,xx,bx,fa,fx,fb, fret, xmin;
int j;
_n = n;
_p = p;
_xi = xi;
ax = 0.;
xx = 1.;
if (mnbrak(&ax, &xx, &bx, &fa, &fx, &fb, f1dim)) <-------625라인
{
*ierr = 1;
return(0);
}
fret = brent(ax,bx, f1dim, tol, &xmin, ierr);
for (j=0; j {
xi[j] = xmin*xi[j];
p[j] = p[j]+xi[j];
}
return(fret);
}
static double df1dim(double x, double *df)
{
int j;
double f;
*df =0. ;
for (j=0; j<_n; j++)
_xt[j] = _p[j] + x*_xi[j];
f = _dfunc(_xt,_df);
for (j=0; j<_n; j++)
*df += _df[j] * _xi[j];
return(f);
}
.........
-------------------------------------------------------
mops.c 라인 86
#include
#include
/*
sccsID = "@(#)mops.c 1.2\t4/19/94"
*/
.................
int Rotate (double **matrix, double **rot, int n)
{
int i,j,k,l;
double **Tmp;
Tmp = (double**)mkmatrix(n,n,sizeof(double));
if (Tmp==0)
{
fprintf(stderr,"Rotate>>> error allocating
memory\n");
return(1);
}
for (i=0;i for (j=i;j { Tmp[i][j]=0.;
for (k=0;k {
Tmp[i][j] += rot[k][i]*rot[k][j]
*matrix[k][k];
for (l=0;l if (matrix[k][l]==0.)
continue;
else
Tmp[i][j]+=
(rot[k][i]*rot[l][j]+rot[l][i]*rot[k][j])*matrix[k][l];
}
}
for (i=0; i for (j=i; j matrix[i][j] = matrix[j][i] = Tmp[i][j];
freematrix(Tmp); <--------------라인86
return(0);
}
-------------------------------------------------------
newton.c 라인 338
#include
#include
#include
#define EPS 1.e-6
#ifdef __alpha
#define ISNAN(x) checkfpe()
#else
#define ISNAN isnan
#endif
/*
double newton ( double f(), double x0, int *ierr )
evaluates zeros of the function f near the
starting point x0
sccsID = "@(#)newton.c 1.3\t1/27/94"
*/
double newton (double f(double), double x0, double eps, double a,
double b, int err_mode, int *ierr)
.......................
int newton_n (int n, void f(double*,double*), double *x0, double
eps,double *xmin, double *xmax)
/* returns zero of a set of n equations wth n varibales */
{
double **jacobian,tmp,*x,*fplus,*fminus,*f_old,delta_old,
delta, fnorm, fnorm_old;
double **invjacobian;
double h, xi;
double dnorm(int , double*);
float *work;
double dtmp;
int i,j, niter=0, nderiv=0, ierr=0;
if (xmin!=NULL && xmax != NULL)
{
for (i=1; i {
if (xmin[i]==xmax[i])
{
sprintf(_libgk_errmsg,"newton_n xmax[%d]
==xmin[%d]",i,i);
return (-1);
}
if (xmin[i] > xmax[i])
{
if (x0[i] > xmin[i])
xmax[i] = HUGE_VAL;
else if (x0[i] < xmax[i])
xmin[i] = -HUGE_VAL;
else
{
tmp = xmin[i];
xmin[i] = xmax[i];
xmax[i] = tmp;
}
}
if ( x0[i] < xmin[i] || x0[i] > xmax[i])
{
sprintf(_libgk_errmsg,"newton_n x0[%d]
outside range",i);
return(-1);
}
}
}
if ((jacobian = (double**)mkmatrix(n,n,sizeof(double))) ==
NULL)
{
sprintf(_libgk_errmsg,"newton_n malloc error");
return(-2);
}
if ( (f_old=(double*)malloc(4*n*sizeof(double))) == NULL)
{
freematrix(jacobian); <----------------라인338
sprintf(_libgk_errmsg,"newton_n malloc error");
return(-2);
}
fminus = f_old + n;
fplus = fminus + n;
x = fplus + n;
if ( (work=(float*)malloc(n*sizeof(float)))==NULL)
{
sprintf(_libgk_errmsg,"newton_n malloc error");
freematrix(jacobian);
free(f_old);
}
f(x0, f_old);
if ( ISNAN(dtmp) )
{
...............
등등입니다. 관심가져주셔서 감사합니다.
Re^3: 관련코듭니다
mkmatrix() 는 크게 문제없어보이구요.
mnbrak(....., f1dim) 에서 워닝 나오는 것은
f1dim()과 mnbrak의 7번째 아규먼트의 타입이 틀려서 그런거니까요,
워닝을 없애시려면 f1dim()이나 7번째 아규먼트로 쓰이는 함수의
원형을 맞춰주시면 됩니다.
static double f1dim(double x, ...) 등으로 바꿔주시면 되는데요,
안해주셔도 정확히만 변수를 참조하신다면 문제는 안될 것 같고요.
아니면 조금은 위험하지만 mnbrak() 함수원형 선언시에
뒷쪽함수를 바꾸시면 됩니다.
int mnbrak(double *ax, double *bx, double *cx, double *fa, double
*fb,double *fc, double func());
freematrix()는 코드가 좀 잘못되어 있는 듯 합니다.
void freematrix(void **A)
{
free(A[0]);
free(A);
}
는 일단 워닝을 내게되고요,
워닝을 피하시려면 약간 편법을 쓰시면 될 듯 합니다.
void freematrix(void *A)
{
char **_a = A;
free(_a[0]);
free(_a);
}
식으로 하시면 되겠습니다.
근데 프로그램이 세그먼테이션 오류 나서 죽게된 이유는 아닌 듯 싶습니
다. 이전에 잘못된 포인터(할당안된 포인터, 잘못된 주소, free()된 포인
터를 이후에 또 free())가 free()된다든지 하는 문제때문에 발생한 것이
아닌가 생각이 드네요.
혹, 소스의 변경이 없었다면... 흠.. 무슨 이유일까요?
정렬이 안 맞았다고 하면 버스 에러가 났을텐데.....
답변 감사합니다.
쉽게 해결될것 같지 않네요. 답변해주셔서 감사합니다.
그런데 malloc.c No such file or directory 라는
메세지는 왜 나올까요??
Re^5: 답변 감사합니다.
malloc.c는 컴파일이 안되고 있는것으로 보입니다.
Makefile을 다시한번 확인해 보십시요.
make후에 ls의 화면을보면 .o이라는 파일이 생성되어 있지
않더군요.
그 이외에 Segment fault 에러는 string관련 함수에서
자주 발생을 합니다. strcpy, sprintf...
님의 경우는 malloc.c만 컴파일 되면 될것 같은데 그것이
포함이 되어있다면 str... 계열 함수들이 불리기전에 Source
변수들의 크기를 검사해보는 것이 좋습니다.
제가 자주 겪은 에러중 대부분이 strcpy()를 사용할 때
Source쪽 데이터의 크기가 0일때 많이 일어나더군요.
그럼 좋은 하루되세요.
댓글 달기