CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: All combinations of 0's and 1's in 128 bit character array?

    Quote Originally Posted by itsmeandnobodyelse View Post
    The vector already holds an internal 16-byte elements array.

    If you do

    Code:
         unsigned char * p = (unsigned char *)&v[0];
         some_otherfunction_expecting_a_16bytes_array(p);
    where v is one of the permutations you got the pointer p would point to a 16-elements array.

    If you want to copy the array to fixed-sized char array, you would/could do

    Code:
         unsigned char arr[16] = { '\0' };
         unsigned char * p = (unsigned char *)&v[0];
         memcpy(arr, p, min(sizeof(arr), v.size() * sizeof(v[0])));
    The min is only to make sure that the vector really has 16 elements of type char, same as the arr.

    Regards, Alex
    That is all very true, but the problem remains that if you store bytes inside the vector, when you call next_permutation, you will be per mutating bytes, and not bits. Sure, you can recover the chars easily, but they won't be properlly shifted

    A good solution would have been to use a vector<bool>, as it stores bit(-like) objects. This would work perfectly if you could write &v[0] on a vector bool, but you can't (at least not to my knowledge).

    IMO, it's a lose-lose situation, where you have to get creative and daisy chain several solutions.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: All combinations of 0's and 1's in 128 bit character array?

    Quote Originally Posted by monarch_dodra View Post
    That is all very true, but the problem remains that if you store bytes inside the vector, when you call next_permutation, you will be per mutating bytes, and not bits.
    So what? Make your container 8x larger, and you'll have logically the same result.

  3. #18
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: All combinations of 0's and 1's in 128 bit character array?

    Quote Originally Posted by Lindley View Post
    So what? Make your container 8x larger, and you'll have logically the same result.
    Yes, if you make your container 8 times larger, you get the desired result, but from there, it's the copying back into the char array that becomes problematic :/
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #19
    Join Date
    Dec 2009
    Posts
    145

    Re: All combinations of 0's and 1's in 128 bit character array?

    Quote Originally Posted by itsmeandnobodyelse View Post
    The vector already holds an internal 16-byte elements array.

    If you do

    Code:
         unsigned char * p = (unsigned char *)&v[0];
         some_otherfunction_expecting_a_16bytes_array(p);
    where v is one of the permutations you got the pointer p would point to a 16-elements array.

    If you want to copy the array to fixed-sized char array, you would/could do

    Code:
         unsigned char arr[16] = { '\0' };
         unsigned char * p = (unsigned char *)&v[0];
         memcpy(arr, p, min(sizeof(arr), v.size() * sizeof(v[0])));
    The min is only to make sure that the vector really has 16 elements of type char, same as the arr.

    Regards, Alex

    the index should be 15

  5. #20
    Join Date
    Apr 2007
    Posts
    87

    Re: All combinations of 0's and 1's in 128 bit character array?

    Hmm, actually, using bitset instead of a vector and then just memcpying into a char array of length 16 (bytes) works

    Thanks all!

  6. #21
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: All combinations of 0's and 1's in 128 bit character array?

    Yikes. You shouldn't use memcpy() on C++ objects.

    If it's just an assignment, whatever. But if you actually intend this code to be portable and robust, that's not a good approach.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured