Thread: byte array copying problem

  1. #1
    Registered User TmX's Avatar
    Join Date
    Sep 2006
    Posts
    14

    byte array copying problem

    Let's say I have this following byte array:
    Code:
    {0xAB, 0x34, 0x15, 0xFD, 0x76, 0xED};
    What I want is to have a new array which consists of:
    Code:
    {0x34, 0x15, 0xFD, 0x76}
    This code works as expected (tested using MSVC 2015 update 3):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef unsigned char BYTE;
    
    void dump(BYTE*);
    
    int main(void){
    	BYTE src[6] = {0xAB, 0x34, 0x15, 0xFD, 0x76, 0xED};
    	BYTE dst[6];
    	
    	memcpy(&dst, src+1, 4);
    	dump(dst);
    	return 0;
    }
    
    void dump(BYTE* bArr){
    	int x;
    	for (x = 0; x < sizeof(bArr)/sizeof(BYTE); x++){
    		if (x > 0) printf(":");
    		printf("%02x",bArr[x]);
    	}
    }
    Now I want another new array which consists of:
    Code:
    0x34, 0x15, 0xFD, 0x76, 0xED}
    So I change
    Code:
    memcpy(&dst, src+1, 4);
    into
    Code:
    memcpy(&dst, src+1, 5);
    Surprisingly, the output is still the same:
    Code:
    34:15:fd:76
    Hmm what goes wrong here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,736
    Your use of sizeof() in the dump function is wrong.

    It isn't telling you what the real size of the array is, because all you have is a pointer.

    > memcpy(&dst, src+1, 4);
    Also, since you're dealing with arrays, it suffices to say
    memcpy(dst, src+1, 4);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User TmX's Avatar
    Join Date
    Sep 2006
    Posts
    14
    Hi salem,

    Thanks for the hint. After some Googling, it seems the correct keyword is "array decaying".

    This one is the correct version, I think.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef unsigned char BYTE;
    
    void dump(BYTE*, size_t);
    
    int main(void){
    	BYTE src[6] = {0xAB, 0x34, 0x15, 0xFD, 0x76, 0xED};
    	BYTE dst[6];
    	
    	memcpy(dst, src+1, 5);
    	dump(dst, 5);
    	return 0;
    }
    
    void dump(BYTE* bArr, size_t size){
    	BYTE* end = bArr + size;
    	int x = 0;
    	
    	while (bArr < end){
    		if (x > 0) printf(":");
    		printf("%02x", *bArr++);
    		++x;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-22-2016, 03:20 AM
  2. Why is there one extra byte when copying file?
    By xbfish in forum C Programming
    Replies: 3
    Last Post: 07-21-2011, 03:15 PM
  3. Array copying problem
    By arun10427 in forum C Programming
    Replies: 4
    Last Post: 03-04-2010, 01:25 AM
  4. Simple Byte Array problem
    By OldGit in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-20-2009, 05:45 AM
  5. Problem Copying char array
    By NullStyle in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 08:06 AM

Tags for this Thread