Home > Back-end >  C_str () returns a value can be NULL
C_str () returns a value can be NULL

Time:12-08

Function_name (string arg)
{
If (NULL!=arg. C_str ())
{
//other processing
}
}

Excuse me when NULL==arg. C_str ()
Tried it on the following two cases, as if all is NULL!=arg. C_str ()
String arg.//not initialize
String arg="";//initialization is not an empty string

CodePudding user response:

Can strlen whether return a string to an empty string

CodePudding user response:

If ( '\ 0'!=arg. C_str ())

CodePudding user response:

refer to the second floor yshuise response:
if ( '\ 0'!=arg. C_str ())


Is someone else's code,
I just want to ask is there any NULL==arg. The c_str () this kind of situation,

CodePudding user response:

Focuslight
reference 1 floor response:
can strlen whether return a string to an empty string


This is, I ask how do not judge,
I just want to ask is there any NULL==arg. The c_str () this kind of situation, have what condition can meet?

CodePudding user response:

The default is an empty string, not a null pointer, so there will not be nullptr

CodePudding user response:

reference 5 floor SDGHCHJ reply:
default is an empty string, not a null pointer, so there will not be nullptr of


The NULL==arg. C_str () this kind of situation is unlikely to happen?

CodePudding user response:

On code, see,
 
# include & lt; String>
# include & lt; Iostream>

using namespace std;

Int main ()
{
String STR.

//the default array is not empty
Cout & lt; <"The default capacity & lt;" Cout & lt; <"Pointer value" & lt; <(void *) STR. C_str () & lt;
//clean up is not effective
STR. The clear ();
Cout & lt; <"After the adjustment capacity & lt;" Cout & lt; <"Pointer value" & lt; <(void *) STR. C_str () & lt;
//adjust the space to less than the default space no matter use
STR. Resize (3);
Cout & lt; <"After the adjustment capacity & lt;" Cout & lt; <"Pointer value" & lt; <(void *) STR. C_str () & lt;
//there is no question of big,
STR. Resize (100);
Cout & lt; <"After the adjustment capacity & lt;" Cout & lt; <"Pointer value" & lt; <(void *) STR. C_str () & lt;
return 0;
}


Assembly code
 
If (_Newsize & lt;=this - & gt; _Mysize ())
008856 f3 mov ecx, dword PTR [this]
008856 f6 call STD: : _String_alloc & lt; STD: : _String_base_types & lt; Char, STD: : allocator> : : _Mysize d5h (08812)
008856 fb mov ecx, dword PTR [_Newsize]
008856 fe CMP ecx, dword PTR (eax)
00885700 ja STD: : basic_string & lt; Char, STD: : char_traits & lt; char> , STD: : allocator : : resize + 40 h (0885710 h)


Capacity can't be less than the default capacity,
It is not possible returns NULL
  • Related