• Advertisement
Sign in to follow this  

std::string versus char[255] ?

This topic is 465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I've noticed/ had a bug when I used a std::string to store a variable in a struct, which was gone when I used a char array [255].

Below the 2 pieces of code, 1st being ok/preditable, 2nd not working.

struct DX11_VS_INPUT_ELEMENTDESC
{
	
	DX11_VS_INPUT_ELEMENTDESC(D3D11_INPUT_ELEMENT_DESC pInputDesc)
	{
		memset(SemanticName, 0, 255); 
		strcpy_s(SemanticName, pInputDesc.SemanticName);

		ElementDesc = pInputDesc;
		ElementDesc.SemanticName = nullptr;
	}

	D3D11_INPUT_ELEMENT_DESC	ElementDesc;
	char		 				SemanticName[255];
};

With the std::string:

struct DX11_VS_INPUT_ELEMENTDESC
{
	
	DX11_VS_INPUT_ELEMENTDESC(D3D11_INPUT_ELEMENT_DESC pInputDesc)
	{
		SemanticName = pInputDesc.SemanticName;

		ElementDesc = pInputDesc;
		ElementDesc.SemanticName = nullptr;
	}

	D3D11_INPUT_ELEMENT_DESC	ElementDesc;
	std::string					SemanticName;
};

At some point I create a checksum based on a std::vector of those struct's objects.

Results are unpredictable.

 

I think it has something to do with the fact that pInputDesc.SemanticName is a LPCSTR (no string by value).

Do you know how this can be explained and/or how this will work OK with a std::string?

 

Any input is appreciated.

Share this post


Link to post
Share on other sites
Advertisement

At some point I create a checksum based on a std::vector of those struct's objects.
Results are unpredictable.


Post the checksumming code?

std::string is essentially just a pointer and a length, or two pointers. If your checksum code doesn't take that into account, you'll get different results every time as the pointers will be different every time.

Share this post


Link to post
Share on other sites
Thanks guys.
I dont want to store the pointer in the struct (for which the checksum is calculated), so I want to store the characters/ array in the struct. Would that simply mean char[] is the only option, or can I use a string which is constructed using the chars that fhe LPCSTR points too? Maybe like this:

LPCSTR bla = "sometext";
std::string myString = std::string(bla);

Share this post


Link to post
Share on other sites

Thanks. I understand now, basically .c_str() returns the array of characters (instead of the pointer to it).

I'll have to figure out if/ how I'll do that, because the variable is part of a struct, and the struct is hashed as a whole (void*). In that case it might be a good idea too just store the char array instead of a std::string.

Share this post


Link to post
Share on other sites

Why do you need to hash things via a void*?

 

Seems to me like you could have a generic hash function, which can be overloaded or specialised for DX11_VS_INPUT_ELEMENTDESC.

Share this post


Link to post
Share on other sites

Sorry, I'm actually hashing the DX11_VS_INPUT_ELEMENTDESC struct object.

 

Which contains the D3D11 inputElementDesc + a char array (or string :)) for the semanticName. In the D3D11 elementDesc I assign nullptr to the semanticname (LPCSTR) because of the exact reason above.

Share this post


Link to post
Share on other sites

My point remains though - I don't see why you can't have a specific function that can take the existence of a std::string into account.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement