memcpy, malloc and a world of pain!

This is a discussion on memcpy, malloc and a world of pain! within the C Programming forums, part of the General Programming Boards category; Hi I am trying to combine two uint8_t values into a single uint16_t value, however no matter what I try ...

  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,185
    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    why not to
    Code:
    uint16_t res = x;
    res <<= 8;
    res |= y;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21