Thread: memcpy, malloc and a world of pain!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    memcpy, malloc and a world of pain!

    Hi

    I am trying to combine two uint8_t values into a single uint16_t value, however no matter what I try I keep getting segmentation faults. Can someone explain to me what I am doing wrong – it would be greatly appreciated!

    Thanks

    Code:
    int combineHex(uint8_t x, uint8_t y) {
    	
    	uint16_t *temp = malloc(16);
    	int value;
    	
    	if(temp != NULL) {
    		memcpy(&temp, &x, 8 );
    		memcpy(&temp+8, &y,8);
    		// do something with temp	
    	} // if
    	
    	free(temp);
    					
    	return value;
    	
    } // combine

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    temp+8 moves, not 8 bytes, but 8 of <whatever temp is>, so you're looking at 8x16 bytes down the road.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    Oh right – If that’s the case how would I go about getting two 8 bit numbers into a single 16 bit number?

    03 & FF = 03FF

    Many thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not result = 03 * 0x100 + FF?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    My friend you are an utter genius! Thank you very much – That has been bugging me for hours!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not to
    Code:
    uint16_t res = x;
    res <<= 8;
    res |= y;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int combineHex(uint8_t x, uint8_t y) {
    	
    	uint16_t *temp = malloc(16);
    	int value;
    	
    	if(temp != NULL) {
    		memcpy(&temp, &x, 8 );
    		memcpy(&temp+8, &y,8);
    		// do something with temp	
    	} // if
    	
    	free(temp);
    					
    	return value;
    	
    } // combine
    If you want to do things like this, it's best to work with uint8_t (unsigned char) because it's only one byte.
    Plus you're passing the address of the pointer to malloc which expects a pointer to allocated memory. Since temp is only 4 bytes, you're getting a nasty buffer overrun.

    http://cpwiki.sf.net/Buffer_overrun

    That mentioned, vart's idea is the simplest and the least error prone way of doing it, so go with that instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed