Solved

copy a byte array

Posted on 2007-12-04
7
3,282 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
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!

 

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

756 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