How to pass array of byte?
-
I am trying to pass an array of binary data from one function to another, but it fails. Why?
void C1:Caller()
{
BYTE *pTemplate = NULL; DWORD dwSize=0;
GetData(pTemplate, dwSize);
// in here dwSize=1024, which is correct
// but pTemplate is NULL! Why?
}void C1:GetData(BYTE *pTemplate, DWORD &dwSize)
{
delete pTemplate;
pTemplate = NULL;dwSize = 1024;
pTemplate = new BYTE[dwSize];
// ... fill pTemplate with desired data
}Anyone can help me here? Thanks.
-
I am trying to pass an array of binary data from one function to another, but it fails. Why?
void C1:Caller()
{
BYTE *pTemplate = NULL; DWORD dwSize=0;
GetData(pTemplate, dwSize);
// in here dwSize=1024, which is correct
// but pTemplate is NULL! Why?
}void C1:GetData(BYTE *pTemplate, DWORD &dwSize)
{
delete pTemplate;
pTemplate = NULL;dwSize = 1024;
pTemplate = new BYTE[dwSize];
// ... fill pTemplate with desired data
}Anyone can help me here? Thanks.
If you want to only pass the content of the array, then your method is fine. But as you want to allocate the a new array inside the function, you need to pass the array by reference. Think of it this way: a pointer is more or less the same as an integer containing an address. If you pass the integer value by value, a copy will be made in the function and the function will modify the copy and leave the original intact. It is the same with pointers: as long as you only want to access the content of the pointed memry, no problem. But if you want to assign a new address (like new is doing), then you have to pass it by reference:
void C1:GetData(BYTE* &pTemplate, DWORD &dwSize)
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
If you want to only pass the content of the array, then your method is fine. But as you want to allocate the a new array inside the function, you need to pass the array by reference. Think of it this way: a pointer is more or less the same as an integer containing an address. If you pass the integer value by value, a copy will be made in the function and the function will modify the copy and leave the original intact. It is the same with pointers: as long as you only want to access the content of the pointed memry, no problem. But if you want to assign a new address (like new is doing), then you have to pass it by reference:
void C1:GetData(BYTE* &pTemplate, DWORD &dwSize)
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++I knew I was missing something simple, but I do not expect it to be that simple. Thanks a lot.
-
I am trying to pass an array of binary data from one function to another, but it fails. Why?
void C1:Caller()
{
BYTE *pTemplate = NULL; DWORD dwSize=0;
GetData(pTemplate, dwSize);
// in here dwSize=1024, which is correct
// but pTemplate is NULL! Why?
}void C1:GetData(BYTE *pTemplate, DWORD &dwSize)
{
delete pTemplate;
pTemplate = NULL;dwSize = 1024;
pTemplate = new BYTE[dwSize];
// ... fill pTemplate with desired data
}Anyone can help me here? Thanks.
Joe Smith IX wrote:
Anyone can help me here?
Another solution is to pass the address of
pTemplate
toGetData()
."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch