开发者

Length of a BYTE array in C++

开发者 https://www.devze.com 2022-12-18 14:56 出处:网络
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.

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:

BY开发者_如何学PythonTE *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.


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 BYTEs 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;
}


If the array is a fixed size array, such as:

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);

If the array is not a fixed size array:

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.


In the C way of doing things (which is also relevant to C++) you generally need to keep a record of how long your array is:

BYTE *res;
int len = 100
res = (BYTE *)realloc(res, (byte_len(len)));
len += 2;
res = (BYTE *)realloc(res, (byte_len(len)));

An alternative in the C++ way of doing things s to use the std::vector container class; a vector has the ability to manage the length of the array by itself, and also deals with the issues of memory management..

EDIT: as others have pointed out the use of realloc here is incorrect as it will lead to memory leaks, this just deals with keeping track of the length. You should probably accept one of the other replies as the best answer


Given the information you seem to have available, there is no way to do what you want. When you are working with arrays allocated on the heap, you need to save the size somewhere if you need to work with it again. Neither new nor malloc will do this for you.

Now, if you have the number of items in the array saved somewhere, you can do this to get the total size in characters, which is the unit that realloc works with. The code would look like this:

size_t array_memsize = elems_in_array * sizeof(BYTE);

If you are really working with C++ and not C I would strongly suggest that you use the vector template for this instead of going to malloc and realloc. The vector template is fast and not anywhere near as error prone as rolling your own memory management. In addition, it tracks the size for you.


When you allocate the pointer initially you also need to keep track of the length:

size_t  bufSize  = 100;
BYTE*   buf      = malloc(sizeof(BYTE ) * bufSize);

When you re-allocate you should be carefull with the re-alloc:

BYTE*   temp     = realloc(buf,sizeof(BYTE ) * (bufSize+2));
if (temp != NULL)
{
    bufSize  += 2;
    buf       = temp;
}


If it is a local variable allocated on the stack you can calculate it like this:

BYTE array[] = { 10, 20, 30, ... };
size_t lenght = sizeof(array) / sizeof(BYTE);

If you receive a pointer to the beginning of the array or you allocate it dynamically(on the heap), you need to keep the length as well as the pointer.

EDIT: I also advise you use STL vector for such needs because it already implements dynamic array semantics.

0

精彩评论

暂无评论...
验证码 换一张
取 消