The length of a BYTE array in C ++

I have a C ++ program that has a BYTE array that stores some values. I need to find the length of this array, i.e. the number of bytes in this 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 dummy function that returns the length of a BYTE array, and I would like to know how to implement it.

+6
c ++
source share
6 answers

Given your code:

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

res is a pointer to a BYTE type. The fact that it points to a continuous sequence of n BYTES is due to the fact that you did it. Length information is not part of the index. In other words, res points to only one BYTE , and if you point it to the right place that 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 much BYTE you allocated when you called malloc() or realloc() so that you keep track of the number.

Finally, using realloc() is incorrect, because if realloc() fails, you will skip 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; } 
+7
source share

If the array is a fixed-size array, for example:

 BYTE Data[200]; 

You can find the length (in elements) with a commonly used macro:

 #define ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0])) 

However, in C ++, I prefer to use the following when possible:

 template<typename T, size_t N> inline size_t array_length(T data[N]) { return N; }; 

Because it prevents this:

 // 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 ++, source pointers (e.g. byte* ) are unlimited. If you need the length that you always do when working with arrays, you need to track the length separately. Classes like std::vector help with this because they preserve the length of the array along with the data.

+3
source share

In a way of doing things (which is also related to C ++), you usually 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 C ++ - a way to do things to use the std :: vector container class; the vector has the ability to control the length of the array by itself, and also deals with memory management problems.

EDIT: as others have indicated that using realloc is wrong here, as this will lead to memory leaks, this only applies to length tracking. You should probably accept one of the other answers as the best answer.

+3
source share

Given the information that you seem to have available, you cannot do what you want. When you work 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 elements in an array stored somewhere, you can do this to get the total size in characters, which is the unit that realloc works with. The code will look like this:

 size_t array_memsize = elems_in_array * sizeof(BYTE); 

If you really work with C ++ and not C, I highly recommend that you use a vector template for this instead of switching to malloc and realloc . A vector template is fast and not anywhere near an error prone to rolling back your own memory management. In addition, it keeps track of the size for you.

+2
source share

When you first assign a pointer, you also need to track the length:

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

When redistributing, you must be careful with redistribution:

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

If this is a local variable allocated on the stack, you can calculate it as follows:

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

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

EDIT: I also advise you to use the STL vector for such needs since it already implements the semantics of a dynamic array.

+1
source share

All Articles