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

vector of string to FixedLenStringArray droppoing the last element #791

Closed
ronzhou opened this issue Jul 10, 2023 · 4 comments
Closed

vector of string to FixedLenStringArray droppoing the last element #791

ronzhou opened this issue Jul 10, 2023 · 4 comments

Comments

@ronzhou
Copy link

ronzhou commented Jul 10, 2023

Describe the bug
When transfering a vector of string to FixedLenStringArray, the last element will get lost.

To Reproduce

std::vector<std::string> x{"abc", "def"};
HighFive::FixedLenStringArray<3> arr(x);
std::cout << x.size() << ":" << arr.size() << std::endl;
assert(x.size() == arr.size() && "size must equal");
@1uc
Copy link
Collaborator

1uc commented Jul 10, 2023

I think this is due assumptions about what a string is, precisely. The string literal "abc" is 4 elements long a, b, c and \0. As far as I can tell from looking at the code, e.g.,

const char* src = (iter_begin++)->c_str();
const size_t length = std::min(N - 1, std::strlen(src));
std::memcpy(dst_array.data(), src, length);
dst_array[length] = 0;

HighFive assumes that all strings are null-terminated and enforces this fact. Please try:

HighFive::FixedLenStringArray<4> arr(x);

@1uc
Copy link
Collaborator

1uc commented Jul 10, 2023

We're currently working towards supporting strings more cleanly, see #744.

@ronzhou
Copy link
Author

ronzhou commented Jul 11, 2023

@1uc It still raise the size unequal assertion error after change the length from 3 to 4.

@1uc
Copy link
Collaborator

1uc commented Jul 11, 2023

You're right, and I didn't read sufficiently carefully. There's a PR that addresses the bug #797. Does it fix the issue?

@1uc 1uc closed this as completed Aug 2, 2023
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