Sign in to follow this  
Dragon_Strike

whats the differance, copying strings

Recommended Posts

im having some issues with copying strings... im trying to copy a string from a temporary adress to a permanent using the string copy function (CASE2)... but it doesnt work... instead i have to do it like in CASE1, which for some reason works... im sending the string as a shader variable later on... and in CASE1 i get correct results whereas CASE2 is wrong... i dont rly see whats the difference?
// CASE 1
int size = 12;
temp.Name = new char[size+1];
for (int k = 0; k < size; k++)
{
	temp.Name[k] = d3dxEffects[i].pDefaults[n].pParamName[k];
}
temp.Name[size] = '\0';


//CASE 2
StringCchCopyA(temp.Name, MAX_PATH, d3dxEffects[i].pDefaults[n].pParamName);  


Share this post


Link to post
Share on other sites
Do you allocate temp.Name in CASE 2?
int size = 12+1;
temp.Name = new char[size];
StringCchCopyA(temp.Name, size, d3dxEffects[i].pDefaults[n].pParamName);


But better yet...



struct Whatever {
...
std::string Name;
};

...

Whatever temp;

...

temp.name = d3dxEffects[i].pDefaults[n].pParamName;

...

// to pass to shader:
foo( temp.name.c_str() );

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
thx that solved it.. had to use the data() function to get it working


Be aware that std::string::data() does not necessarily return a null-terminated string, whereas std::string::c_str() does.

Antheus's second example above shows the usage of c_str(). When you want a null terminated C-style string, you should prefer this over data().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this