You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by 吴腾飞 <wu...@yy.com> on 2013/10/12 04:08:39 UTC

The problem of using c binding function

Hi

 

I using c client binding fuction zoo_aget,the passed data cause problem.

 

ZOOAPI int zoo_aget(zhandle_t *zh,const char *path,int
watch,data_completion_t completion,const void *data)

 

1.Using like this is ok:

 

int ret = zoo_aget(zkhandle, path,0,DataCompletion,” zoo_aget”);

DataCompletion:

DataCompletion(int rc, const char *value, int value_len, const struct Stat
*stat, const void *data)

{

  Cout<<data<<endl;//out put is :zoo_aget

}

 

2.But this is no right:

Char* data = “zoo_aget”;

int ret = zoo_aget(zkhandle, path,0,DataCompletion, data);

DataCompletion:

DataCompletion(int rc, const char *value, int value_len, const struct Stat
*stat, const void *data)

{

  Cout<<data<<endl;//out put is not zoo_aget

}

 

Why I in 2 can not got correct output?Using zoo_axxx,pass data also the same
question.

 

Thanks,

 

Albert Wu.


Re: The problem of using c binding function

Posted by Patrick Hunt <ph...@apache.org>.
Likely because data in the second case is allocated on the stack,
which goes out of scope asa you exit the method.

Patrick

On Fri, Oct 11, 2013 at 7:08 PM, 吴腾飞 <wu...@yy.com> wrote:
> Hi
>
>
>
> I using c client binding fuction zoo_aget,the passed data cause problem.
>
>
>
> ZOOAPI int zoo_aget(zhandle_t *zh,const char *path,int
> watch,data_completion_t completion,const void *data)
>
>
>
> 1.Using like this is ok:
>
>
>
> int ret = zoo_aget(zkhandle, path,0,DataCompletion,” zoo_aget”);
>
> DataCompletion:
>
> DataCompletion(int rc, const char *value, int value_len, const struct Stat
> *stat, const void *data)
>
> {
>
>   Cout<<data<<endl;//out put is :zoo_aget
>
> }
>
>
>
> 2.But this is no right:
>
> Char* data = “zoo_aget”;
>
> int ret = zoo_aget(zkhandle, path,0,DataCompletion, data);
>
> DataCompletion:
>
> DataCompletion(int rc, const char *value, int value_len, const struct Stat
> *stat, const void *data)
>
> {
>
>   Cout<<data<<endl;//out put is not zoo_aget
>
> }
>
>
>
> Why I in 2 can not got correct output?Using zoo_axxx,pass data also the same
> question.
>
>
>
> Thanks,
>
>
>
> Albert Wu.
>

Re: The problem of using c binding function

Posted by Patrick Hunt <ph...@apache.org>.
Likely because data in the second case is allocated on the stack,
which goes out of scope asa you exit the method.

Patrick

On Fri, Oct 11, 2013 at 7:08 PM, 吴腾飞 <wu...@yy.com> wrote:
> Hi
>
>
>
> I using c client binding fuction zoo_aget,the passed data cause problem.
>
>
>
> ZOOAPI int zoo_aget(zhandle_t *zh,const char *path,int
> watch,data_completion_t completion,const void *data)
>
>
>
> 1.Using like this is ok:
>
>
>
> int ret = zoo_aget(zkhandle, path,0,DataCompletion,” zoo_aget”);
>
> DataCompletion:
>
> DataCompletion(int rc, const char *value, int value_len, const struct Stat
> *stat, const void *data)
>
> {
>
>   Cout<<data<<endl;//out put is :zoo_aget
>
> }
>
>
>
> 2.But this is no right:
>
> Char* data = “zoo_aget”;
>
> int ret = zoo_aget(zkhandle, path,0,DataCompletion, data);
>
> DataCompletion:
>
> DataCompletion(int rc, const char *value, int value_len, const struct Stat
> *stat, const void *data)
>
> {
>
>   Cout<<data<<endl;//out put is not zoo_aget
>
> }
>
>
>
> Why I in 2 can not got correct output?Using zoo_axxx,pass data also the same
> question.
>
>
>
> Thanks,
>
>
>
> Albert Wu.
>