当前位置: 技术问答>linux和unix
大家来帮我看看这个代码,为什么invalid address alignment??
来源: 互联网 发布时间:2017-02-23
本文导语: $ cat test.c #include static struct { int argc; char **argv; int len; } origarg; static int get_arglen(int argc, char **argv) { char *s = argv[0]; return 0; } void ruby_sysinit(int *argc, char ***argv) { ...
$ cat test.c
#include
static struct {
int argc;
char **argv;
int len;
} origarg;
static int get_arglen(int argc, char **argv)
{
char *s = argv[0];
return 0;
}
void
ruby_sysinit(int *argc, char ***argv)
{
origarg.argc = *argc;
origarg.argv = *argv;
origarg.len = get_arglen(origarg.argc, origarg.argv);
}
int main()
{
int fake_argc = 0;
char* fake_argv[1] = { "" };
ruby_sysinit(&fake_argc, (char ***)&fake_argv);
return 0;
}
$ CC -m64 test.c // 去掉-m64就没问题了
$ ./a.out
Bus Error(coredump)
$ CC -V
CC: Sun C++ 5.11 SunOS_sparc 2010/08/13
$ dbx a.out core
t@1 (l@1) program terminated by signal BUS (invalid address alignment)
0x0000000100000a98: get_arglen+0x0008: ldx [%i1], %o0
(dbx) where -h
current thread: t@1
=>[1] get_arglen(0x0, 0x100000d2c, 0xffffffff7ee48240, 0xffffffff7ee49e44, 0xffffffff7eb00200, 0x4), at 0x100000a98
[2] ruby_sysinit(0xffffffff7ffff154, 0xffffffff7ffff148, 0x1f053c, 0xffffffff7f1085a4, 0xffffffff7ee00700, 0xffffffff7eb00200), at 0x100000af8
[3] main(0x1, 0xffffffff7ffff218, 0xffffffff7ffff228, 0xffffffff7ec4bb50, 0xffffffff7ee00680, 0xffffffff7eb00200), at 0x100000b60
(dbx) frame 1
Current function is get_arglen
11 char *s = argv[0];
(dbx) print argv[0]
argv[0] = 0x1 ""
(dbx) frame 3
Current function is main
29 ruby_sysinit(&fake_argc, (char ***)&fake_argv);
(dbx) print &fake_argv
&fake_argv = 0xffffffff7ffff068
(dbx) print (char **)fake_argv
dbx: Cannot cast an array
(dbx) print fake_argv
fake_argv = (0x100000db4 "")
大家来帮我看看啊,这为什么会coredump啊?
#include
static struct {
int argc;
char **argv;
int len;
} origarg;
static int get_arglen(int argc, char **argv)
{
char *s = argv[0];
return 0;
}
void
ruby_sysinit(int *argc, char ***argv)
{
origarg.argc = *argc;
origarg.argv = *argv;
origarg.len = get_arglen(origarg.argc, origarg.argv);
}
int main()
{
int fake_argc = 0;
char* fake_argv[1] = { "" };
ruby_sysinit(&fake_argc, (char ***)&fake_argv);
return 0;
}
$ CC -m64 test.c // 去掉-m64就没问题了
$ ./a.out
Bus Error(coredump)
$ CC -V
CC: Sun C++ 5.11 SunOS_sparc 2010/08/13
$ dbx a.out core
t@1 (l@1) program terminated by signal BUS (invalid address alignment)
0x0000000100000a98: get_arglen+0x0008: ldx [%i1], %o0
(dbx) where -h
current thread: t@1
=>[1] get_arglen(0x0, 0x100000d2c, 0xffffffff7ee48240, 0xffffffff7ee49e44, 0xffffffff7eb00200, 0x4), at 0x100000a98
[2] ruby_sysinit(0xffffffff7ffff154, 0xffffffff7ffff148, 0x1f053c, 0xffffffff7f1085a4, 0xffffffff7ee00700, 0xffffffff7eb00200), at 0x100000af8
[3] main(0x1, 0xffffffff7ffff218, 0xffffffff7ffff228, 0xffffffff7ec4bb50, 0xffffffff7ee00680, 0xffffffff7eb00200), at 0x100000b60
(dbx) frame 1
Current function is get_arglen
11 char *s = argv[0];
(dbx) print argv[0]
argv[0] = 0x1 ""
(dbx) frame 3
Current function is main
29 ruby_sysinit(&fake_argc, (char ***)&fake_argv);
(dbx) print &fake_argv
&fake_argv = 0xffffffff7ffff068
(dbx) print (char **)fake_argv
dbx: Cannot cast an array
(dbx) print fake_argv
fake_argv = (0x100000db4 "")
大家来帮我看看啊,这为什么会coredump啊?
|
void
ruby_sysinit(int *argc, char ***argv)
{
origarg.argc = *argc;
origarg.argv = *argv;
origarg.len = get_arglen(origarg.argc, origarg.argv);
}
int main()
{
int fake_argc = 0;
char* fake_argv[1] = { "" };
ruby_sysinit(&fake_argc, (char ***)&fake_argv);
return 0;
}
楼主这指针学的,一看就不会指针。 你以为多&一下就是多一个*啊 =,=
只是给你改对了,都不理解为什么要传int的地址,蛋疼。
ruby_sysinit(int *argc, char ***argv)
{
origarg.argc = *argc;
origarg.argv = *argv;
origarg.len = get_arglen(origarg.argc, origarg.argv);
}
int main()
{
int fake_argc = 0;
char* fake_argv[1] = { "" };
ruby_sysinit(&fake_argc, (char ***)&fake_argv);
return 0;
}
楼主这指针学的,一看就不会指针。 你以为多&一下就是多一个*啊 =,=
#include
static struct {
int argc;
char **argv;
int len;
} origarg;
static int get_arglen(int argc, char* *argv)
{
char *s = argv[0];
return 0;
}
void
ruby_sysinit(int *argc, char* *argv)
{
origarg.argc = *argc;
origarg.argv = argv;
origarg.len = get_arglen(origarg.argc, origarg.argv);
}
int main()
{
int fake_argc = 0;
char* fake_argv[1] = { "" };
ruby_sysinit(&fake_argc, fake_argv);
return 0;
}
只是给你改对了,都不理解为什么要传int的地址,蛋疼。
|
char* fake_argv[1] 不能转换成三级指针。。。
加个-xmemalign来忽略这个sigbus
加个-xmemalign来忽略这个sigbus
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。