总线错误asprintf。 OS X 10.3.6。 gcc 3.3 [英] Bus Error asprintf. OS X 10.3.6. gcc 3.3
问题描述
在
OS X下编译时,我似乎无法让asprintf工作。
使用以下代码:
#include< string.h>
#include< stdio.h>
int main(){
char ** w;
asprintf(w," fooo");
printf("%s \ n",* w);
返回0;
}
并使用带有-lc和-Wall标志的gcc编译,我没有错误。
程序运行吐出总线错误。 Linux上相同的代码
机器gcc 3.2.2按预期编译并运行。
任何帮助都会很棒。
>
Neil
2004年11月19日星期五09:18:27 -0800,Neil Dunn写道:
< blockquote class =post_quotes>
在OS OS下编译时,我似乎无法让asprintf工作。
使用以下代码:
#include< string.h>
#include< stdio.h>
int main(){
char ** w;
asprintf(w," fooo");
printf("%s \ n",* w); <回到0;
使用带有-lc和-Wall标志的gcc编译,我没有错误。
<程序运行吐出一个总线错误。 Linux上的相同代码
机器gcc 3.2.2按预期编译和运行。
任何帮助都会很棒。
Neil
asprintf函数是GNU / BSD扩展(不是标准C),而不是
on-topic。但是,您的程序中存在一个基本错误,
可能无疑是您问题的原因。
asprintf函数的原型是:
int asprintf(char ** strp,const char * fmt,...);
其中strp是指向char的指针的地址[0],asprintf
函数将设置为指向新分配的字符串。你没有将指针的地址传递给char,而是将一个
未初始化指针的值传递给指向char的指针。 asprintf将未初始化的w指向的地址取值为
,并将其更改为指向新分配的字符串,导致未定义的行为。
这是你b $ b应该做的事情:
#include< string.h>
#include< stdio。 h>
int main(无效){
char * w;
asprintf( & w,fooo);
printf("%s \ nn",w);
返回0 ;
}
[0]为了迂腐,这更好地表达为指向指向
$ b $的指针b char,可以这样写:
#include< string.h>
#include< stdio.h>
int main(无效){
char * str;
char ** w =& str;
asprintf(w," fooo");
printf("%s \ nn",* w);
返回0;
}
这没关系,因为这里已经正确初始化了。
< OT>
不要忘记,至少在Linux上你还需要在#include< stdio.h&之前#define _GNU_SOURCE
GT;访问asprintf的原型。 < / OT>
Rob Gamble
" Robert Gamble:Neil Dunn:
我在OS下编译时似乎无法让asprintf工作。
使用下面的代码:
#include< string.h>
#include< stdio.h>
int main(){
char ** w;
asprintf(w," fooo");
printf("%s \ n",* w);
返回0;
使用带有-lc和-Wall标志的gcc编译,我没有错误。
该程序运行吐出一个总线错误。 Linux上的相同代码
机器gcc 3.2.2按预期编译和运行。
任何帮助都会很棒。
asprintf函数是一个GNU / BSD扩展(不是标准C)而不是
主题在这里。但是,您的程序中存在一个根本性错误,
可能无疑是您问题的原因。
asprintf函数的原型是:
int asprintf(char ** strp,const char * fmt,...);
由于此函数是OT,我可以拼错一下,也就是说,我看到如何使用这些有趣的东西来获得一些疯狂的东西,例如char ** tja,但是怎么回事?看东西?其中strp是指向char的指针的地址[0],asprintf
函数将设置为指向新分配的字符串。您没有将指针的地址传递给char,而是将未初始化的指针的值传递给指向char的指针。 asprintf取值为未初始化的w指向的地址,并将其更改为指向新分配的字符串,导致未定义的行为。
我想要做的是调用I_DONT_KNOW类型的函数,它具有作为
标识符的整数并返回一个简单的,花园多样的指针。 MPJ
2004年11月19日星期五12:13:27 -0600,Merrill& Michele写道:
" Robert Gamble:> Neil Dunn:>在OS下编译时,我似乎无法让asprintf工作
> X.
>
>使用以下代码:
>
> #include< string.h>
> #include< stdio.h>
>
> int main(){
>
> char ** w;
>
> asprintf(w," fooo");
>
> printf("%s \ n",* w);
>
>返回0;
>
> }
>
>并使用带有-lc和-Wall标志的gcc进行编译,我没有错误。
>
>该程序运行吐出一个总线错误。 Linux上的相同代码
>机器gcc 3.2.2按预期编译和运行。
>
>任何帮助都会很棒。asprintf函数是一个GNU / BSD扩展(不是标准C),而不是
这里的主题。但是,您的程序中存在一个根本性错误,
可能无疑是您问题的原因。
asprintf函数的原型是:
int asprintf(char ** strp,const char * fmt,...);
由于此函数是OT,我可以拼错一下,也就是说,我看到如何通过疯狂看待像char ** tja这样的东西,但是如何回归这些看起来很滑稽的东西呢?
从函数返回任何其他类型的方式相同:
char ** myfunc(void){
char ** ptr;
/ *初始化ptr,例如使用malloc * /
ptr = NULL;
返回ptr;
}
其中strp是指向char的指针的地址[0],asprintf
函数将设置为指向新分配的字符串。您没有将指针的地址传递给char,而是将未初始化的指针的值传递给指向char的指针。 asprintf取值为未初始化的w指向的地址,并将其更改为指向新分配的字符串,导致未定义的行为。
我是什么想做的是调用I_DONT_KNOW类型的函数,它具有作为标识符的整数并返回一个简单的,花园种类的指针。 MPJ
我真的不知道你在这里问什么,你能澄清一下吗?也许
你正在寻找void * myfunc(int); (真的出去了吗
)。
Rob Gamble
Hi,
I don''t seem to be able to get asprintf to work when compiling under
OS X.
Using the following code:
#include <string.h>
#include <stdio.h>
int main() {
char **w;
asprintf(w,"fooo");
printf("%s\n",*w);
return 0;
}
and compiling using gcc with the -lc and -Wall flags, I get no erros.
The program runs spitting out a Bus Error. The same code on a Linux
machine gcc 3.2.2 compiles and runs as expected.
Any help would be great.
Neil
On Fri, 19 Nov 2004 09:18:27 -0800, Neil Dunn wrote:
Hi,
I don''t seem to be able to get asprintf to work when compiling under OS
X.
Using the following code:
#include <string.h>
#include <stdio.h>
int main() {
char **w;
asprintf(w,"fooo");
printf("%s\n",*w);
return 0;
}
and compiling using gcc with the -lc and -Wall flags, I get no erros.
The program runs spitting out a Bus Error. The same code on a Linux
machine gcc 3.2.2 compiles and runs as expected.
Any help would be great.
Neil
The asprintf function is a GNU/BSD extension (not Standard C) and not
on-topic here. However, there is a fundamental error in your program that
may is undoubtedly the cause of your issue. The prototype for the
asprintf function is:
int asprintf(char **strp, const char *fmt, ...);
Where strp is the address[0] of a pointer to char which the asprintf
function will set to point to a newly allocated string. You are not
passing the address of a pointer to char, you are passing the value of an
uninitialized pointer to pointer to char. The asprintf takes the value at
the address pointed to by the uninitialized w and changes it to point to
the newly allocated string causing undefined behavior. Here is what you
should be doing:
#include <string.h>
#include <stdio.h>
int main(void) {
char *w;
asprintf(&w,"fooo");
printf("%s\n",w);
return 0;
}
[0] To be pedantic, this is better articulated as a pointer to pointer to
char and can be written like this:
#include <string.h>
#include <stdio.h>
int main(void) {
char *str;
char **w = &str;
asprintf(w,"fooo");
printf("%s\n",*w);
return 0;
}
This is okay because here w has been properly initialized.
<OT>
Don''t forget that at least on Linux you also need to #define _GNU_SOURCE
before #include <stdio.h> to access the prototype for asprintf. </OT>
Rob Gamble
"Robert Gamble:Neil Dunn: Hi, I don''t seem to be able to get asprintf to work when compiling under OS
X.
Using the following code:
#include <string.h>
#include <stdio.h>
int main() {
char **w;
asprintf(w,"fooo");
printf("%s\n",*w);
return 0;
}
and compiling using gcc with the -lc and -Wall flags, I get no erros.
The program runs spitting out a Bus Error. The same code on a Linux
machine gcc 3.2.2 compiles and runs as expected.
Any help would be great.The asprintf function is a GNU/BSD extension (not Standard C) and not
on-topic here. However, there is a fundamental error in your program that
may is undoubtedly the cause of your issue. The prototype for the
asprintf function is:
int asprintf(char **strp, const char *fmt, ...);
Since this function is OT, might I splice in question, namely, I see how to
PASS crazy looking things like char **tja, but how does one RETURN one of
these funny-looking things? Where strp is the address[0] of a pointer to char which the asprintf
function will set to point to a newly allocated string. You are not
passing the address of a pointer to char, you are passing the value of an
uninitialized pointer to pointer to char. The asprintf takes the value at
the address pointed to by the uninitialized w and changes it to point to
the newly allocated string causing undefined behavior.
What I want to do is call a function of type I_DONT_KNOW that has ints as
identifiers and return a simple, garden-variety pointer. MPJ
On Fri, 19 Nov 2004 12:13:27 -0600, Merrill & Michele wrote:
"Robert Gamble:> Neil Dunn: Hi, > I don''t seem to be able to get asprintf to work when compiling under OS
> X.
>
> Using the following code:
>
> #include <string.h>
> #include <stdio.h>
>
> int main() {
>
> char **w;
>
> asprintf(w,"fooo");
>
> printf("%s\n",*w);
>
> return 0;
>
> }
>
> and compiling using gcc with the -lc and -Wall flags, I get no erros.
>
> The program runs spitting out a Bus Error. The same code on a Linux
> machine gcc 3.2.2 compiles and runs as expected.
>
> Any help would be great.The asprintf function is a GNU/BSD extension (not Standard C) and not
on-topic here. However, there is a fundamental error in your program that
may is undoubtedly the cause of your issue. The prototype for the
asprintf function is:
int asprintf(char **strp, const char *fmt, ...);
Since this function is OT, might I splice in question, namely, I see how to
PASS crazy looking things like char **tja, but how does one RETURN one of
these funny-looking things?
The same way you return any other type from a function:
char ** myfunc (void) {
char **ptr;
/* Initialize ptr, for example by using malloc */
ptr = NULL;
return ptr;
}
Where strp is the address[0] of a pointer to char which the asprintf
function will set to point to a newly allocated string. You are not
passing the address of a pointer to char, you are passing the value of an
uninitialized pointer to pointer to char. The asprintf takes the value at
the address pointed to by the uninitialized w and changes it to point to
the newly allocated string causing undefined behavior.
What I want to do is call a function of type I_DONT_KNOW that has ints as
identifiers and return a simple, garden-variety pointer. MPJ
I really have no idea what you are asking here, can you clarify? Maybe
you are looking for "void * myfunc (int);" (Really going out on a limb
here).
Rob Gamble
这篇关于总线错误asprintf。 OS X 10.3.6。 gcc 3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!