Solved

copy a byte array

Posted on 2007-12-04
7
3,272 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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 viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now