Click here to Skip to main content
15,885,944 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to add or append Extra bytes to BYTE*?
Posted

From looking at your comments, you need to dynamically allocate the size of the buffer:
BYTE *buffer = NULL; //<-- Declare
...
buffer = new BYTE[size]; //<-- Allocate once size is known
memcpy(buffer, data_source, size); //<-- Copy data over from source
...
delete [] buffer; //<-- Deallocate when done with buffer
 
Share this answer
 
v2
This question is not 100% clear. If you mean that this is a pointer to heap and you need to allocate more memory for the block of data pointer by the same pointer, you need to use realloc.

Here is how: http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/[^].

—SA
 
Share this answer
 
You want to increase the size of your BYTE array at runtime, is that what you want to achieve ?
Unfortunately, this is not directly possible in C/C++. What you could do instead is one of those solutions:
1) Use a STL container (vector, list, ...) which has support for dynamic container. This is only possible if you write C++ code
2) When you need to increase the size of the array, create a new array of the correct size, copy all the contents of the previous array and delete the previous array.
 
Share this answer
 
Comments
Gokulnath007 20-Jun-11 4:13am    
BYTE *buffer=NULL;
how can i add 40 bytes to buffer.
Cedric Moonen 20-Jun-11 4:15am    
Your buffer is NULL, so how can you add bytes to a non-existent buffer ? You will first need to allocate a buffer for 40 bytes (using new in C++ or malloc in C). Of course, you won't be able to add bytes to that buffer after that except if you use one of the solutions I provided.
Gokulnath007 20-Jun-11 4:19am    
I have read a binary file in buffer, its size is 86016 bytes and now i want to add extra bytes of 8192 to buffer.
Cedric Moonen 20-Jun-11 4:26am    
Yes, so ? What is the problem with the solutions I provided you ? Did you even read my answer ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900