Thread: Trying to copy buffers using memcpy in C under UNIX

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    Trying to copy buffers using memcpy in C under UNIX

    Hi guys,

    I've been driving myself crazy trying to figure out what's wrong here, so I thought I'd see if someone else might have a better idea.

    Basically I'm reading from a file into a void pointer called inBuf, then trying to copy the data referenced by inBuf (and offset by a variable amount called sizeInBuf - but it isn't necessary for this example) into another void pointer called inTemp (code below). However, when I try a memcpy, my inTemp pointer remains blank. It could be that I'm missing something incredibly foolish, or it could be that it's 5:30am and I've been working on this for the last twelve hours or so. Anyway, I'd appreciate any help someone might be able to provide.

    void *buffer = malloc(50000);
    void *inTemp = malloc(10000);
    char c;
    read(infd, inBuf, 5000);
    memcpy( &c, (char *) inBuf, 1 );
    printf("char for inBuf: %s\n", &c);
    memcpy( inTemp, (char *) buffer + sizeInBuf, 5000 );
    memcpy( &c, (char *) inTemp, 1 );
    printf("char for inTemp: %s\n", &c);

    The result is:
    char for inBuf: i
    char for inTemp:

    I'm certainly missing something here, but I would think that the first byte pointed to by inTemp should be the same as the first byte pointed to by inBuf - but as you can tell, it's not. inBuf returns 'i', but inTemp returns a null.

    Any thoughts?

    Thanks in advance!
    Meeper

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    I think that you could should use memset before using memcpy.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by tvenki
    I think that you could should use memset before using memcpy.
    Why write one lot of values to the array (all zeros), then overwrite them with another lot?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    2
    All of you (especially Salem), thank you! This is exactly the kind of thing an over-tired mind does.

    I'll definitely look into using memset or another more efficient method, but I wanted results working first.

    Thanks again all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  2. how to copy binary files using Unix API's
    By rohan_ak1 in forum C Programming
    Replies: 25
    Last Post: 05-07-2008, 09:12 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  5. Copy to Buffers
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-05-2004, 06:50 PM