Solved

copy a byte array

Posted on 2007-12-04
7
3,283 Views
Last Modified: 2013-12-14
How to copy a byte array to another byte array ? The following is my code :

BYTE* ba2;

void CopyByteArray(LPVOID lpInBuf) // lpInBuf is a byte array
{
  ba2 = (BYTE *) malloc(30);
  memcpy (ba2, (BYTE*) lpInBuf, sizeof lpInBuf);
}

I have 2 questions here :
1) is it correct to use memcpy ?
2) to pass as an parameter to memcpy, how to get the size of the byte array ? sizeof always gives "4"

Thanks
0
Comment
Question by:walterwkh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 500 total points
ID: 20408595
your example is very strange, I suposse you mean:

BYTE CopyByteArray(LPBYTE lpInBuf, int size) // lpInBuf is a byte array
{
    BYTE *ba2 = (BYTE *) malloc(size);
    memcpy (ba2, lpInBuf, size);
}

Notice you cannot know the size on the array based on the pointer to the array, you have to specify explicity
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20408732
Is that what you want?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20408745
To add up to that, 'sizeof(lpInBuf)' wil always equal four on 32bit systems, that's why you have to specify the size additionally.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:walterwkh
ID: 20408751
Jaime, if my byte array is not a char array, is it still valid to use memcpy ?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20408771
yes, you always can use memcpy, but you have to pay attention to the 'size' argument (the third one). It always should be expressed in bytes.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20408789
I noticed a bug in my initial code, it should be:

BYTE *CopyByteArray(BYTE *lpInBuf, int size) // lpInBuf is a byte array
{
    BYTE *ba2 = (BYTE *) malloc(size);
    memcpy (ba2, lpInBuf, size);
    return ba2;
}

there are some things to remark:
malloc returns a void pointer, so the (BYTE *) casting was needed.
memcpy expects 2 void pointers, so any pointer can be passed wihout casting to void *
the function now returns the pointer to the new buffer, you are responsible to free it.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20408792
In fact, you might want to use 'sizeof(DATATYPE)' as a multiplier if yoi are passing the amount of elements. But, 'char' is one byte, so you are fine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question