concatonate memory buffers

This is a discussion on concatonate memory buffers within the C Programming forums, part of the General Programming Boards category; I have two things I'd like to concatenate together uint 8*buffer; // this is just a data buffer struct *aHeaderStruct; ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    concatonate memory buffers

    I have two things I'd like to concatenate together

    uint 8*buffer; // this is just a data buffer

    struct *aHeaderStruct; // some header structure

    I'd like to prepend the header struct in front of the data buffer.

    What is the best way to do that?

    Thanks

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    memcpy() would work into a third buffer, however, the _BEST_ way would be to make another structure that contains both of the items you want.

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    959
    Can I ask why you would want to do such a thing?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it99 View Post
    I'd like to prepend the header struct in front of the data buffer.
    Just do it then.

    I think you need to provide some more context WRT where the "prepending" will occur. Like you want another struct? You want to transmit this thru a socket? You want to write it to a file? What?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Quote Originally Posted by MK27 View Post
    Just do it then.

    I think you need to provide some more context WRT where the "prepending" will occur. Like you want another struct? You want to transmit this thru a socket? You want to write it to a file? What?

    I am trying to send these through a socket. I have a structure I want to prepend to a buffer of data. So I really just want it as a buffer of data that I can send through socket.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    uint_8 *buffer; // this is just a data buffer
    struct somestruct *aHeaderStruct; // some header structure
    int sock; // your socket
    send (sock, aHeaderStruct, sizeof(struct somestruct),0);
    send (sock, buffer, sizeof(uint_8),0);
    The sizes there may not be inappropriate, but there is nothing special you need to do. You just send the data.

    It's a good idea to use the return value of send with sockets in a loop to make sure all the data has been sent/recieved, eg:
    Code:
    unsigned char *data;
    int sz = // the data length
    int total = 0, sent;
    while (total < sz) {
        sent = send(sock,&data[total],sz-total,0);
        total += sent;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem - malloc, pointers and vector
    By DrSnuggles in forum C++ Programming
    Replies: 18
    Last Post: 08-14-2009, 10:28 AM
  2. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM

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