Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Plain View
Zookeeper, mail # user - C API question


+
Marios Hadjieleftheriou 2013-07-31, 02:06
+
Mohammad Shamma 2013-07-31, 04:55
+
Marios Hadjieleftheriou 2013-07-31, 16:24
+
Marshall McMullen 2013-07-31, 17:25
Copy link to this message
-
Re: C API question
Marshall McMullen 2013-07-31, 17:31
And obviously you could layer a reinterpet_cast on the const_cast to get it
from a void* to whatever your object type really is.
On Wed, Jul 31, 2013 at 11:25 AM, Marshall McMullen <
[EMAIL PROTECTED]> wrote:

> IMO I think this is correct behavior from zookeeper's perspective. By
> marking the parameter as a const void* the C API is promising that *it*
> will not modify the contents of memory referred to by the pointer. That is
> absolutely the correct contract for zookeeper to make. If it were not const
> then the C API would not be able to guarantee that. Moreover, as a client
> building on top of the C API looking at the C API I can easily tell looking
> at these functions that it is promising not not modify that data. I don't
> see any problems with this.
>
> As to the problem in your callback, that is easily solvable since you own
> the memory in the callback so you know you can safely const-cast away the
> const. You mentioned reinterpet_cast, so in c++ this would be:
>
> void* data = const_cast<void *>(data_ptr);
>
>
>
> On Wed, Jul 31, 2013 at 10:24 AM, Marios Hadjieleftheriou <
> [EMAIL PROTECTED]> wrote:
>
>> Hi Mohammad.
>>
>> The parameter is a const void * (reading from right to left: a pointer
>> to a void that is const). Hence the data pointed to cannot be changed.
>>
>> Moreover, it does not make sense to pass a void* const (right to left:
>> a const pointer to a void) as a function argument, because the pointer
>> itself ("data" in this case) is passed by value, hence it is
>> meaningless for it to be const or not.
>>
>> In any case, in my callback function I tried to reinterpret_cast my
>> data to a non-const instance and the compiler complained that it needs
>> to be const. I cannot change my data.
>>
>> On Wed, Jul 31, 2013 at 12:55 AM, Mohammad Shamma
>> <[EMAIL PROTECTED]> wrote:
>> > I think the const in this case would cause the pointer to be fixed. The
>> > data referred to by the pointer can still be updated.
>> >
>> > This is a similar question on stackoverflow:
>> > http://stackoverflow.com/questions/6407041/constant-pointer
>> >
>> >
>> > On Tue, Jul 30, 2013 at 7:06 PM, Marios Hadjieleftheriou
>> > <[EMAIL PROTECTED]>wrote:
>> >
>> >> The string_completiion_t callback signature of the Zookeeper C API is:
>> >>
>> >> typedef void(* string_completion_t)(int rc, const char *value, const
>> void
>> >> *data)
>> >>
>> >> That means that when the callback is called, the user provided data is
>> >> returned as a const void and cannot be modified by client code, which
>> >> kind of defeats the purpose of passing any data in the first place.
>> >>
>> >> Is there any reason why this parameter needs to be const?
>> >>
>> >
>> >
>> >
>> > --
>> > Mohammad Shamma
>>
>
>
+
Marios Hadjieleftheriou 2013-08-01, 15:14
+
Stephen Tyree 2013-08-01, 15:20