What is the use of unsigned char
pointers? I have seen it at many places that pointer is type cast to pointer to unsinged char
Why do we do so?
We receive a pointer to int
and then type cast it to unsigned char*
. But if we try to print element in that array using cout it does not print anything. why? I do not understand. I am new to c++.
EDIT Sample Code Below
int Stash::add(void* element)
{
if(next >= quantity)
// Enough space left?
inflate(increment);
// Copy element into storage, starting at next empty space:
int startBytes = next * size;
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < size; i++)
storage[startBytes + i] = e[i];
next++;
return(next - 1); // Index number
}
Unsigned char must be used for accessing memory as a block of bytes or for small unsigned integers. Signed char must be used for small signed integers and simple char must be used only for ASCII characters and strings.
If your values range is [0,255] you should use unsigned char but if it is [-128,127] then you should use signed char . Suppose you are use the first range ( signed char ), then you can perform the operation 100+100 . Otherwise that operation will overflow and give you an unexpected value.
unsigned char is a character datatype where the variable consumes all the 8 bits of the memory and there is no sign bit (which is there in signed char). So it means that the range of unsigned char data type ranges from 0 to 255.
You would use a signed char when you need to represent a quantity in the range [-128, 127] and you can't (for whatever reason) spare more than a single byte to do it.
You are actually looking for pointer arithmetic:
unsigned char* bytes = (unsigned char*)ptr;
for(int i = 0; i < size; i++)
// work with bytes[i]
In this example, bytes[i]
is equal to *(bytes + i)
and it is used to access the memory on the address: bytes + (i* sizeof(*bytes))
. In other words: If you have int* intPtr
and you try to access intPtr[1]
, you are actually accessing the integer stored at bytes: 4 to 7:
0 1 2 3
4 5 6 7 <--
The size of type your pointer points to affects where it points after it is incremented / decremented. So if you want to iterate your data byte by byte, you need to have a pointer to type of size 1 byte (that's why unsigned char*
).
unsigned char
is usually used for holding binary data where 0
is valid value and still part of your data. While working with "naked" unsigned char*
you'll probably have to hold the length of your buffer.
char
is usually used for holding characters representing string and 0
is equal to '\0'
(terminating character). If your buffer of characters is always terminated with '\0'
, you don't need to know it's length because terminating character exactly specifies the end of your data.
Note that in both of these cases it's better to use some object that hides the internal representation of your data and will take care of memory management for you (see RAII idiom). So it's much better idea to use either std::vector<unsigned char>
(for binary data) or std::string
(for string).
The unsinged char
type is usually used as a representation of a single byte
of binary data. Thus, and array is often used as a binary data buffer, where each element is a singe byte.
The unsigned char*
construct will be a pointer to the binary data buffer (or its 1st element).
I am not 100% sure what does c++
standard precisely says about size of unsigned char
, whether it is fixed to be 8 bit or not. Usually it is. I will try to find and post it.
After seeing your code
When you use something like void* input
as a parameter of a function, you deliberately strip down information about inputs original type. This is very strong suggestion that the input will be treated in very general manner. I.e. as a arbitrary string of bytes. int* input
on the other hand would suggest it will be treated as a "string" of singed integers.
void*
is mostly used in cases when input gets encoded, or treated bit
/byte
wise for whatever reason, since you cannot draw conclusions about its contents.
Then In your function you seem to want to treat the input as a string of bytes. But to operate on objects, e.g. performing operator=
(assignment) the compiler needs to know what to do. Since you declare input as void*
assignment such as *input = something
would have no sense because *input
is of void
type. To make compiler to treat input
elements as the "smallest raw memory pieces" you cast it to the appropriate type which is unsigned int
.
The cout
probably did not work because of wrong or unintended type conversion. char*
is considered a null terminated string and it is easy to confuse singed
and unsigned
versionin code. If you pass unsinged char*
to ostream::operator<<
as a char*
it will treat and expect the byte
input as normal ASCII characters, where 0
is meant to be end of string not an integer value of 0
. When you want to print contents of memory it is best to explicitly cast pointers.
Also note that to print memory contents of a buffer you would need to use a loop, since other wise the printing function would not know when to stop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With