tempCString

Creates temporary C string with copy of passed text.

Returned object is implicitly convertible to const To* and has two properties: ptr to access C string as const To* and buffPtr to access it as To*.

The temporary C string is valid unless returned object is destroyed. Thus if returned object is assigned to a variable the temporary is valid unless the variable goes out of scope. If returned object isn't assigned to a variable it will be destroyed at the end of creating primary expression.

More...
tempCString
(
To = char
From
)
(
in From[] str
)
if (
isSomeChar!To &&
isSomeChar!From
)

Detailed Description

Implementation note

For small strings tempCString will use stack allocated buffer, for large strings (approximately 1000 characters and more) it will allocate temporary one from $(DPREF2 memory, allocation, threadHeap).

Note: This function is intended to be used in function call expression (like strlen(str.tempCString())). Incorrect usage of this function may lead to memory corruption. See WARNING in Examples section.

Examples

t
{
	import core.stdc.string;

	string str = "abc";

	// Intended usage
	assert(strlen(str.tempCString()) == 3);

	// Correct usage
	auto tmp = str.tempCString();
	assert(strlen(tmp) == 3); // or `tmp.ptr`, or `tmp.buffPtr`

	// $(RED WARNING): $(RED Incorrect usage)
	auto pInvalid1 = str.tempCString().ptr;
	const char* pInvalid2 = str.tempCString();
	// Both pointers refer to invalid memory here as
	// returned values aren't assigned to a variable and
	// both primary expressions are ende

See Also

$(DPREF2 memory, allocation, tempAlloc)

Meta