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
Solved

copy a byte array

Posted on 2007-12-04
7
3,281 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

839 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