窃听器 [英] bug

查看:74
本文介绍了窃听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




/ *开始端口到c ++ * /


#ifdef __cplusplus

externC" ; {

#endif


int * get_an_int(void);

int * pass_pointer_triv(int *);

#ifdef __cplusplus

}

#endif


/ *结束端口到c ++ * /


#include< stdio.h>

#include< stdlib.h>


int main(void)

{

int * rt,* qt;

qt = get_an_int();

rt = pass_pointer_triv(qt );

printf(t is%d \ n,* rt);

printf(" tja \ n");

返回0;

}


int * get_an_int(无效)

{

int t;

int * pt;

pt = malloc(sizeof * pt);

t = 41;

pt [0] = t;

返回pt;

}


pass_pointer_triv(int * a)

{

int t;

t = * a;

printf(" t is%d \ n", t);

返回a;

}

/ *结束来源* /

我相信这个来源是一个愚蠢的错误,远离kosher c ++。

我的编译器告诉我,微不足道的指针传递在

间接的水平上有所不同,但在我看来,这一切都是看起来一样。我相信我的

编译器是正确的。感谢任何提示。 furunculus



/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);
# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)
{
int t;
t = *a;
printf("t is %d\n", t);
return a;
}
/* end source */
I believe that this source is one silly error away from being kosher c++.
My compiler tells me that the trivial pointer pass varies in level of
indirection, but to my eye, it all looks the same. I believe that my
compiler is correct. Grateful for any hints. furunculus

推荐答案



" Your Uncle" <在***** @ crippled.net>在消息中写道

news:44 *********************** @ news.usenetmonster。 com ...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...


/ *开始端口到c ++ * /

#ifdef __cplusplus
extern" C" {
#endif

int * get_an_int(void);
int * pass_pointer_triv(int *);

#ifdef __cplusplus
}
#endif

/ *结束端口到c ++ * /

#include< stdio.h>
#include< stdlib.h>

int main(void)
{*> int * rt,* qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf(t is%d \ n,* rt);
printf(" tja \ n");
返回0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc(sizeof * pt);


我的编译器抱怨这个。

错误C2440:''='':无法从''void *''转换为''int *' '


要么转换为int *,要么使用new(首选)。 t = 41;
pt [0] = t;
返回pt;
}

pass_pointer_triv(int * a)


你忘记了退货类型。将此更改为

int * pass_pointer_triv(int * a)

并为我编译,除了malloc错误。

{
int t;
t = * a;
printf(t is%d \ n,t);
返回;
}
/ *结束源* /
我相信这个源是一个愚蠢的错误远离kosher c ++。
我的编译器告诉我,平凡的指针传递在
间接的级别不同,但是我的眼睛看起来都一样。我相信我的
编译器是正确的。感谢任何提示。 furunculus


/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);
# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);
My compiler complains about this.
error C2440: ''='' : cannot convert from ''void *'' to ''int *''

Either typecast to int * or use new (preferred) instead. t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)
You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.
{
int t;
t = *a;
printf("t is %d\n", t);
return a;
}
/* end source */
I believe that this source is one silly error away from being kosher c++.
My compiler tells me that the trivial pointer pass varies in level of
indirection, but to my eye, it all looks the same. I believe that my
compiler is correct. Grateful for any hints. furunculus





" Jim Langston" < TA ******* @ rocketmail.com>在消息中写道

news:3p *************** @ fe05.lga ...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:3p***************@fe05.lga...

"你的叔叔 <在***** @ crippled.net>在消息中写道
新闻:44 *********************** @ news.usenetmonster。 com ...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...


/ *开始端口到c ++ * /

#ifdef __cplusplus
extern" C" {
#endif

int * get_an_int(void);
int * pass_pointer_triv(int *);

#ifdef __cplusplus
}
#endif

/ *结束端口到c ++ * /

#include< stdio.h>
#include< stdlib.h>

int main(void)
{*> int * rt,* qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf(t is%d \ n,* rt);
printf(" tja \ n");
返回0;
}

int * get_an_int(无效)
{
int t;
int * pt;
pt = malloc(sizeof * pt);


/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);
# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);



我的编译器抱怨这个。
错误C2440:''='':无法从''void *''转换为''int *''

无论是强制转换为int *还是使用new(首选)。



My compiler complains about this.
error C2440: ''='' : cannot convert from ''void *'' to ''int *''

Either typecast to int * or use new (preferred) instead.



请解释。


Please explain.

t = 41;
pt [0] = t;
返回pt;
}

pass_pointer_triv(in t * a)
t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)



您忘记了返回类型。将此更改为
int * pass_pointer_triv(int * a)
并为它编译,除了malloc错误。



You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.



我认为那会'让她去谢谢你假装我的问题

今晚是你的问题。 ciao,f


I think that''s gonna get her going. Thanks for pretending like my problems
were your problems tonight. ciao, f




" Your Uncle" <在***** @ crippled.net>在消息中写道

news:44 *********************** @ news.usenetmonster。 com ...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...

Jim Langston < TA ******* @ rocketmail.com>在消息中写道
新闻:3p *************** @ fe05.lga ...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:3p***************@fe05.lga...

你的叔叔 <在***** @ crippled.net>在消息中写道
新闻:44 *********************** @ news.usenetmonster。 com ...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...


/ *开始端口到c ++ * /

#ifdef __cplusplus
extern" C" {
#endif

int * get_an_int(void);
int * pass_pointer_triv(int *);

#ifdef __cplusplus
}
#endif

/ *结束端口到c ++ * /

#include< stdio.h>
#include< stdlib.h>

int main(void)
{*> int * rt,* qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf(t is%d \ n,* rt);
printf(" tja \ n");
返回0;
}

int * get_an_int(无效)
{
int t;
int * pt;
pt = malloc(sizeof * pt);
我的编译器抱怨这个。
错误C2440:''='':无法从''void *''转换为''int *''

要转换为int *或使用new (首选)。


/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);
# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);
My compiler complains about this.
error C2440: ''='' : cannot convert from ''void *'' to ''int *''

Either typecast to int * or use new (preferred) instead.


请解释。


Please explain.




malloc返回void *(void指针)。如果你在c ++中使用它,你需要

使用reinterpret_cast或c-style
强制转换为正确的类型。这些工作中的任何一个,reinterpert_cast首选。


pt = reinterpret_cast< int *>(malloc(sizeof * pt));

pt =(int *)malloc(sizeof * pt);


但是首选使用new。


int * pt = new int;


那么只需使用delete来摆脱内存而不是免费(你使用带有malloc的
,但你的代码中缺少你的内存泄漏)



malloc returns a void* (void pointer). If you use it in c++ you need to
typecast it to the proper type either with reinterpret_cast or a c-style
cast. Either of these work, the reinterpert_cast prefered.

pt = reinterpret_cast<int*>( malloc( sizeof *pt ) );
pt = (int*) malloc( sizeof *pt );

but prefered is using new.

int *pt = new int;

Then just use delete to get rid of the memory instead of free (which you use
with malloc, but you are missing in your code so you have a memory leak)

t = 41;
pt [0] = t;
返回pt;
}

pass_pointer_triv(int * a)
t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)



您忘记了返回类型。将此更改为
int * pass_pointer_triv(int * a)
并为我编译,除了malloc错误。



You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.


我认为这会让她继续前进。感谢像我这样的问题假装你今晚的问题。 ciao,f


I think that''s gonna get her going. Thanks for pretending like my
problems were your problems tonight. ciao, f



这篇关于窃听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆