Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: calling a function with a std::string argument using a null pointer (http://www.velocityreviews.com/forums/t725075-re-calling-a-function-with-a-std-string-argument-using-a-null-pointer.html)

Kai-Uwe Bux 06-08-2010 11:57 PM

Re: calling a function with a std::string argument using a null pointer
 
Lynn McGuire wrote:

> Does anyone know what happens when I call a function with a
> std::string argument using a null pointer ? I have googled this
> but have not gotten a good answer.

[...]

Undefined behavior: [21.3.1/9] governs the relevant constructor and requires
that the pointer passed shall not be a null pointer. Hence, any program
violating that constraint is in UB land.


Best

Kai-Uwe Bux

Victor Bazarov 06-09-2010 03:08 PM

Re: calling a function with a std::string argument using a null pointer
 
On 6/9/2010 10:50 AM, Lynn McGuire wrote:
>>> Does anyone know what happens when I call a function with a
>>> std::string argument using a null pointer ? I have googled this

>>
>> Undefined behavior: [21.3.1/9] governs the relevant constructor and
>> requires
>> that the pointer passed shall not be a null pointer. Hence, any program
>> violating that constraint is in UB land.

>
> So this is functionally equivalent to "std::string aString = NULL" ?


Mmm... Pretty much. Probably even to "std::string aString(NULL);"

> Is there any way to modify this behavior of std::string other than
> subclassing ?


You cannot modify the behaviour of any standard component. You can only
modify what *you* write.

V
--
I do not respond to top-posted replies, please don't ask

Jonathan Lee 06-09-2010 03:28 PM

Re: calling a function with a std::string argument using a nullpointer
 
On Jun 9, 10:50*am, Lynn McGuire <l...@winsim.com> wrote:
> Is there any way to modify this behavior of std::string other than
> subclassing ?


As Victor mentioned, you can't modify std::string. You
could write an overload of someFunction():

string someFunction(const char* p) {

}


Jonathan Lee 06-09-2010 03:30 PM

Re: calling a function with a std::string argument using a nullpointer
 
On Jun 9, 11:28*am, Jonathan Lee <jlee...@uwo.ca> wrote:
> As Victor mentioned, you can't modify std::string. You
> could write an overload of someFunction():


Arg.. sent by accident.

string someFunction(const char* p) {
return someFunction(string(p ? p : ""));
}

Hacky, but functional.

--Jonathan

Victor Bazarov 06-09-2010 03:39 PM

Re: calling a function with a std::string argument using a nullpointer
 
On 6/9/2010 11:30 AM, Jonathan Lee wrote:
> On Jun 9, 11:28 am, Jonathan Lee<jlee...@uwo.ca> wrote:
>> As Victor mentioned, you can't modify std::string. You
>> could write an overload of someFunction():

>
> Arg.. sent by accident.
>
> string someFunction(const char* p) {
> return someFunction(string(p ? p : ""));
> }
>
> Hacky, but functional.


And considering the comment in the original post, perhaps it would be
better to simply throw:

std::string someFunction(std::string const& s) { .. normal .. }

std::string someFunction(char const* ptr) {
if (!ptr) throw("you forgot to initialize the pointer");
return someFunction(std::string(ptr));
}

At least then, if passing NULL is really an error, it will be flagged,
not masked...

Just my $0.02.

V
--
I do not respond to top-posted replies, please don't ask

Jonathan Lee 06-09-2010 04:20 PM

Re: calling a function with a std::string argument using a nullpointer
 
On Jun 9, 11:39*am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> And considering the comment in the original post, perhaps it would be
> better to simply throw:


Don't know if it's standard, but the constructor of std::string
_does_ throw when null is passed in (for the computer I'm currently
sitting at -- GCC 4.1.2). So why bother with a wrapper?

--Jonathan

red floyd 06-09-2010 04:44 PM

Re: calling a function with a std::string argument using a nullpointer
 
On Jun 9, 9:19*am, Lynn McGuire <l...@winsim.com> wrote:
> >> So this is functionally equivalent to "std::string aString = NULL" ?

>
> > Mmm... Pretty much. Probably even to "std::string aString(NULL);"

>
> So, is it safe to say that "std::string aString = NULL" will have
> a true comparison case of "aString.c_str () == NULL" ?
>


No. It's undefined behavior, which means *ANYTHING* can happen,
including working "as-expected" (whatever "as-expected" means);
segfaulting; causing errors elsewhere in your code; infecting your
system with a virus (unlikely, but...)

That's the whole point of undefined behavior. It's... undefined.

Kai-Uwe Bux 06-09-2010 04:56 PM

Re: calling a function with a std::string argument using a null pointer
 
Jonathan Lee wrote:

> On Jun 9, 11:39 am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
>> And considering the comment in the original post, perhaps it would be
>> better to simply throw:

>
> Don't know if it's standard, but the constructor of std::string
> _does_ throw when null is passed in (for the computer I'm currently
> sitting at -- GCC 4.1.2). So why bother with a wrapper?


As far as I know, it's _not_ standard. The standard says it's undefined
behavior. Hence, an implementation is free to crash, throw, or whatever it
pleases.

A wrapper would _ensure_ defined behavior.


Best

Kai-Uwe Bux

Bo Persson 06-09-2010 08:36 PM

Re: calling a function with a std::string argument using a null pointer
 
Lynn McGuire wrote:
>>> So this is functionally equivalent to "std::string aString =
>>> NULL" ?

>>
>> Mmm... Pretty much. Probably even to "std::string aString(NULL);"

>
> So, is it safe to say that "std::string aString = NULL" will have
> a true comparison case of "aString.c_str () == NULL" ?
>
> Thanks,
> Lynn McGuire


No, not at all.

It is highly likely that in this case the string constructor will call
strlen(NULL) to try to size up the argument. This is very ungood.


Bo Persson





AnonMail2005@gmail.com 06-09-2010 08:48 PM

Re: calling a function with a std::string argument using a nullpointer
 
On Jun 9, 1:20*pm, Lynn McGuire <l...@winsim.com> wrote:
> > As far as I know, it's _not_ standard. The standard says it's undefined
> > behavior. Hence, an implementation is free to crash, throw, or whatever it
> > pleases.

>
> > A wrapper would _ensure_ defined behavior.

>
> Yes. *Our code would just require a LOT of wrappers.
>
> Thanks,
> Lynn


I think the "wrapper" that someone mentioned above was to create an
overloaded function that takes a const char *. The function would do
the null pointer check, adjust as necessary, and then forward the call
to the real function taking a std::string.

If you put that function in the same header as the original function,
then your new function will be chosen over the old function for cases
where it is invoked with a char *. Do you see why?

So, only one "wrapper" (overloaded) function is needed in a single
file and no changes to client code are needed.

HTH


All times are GMT. The time now is 08:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.