Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum has no buffer #2

Open
imtrobin opened this issue Oct 26, 2017 · 7 comments
Open

Enum has no buffer #2

imtrobin opened this issue Oct 26, 2017 · 7 comments

Comments

@imtrobin
Copy link

imtrobin commented Oct 26, 2017

Hi, I tested with enum with default value 0, the buffer stored is empty. v0.9.9

Repro

#include "SimpleBinStream/SimpleBinStream.h"
#include <iostream>
#include <sstream>
using namespace std;

enum TestEnum : unsigned char
{
	  EnumOne
	, EnumTwo
	, EnumThree

// If I use this, then the buffer shows out properly
// 	 EnumOne    = 1 
// 	, EnumTwo   = 2 
// 	, EnumThree = 3

	, End
};

int main (int argc, char * argv [])
{
	simple::mem_ostream <std::true_type>  out;

	// this is problem
	out << TestEnum::EnumOne;
	// this is fine
	//out << TestEnum::EnumOne;

	const char * buffer = reinterpret_cast <const char*> (out.get_internal_vec ().data ());
	int buffersize = out.get_internal_vec ().size ();

	// Output buffer here is nothing unless I assign values to the enum
 	cout << "out\nBufferSize:" << buffersize << " buffer:" << buffer << "*" << endl;
	return 0;
}

@shaovoon
Copy link
Owner

I do not have access to C++ compiler now. I'll test when I get home. Zero is NULL terminator in char array and strings. Possible that your string is terminated by NULL. You should try to compare the enum to the original enum and see if the values are the same.

@imtrobin
Copy link
Author

the string terminating char shold be '\0', not 0.

@shaovoon
Copy link
Owner

You can run the code below in cpp.sh or other online C++ compiler.

#include <iostream>

int main()
{
  if('\0' == 0)
  {
    std::cout << "same" << std::endl;
  }
  else
  {
    std::cout << "diff" << std::endl;
  }
  return 0;  
}

@shaovoon
Copy link
Owner

There are size differences between '\0' and 0.

'\0' is 1 byte zero while literal 0 in source code is usually translated to at least 4 bytes.

@imtrobin
Copy link
Author

the value of '\0' is 0 so comparing it to 0 will be same ,but I think in case of enum as value 0, should be different.

@shaovoon
Copy link
Owner

But you cast it to const char*

@imtrobin
Copy link
Author

yes, so that I can serialize it over network. I would expect the enum to be a 0, even if I changed the enum to be of type int, I think same result (I don't have compiler now)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants