4

this is one area of C/C++ that i have never been good at.

my problem is that i have a string that will need to eventually contain some null characters. treating everything as a char array (or string) won't work, as things tend to crap out when they find the first null. so i thought, ok, i'll switch over to uint8_t, so everything is just a number. i can move things around as needed, and cast it back to a char when i'm ready.

my main question right now is: how can i copy a portion of a string to an uint8_t buffer?

effectively, i'd like to do something like:

std::string s = "abcdefghi";
uint8_t *val = (uint8_t*)malloc(s.length() + 1);
memset(val, 0, s.length() + 1);

// Assume offset is just some number
memcpy(val + offset, s.substr(1, 5).c_str(), 5);

obviously, i get an error when i try this. there is probably some sort of trickery that can be done in the first argument of the memcpy (i see stuff like (*(uint8_t*)) online, and have no clue what that means).

any help on what to do?

and while i am here, how can i easily cast this back to a char array? just static_cast the uint8_t pointer to a char pointer?

thanks a lot.

6
  • 5
    std::string is perfectly capable of containing '\0' characters. There's no reason to have to do this unless you're using C-string manipulation functions, or other APIs that only take char*s with no length. And those APIs won't do the right thing for strings that have embedded nulls anyway. May 31, 2012 at 19:45
  • after reading your note, i started looking around. std::string might work out. i will perform some tests. looks like i'm fine as long as i keep away from c_str. i'd still like to know the answer to the above question, just out of curiosity. May 31, 2012 at 19:52
  • Things like (*(uint8_t*)) are often casts to a more abstract data type, then a cast that becomes legal at that point but wasn't previously, then a cast back down. It's used often when casting function pointers, for example (DWORD)(*(void**))(void(*)(int)) (-ish, haven't used it in a while). Just for reference. :)
    – ssube
    May 31, 2012 at 19:55
  • 1
    "obviously, i get an error when i try this". Don't make us guess. What error message do you get?
    – Robᵩ
    May 31, 2012 at 20:11
  • You have multiple incorrect underlying assumptions. It is difficult to answer your question as a result. char arrays are perfectly capable of holding nul characters, as a std::string objects. Further, there is nothing that uint8_t does that char doesn't do equally well. Please show us what you have tried, and we'll tell you where you are going wrong. Can you create a 10-line program that demonstrates what is going wrong for you? See sscce.org.
    – Robᵩ
    May 31, 2012 at 20:18

2 Answers 2

1

i thought, ok, i'll switch over to uint8_t, so everything is just a number.

That's not going to make algorithms that look for a '\0' suddenly stop doing it, nor do algorithms that use char have to pay attention to '\0'. Signaling the end with a null character is a convention of C strings, not char arrays. uint8_t might just be a typedef for char anyway.

As Nicol Bolas points out std::string is already capable of storing strings that contain the null character without treating the null character specially.

As for your question, I'm not sure what error you're referring to, as the following works just fine:

#include <iostream>
#include <string>
#include <cstdint>
#include <cstring>

int main() {
    std::string s = "abcdefghi";
    std::uint8_t *val = (std::uint8_t*)std::malloc(s.length() + 1);
    std::memset(val, 0, s.length() + 1);

    int offset = 2;
    std::memcpy(val + offset, s.substr(1, 5).c_str(), 5);
    std::cout << (val+offset) << '\n';
}

The memcpy line takes the second through sixth characters from the string s and copies them into val. The line with cout then prints "bcdef".

Of course this is C++, so if you want to manually allocate some memory and zero it out you can do so like:

std::unique_ptr<uint8_t[]> val(new uint8_t[s.length()+1]());

or use a vector:

std::vector<uint8_t> val(s.length()+1,0);

To cast from an array of uint8_t you could (but typically shouldn't) do the following:

char *c = reinterpret_cast<uint8_t*>(val);
0

Well, the code works ok, it copies the substring in val. However, you will have 0s on all the positions until the offset.

e.g. for offset=2 val would be {0, 0, b, c, d, e, f, 0, 0, 0}

If you print this, it will show nothing because the string is null terminated on the first position (I guess this is the error you were talking about...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.