You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucy.apache.org by Marvin Humphrey <ma...@rectangular.com> on 2013/10/10 05:11:03 UTC
[lucy-dev] Const-ness and "steal" String constructors
On Sat, Sep 14, 2013 at 12:29 PM, <nw...@apache.org> wrote:
> Const 'ptr' instance variable of String
> Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
> Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/fc363f05
> Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/fc363f05
> Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/fc363f05
> /** Return a new String which holds a copy of the passed-in string.
> * Check for UTF-8 validity.
> @@ -48,19 +48,19 @@ class Clownfish::String cnick Str
> * passed-in string. Check validity of supplied UTF-8.
> */
> inert incremented String*
> - new_steal_str(char *ptr, size_t size);
> + new_steal_str(const char *ptr, size_t size);
>
> /** Return a pointer to a new String which assumes ownership of the
> * passed-in string. Do not check validity of supplied UTF-8.
> */
> inert incremented String*
> - new_steal_from_trusted_str(char *ptr, size_t size);
> + new_steal_from_trusted_str(const char *ptr, size_t size);
>
> /** Initialize the String using the passed-in string. Do not check
> * validity of supplied UTF-8.
> */
> public inert String*
> - init_steal_trusted_str(decremented String *self, char *ptr, size_t size);
> + init_steal_trusted_str(String *self, const char *ptr, size_t size);
>
> /** Return a String which holds a single character.
It seems to me that the "steal" constructors should take `char*` instead of
`const char*`. We have to cast away constness in the destructor when we
invoke "free" on the stolen strings, right? The potential exists to create a
situation where we free() non-heap memory and the compiler doesn't issue a
warning.
Marvin Humphrey
Re: [lucy-dev] Const-ness and "steal" String constructors
Posted by Nick Wellnhofer <we...@aevum.de>.
On 10/10/2013 05:11, Marvin Humphrey wrote:
> On Sat, Sep 14, 2013 at 12:29 PM, <nw...@apache.org> wrote:
>> inert incremented String*
>> - new_steal_str(char *ptr, size_t size);
>> + new_steal_str(const char *ptr, size_t size);
>
> It seems to me that the "steal" constructors should take `char*` instead of
> `const char*`. We have to cast away constness in the destructor when we
> invoke "free" on the stolen strings, right? The potential exists to create a
> situation where we free() non-heap memory and the compiler doesn't issue a
> warning.
You're right. Good catch.
Nick