String literal and char*

Oct 11, 2017 at 4:27pm
Hey,

I've rarely used char* in my past. I just try to avoid them and use strings as much as possible. Therefore I kinda lack knowledge about char* strings at my work.

I would like to know if this is safe code for returning a char*

1
2
3
4
5
char* Dosometh(char* group)
{
	char* formatAddition = "Test_";
	return group + strlen(formatAddition);
}


Normally this is bad code because you return a pointer to a temporary.
But as far as i know it is safe to return temp string literals, and in this case we don't have a typical pointer to a temp.

but does group + strlen(formatAddition) create a string literal?
Last edited on Oct 11, 2017 at 4:27pm
Oct 11, 2017 at 4:35pm
I should mention that group is just a string literal passed in
so group + strlen(FormatAdittion)

would return a part of a string literal
Last edited on Oct 11, 2017 at 4:35pm
Oct 11, 2017 at 4:43pm
strlen(formatAddition) is a number. The length of the char* style string.

In this case, the number 5.

So the code is effectively:
return group + 5;

So you're returning a char*, pointing to the fifth letter of the whatever group is pointing at. Is that what you're trying to do? Effectively, get back a pointer into the string you passed in?

If group is pointing to a c-style string less than five letters long, this is massively unsafe.

If you can guarantee that group will be pointing at something long enough, such as "Test_12345", then you'll get back a pointer to the c-style string "12345".

I don't know if I'd call if "safe" but there's no reason why it wouldn't work.

Oct 11, 2017 at 4:55pm
oh right, so this is acutally never a temporary.

yeah i know that group will always contain "Test_" and more, I'm only concerned about returning it, but it seems to be ok
Oct 11, 2017 at 4:56pm
> I would like to know if this is safe code for returning a char*

It is not compileable code.

1
2
3
char* formatAddition = "Test_";
// g++: error: ISO C++ forbids converting a string constant to 'char*'
// clang++: ISO C++11 does not allow conversion from string literal to 'char *' 

http://coliru.stacked-crooked.com/a/513d1963615e5c78

This is fine: const char* formatAddition = "Test_";


> does group + strlen(formatAddition) create a string literal?

No. It yields a pointer to the character after the first five characters in the c-style string group.


> I should mention that group is just a string literal passed in

To be able to accept a (pointer to the first character of a) string literal, the function should be:
1
2
3
4
5
6
const char* Dosometh( const char* group )
{
	const char* formatAddition = "Test_";
	// assert( strlen(group) >= strlen(formatAddition) ) ;
	return group + strlen(formatAddition);
}
Oct 11, 2017 at 5:07pm
interesting, because it compiled on all c++11 compilers :o
char* formatAddition = "Test_";

I've tried it on compiler explorer to make sure we aren't using a compiler I'm not aware of.
It works there as well

and i were wrong, group isn't a string literal, it is a char* which gets assinged somewhere
and and the variables which get the return types are non const therefore i can't return a const char*
Last edited on Oct 11, 2017 at 5:17pm
Oct 11, 2017 at 5:16pm
Compile with both -std-c++11 (or better c++14) and pedantic-errors (both are required)
If either is missing, C++ conformance (to the extent that the implementation is capable of conforming) is not enforced; the code is assumed to be in a non-standard dialect of C++ which is wildly popular amongst the Linux crowd.
Oct 11, 2017 at 5:20pm
yeah i was suprised why it compiled without const.
on compiler explorer compiled with clang throws an warning at least.
Oct 11, 2017 at 5:21pm
1
2
3
4
5
char* Dosometh(char* group)
{
	const char* formatAddition = "Test_";
	return group + strlen(formatAddition);
}

is my updated version now, input isn't const because i get a non const passed in
and return is also non const because the variables which get the return typed assigned to are also non const.

and as mentioned group is no string literal, that was wrong information
Last edited on Oct 11, 2017 at 5:22pm
Oct 11, 2017 at 5:25pm
> group isn't a string literal, it is a char* which gets assinged somewhere
> and and the variables which get the return types are non const therefore i can't return a const char*

Then this would be fine:
1
2
3
4
5
6
char* Dosometh( char* group )
{
	const char* formatAddition = "Test_";
	// assert( strlen(group) >= strlen(formatAddition) ) ;
	return group + strlen(formatAddition);
}


Or, just:
1
2
3
4
5
6
char* Dosometh( char* group )
{
	static constexpr auto len_formatAddition = sizeof( "Test_" ) - 1 ;
	// assert( strlen(group) >= len_formatAddition ) ;
	return group + len_formatAddition ;
}
Oct 11, 2017 at 5:52pm
is constexpr for variables already in c++11 ? cant remember,

yeah, but i think the first one is more readable, they'll generate the same optimized code anyway

for -O2

https://godbolt.org/g/7yKNW5
Topic archived. No new replies allowed.