Solved

copy a byte array

Posted on 2007-12-04
7
3,288 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
Technology Partners: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

635 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