What does
vector<unsigned char> vchTmp(pend-pbegin 1, 0);
mean?
This line is taken from the base58.h source code from the first commit of bitcoin source code. source here
click on base58.h
line 27
I cannot understand what does it mean.
Thanks Massimo
CodePudding user response:
inline string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
{
// Convert big endian data to little endian
// Extra zero at the end make sure bignum will interpret as a positive number
vector<unsigned char> vchTmp(pend-pbegin 1, 0);
reverse_copy(pbegin, pend, vchTmp.begin());
// ...
This means that the function takes a begin and end iterator defined as const unsigned char*
. It then creates a vector<unsigned char>
with the size of the distance between pbegin
and pend
and adds one extra unsigned char
( 1
).
Apparently that is to make "bignum" (a library dealing with big numbers) always interpret it as a positive number. The 0
is copied into all unsigned char
elements in the vector. 0
is the default value here so it's unnecessary.
It would probably be clearer to do:
vector<unsigned char> vchTmp(std::distance(pbegin, pend) 1);
The following reverse_copy
puts the data [pbegin, pend)
into vchTmp
in reverse order.
reverse_copy(pbegin, pend, vchTmp.begin());
If you'd call the function like this:
unsigned char arr[] = {'B','a','r'};
EncodeBase58(std::begin(arr), std::end(arr));
Then vchTmp
would contain 4 elements, in this order: 'r'
, 'a'
, 'B'
, 0
(except they are unsigned char
, not char
). The last 0
is the the extra 0
that was added by creating the vector with 1
above.
CodePudding user response:
The statement creates a vector
named vchTmp
with elements of type unsigned char
and with pend - pbegin 1
number of elements. Moreover all of those elements are initialized with the integer 0
.
The statement uses constructor 3 listed here:
vector( size_type count,
const T& value,
const Allocator& alloc = Allocator());
Note that the char
(and unsigned char
) data type is an integral type, meaning the underlying value is stored as an integer. Now, the integer stored by a char
(and unsigned char
) variable is intepreted as an ASCII character. And since they've specified the integer 0
which corresponds to the NUL character '\0'
in the ASCII table, this effectively means that the statement creates a vector named vchTmp
that contains pend - pbegin 1
NUL characters.
For example,
unsigned char arr[] = "stackoverflow";
unsigned char* pbegin = arr;
unsigned char*pend = arr 3;
std::vector<unsigned char> vchTmp(pend-pbegin 1, 0); //vchTmp contains 4 NUL characters
CodePudding user response:
Let's take a simple example:
std::vector<unsigned char> vec(5, 72);
This means declaring a std::vector named vec of the type unsigned char, which has 5 elements, and each of those 5 elements contains the value 72, which when translated to char (as the type of the vector is unsigned char), means 'H'. So the values in vec are:
{ 'H', 'H', 'H', 'H', 'H' }
Thus, the above line of code can also be written as:
std::vector<unsigned char> vec(5, 'H');