I have a program in C++ that has a BYTE array that stores some values. I need to find the length of that array i.e. number of bytes in that array. Please help me in this regard.
This is the code:
BYTE *res;
res = (BYTE *)realloc(res, (byte_len(res)+2));
byte_len
is a fictitious function that returns the length of the BYTE array and I would like to know how to implement it.
A byte is a group of 8 bits. A bit is the most basic unit and can be either 1 or 0. A byte is not just 8 values between 0 and 1, but 256 (28) different combinations (rather permutations) ranging from 00000000 via e.g. 01010101 to 11111111 .
Length of Array = size of array/size of 1 datatype that you are using to define an array. The logic is elaborated further programmatically in the below section [Using sizeof()].
To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.
The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.
Given your code:
BYTE *res;
res = (BYTE *)realloc(res, (byte_len(res)+2));
res
is a pointer to type BYTE
. The fact that it points to a contiguous sequence of n
BYTES is due to the fact that you did so. The information about the length is not a part of the pointer. In other words, res
points to only one BYTE
, and if you point it to the right location, where you have access to, you can use it to get BYTE
values before or after it.
BYTE data[10];
BYTE *res = data[2];
/* Now you can access res[-2] to res[7] */
So, to answer your question: you definitely know how many BYTE
s you allocated when you called malloc()
or realloc()
, so you should keep track of the number.
Finally, your use of realloc()
is wrong, because if realloc()
fails, you leak memory. The standard way to use realloc()
is to use a temporary:
BYTE *tmp;
tmp = (BYTE *)realloc(res, n*2);
if (tmp == NULL) {
/* realloc failed, res is still valid */
} else {
/* You can't use res now, but tmp is valid. Reassign */
res = tmp;
}
BYTE Data[200];
You can find the length (in elements) with the commonly used macro:
#define ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0]))
However, in C++ I prefer to use the following where possible:
template<typename T, size_t N>
inline size_t array_length(T data[N])
{
return N;
};
Because it prevents this from occurring:
// array is now dynamically allocated
BYTE* data = new BYTE[200];
// oops! this is now 4 (or 8 on 64bit)!
size_t length = ARRAY_LENGTH(data);
// this on the other hand becomes a compile error
length = array_length(data);
In C++, raw pointers (like byte*
) are not bounded. If you need the length, which you always do when working with arrays, you have to keep track of the length separately. Classes like std::vector
help with this because they store the length of the array along with the data.
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