Thread: Copy struct to char buffer

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    63

    Copy struct to char buffer

    Hi all,

    I want to copy some struct to a char buffer (to send it over the network), so how can i deal with that (memcpy doesn't work for me), below a sample of my code:

    Code:
    struct proc_state {
        long proc_id;
        long proc_mem;    
        long proc_cpu;
    };
    
    int main(int argc, char *argv[]) {
        struct proc_state *s;
        
        outbuf = malloc(1024 * sizeof (char));
    
        s = malloc(sizeof(struct proc_state ));
       
    
        read_my_proc_state(s); /* I use this function to fill my structure*/
        memcpy(outbuf, s, sizeof (s));
        
    
        printf("The buffer to send is %s\n", outbuf);
        return 0;
    }
    Thanks.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >>(memcpy doesn't work for me)
    Sure it does.

    memcpy(outbuf, s, sizeof (*s));

    s may not be the correct size that you need, because s is a pointer, but *s on the other hand...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks whiteflags but this doesn't change the result, still print some garbage data.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Can you prove that read_my_proc_state populates s correctly? I don't see you checking a return value on that, and there's no sign of debug output. Did you run it through a debugger to be sure? Can we see the read_my_proc_state function to look for possible errors there?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So memcpy is doing exactly what you tell it to do. Unfortunately, that means first you have to figure out what you want it to do. Hint: printing out long integers with %s isn't it.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks guys,

    Can you prove that read_my_proc_state populates s correctly?
    Yes it does (it reads some files and populate my struct, so i'm sure it's ok.

    So memcpy is doing exactly what you tell it to do.
    No .

    Is there any tips to convert a struct to a char * buffer?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jean.yves View Post
    No .

    Is there any tips to convert a struct to a char * buffer?
    Instead of memcpy, you should probably be using sprintf then.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you followed my advice, memcpy should have correctly copied the bytes pointed to by s to output. If you are interested in seeing the contents of output before sending it, then you will have to print it another way. The format specifier %s is for strings of text, and you are not displaying text.

    Typically you would iterate through the buffer printing like so

    printf("%x", output[i]);

    or something more clever, depending on what, exactly, you want to see.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Quote Originally Posted by whiteflags View Post
    If you followed my advice, memcpy should have correctly copied the bytes pointed to by s to output. If you are interested in seeing the contents of output before sending it, then you will have to print it another way. The format specifier %s is for strings of text, and you are not displaying text.

    Typically you would iterate through the buffer printing like so

    printf("%x", output[i]);

    or something more clever, depending on what, exactly, you want to see.
    Thanks i use this (as you suggest) to output the result and it's not ok :

    Code:
     i = 0;
            while (outbuf[i]) {
                     printf("BUFFER TO SEND %x ", outbuf[i]);
                    i++;
            }
            printf("\n");

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    I got some HEXA words but i want strings

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't want your numbers to print in hex, why are you using %x?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't have a string, you have binary data that just happens to be stored in a character buffer. It is not null terminated, so you can't work with it like you do a string. In fact, there is almost certainly going to be null characters in there that you actually want since you use 4 bytes to store proc_id, but it's almost certainly a 2-byte value, so you have at least two zero bytes (null characters) in your struct.

    You could try using something other than %s in a printf. Read the documentation on how to print out long integers using printf, then print s->proc_id, etc.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Thanks i use this (as you suggest) to output the result and it's not ok :

    The code that you posted will stop on the first zero byte, similar to %s. Again, if this is for verification, you most likely want to see if the numbers correspond with the numbers in the structure. The reason you can't print the bytes of a long with %s is because you may have some zero bytes in the actual number, depending on how it is expressed in base 2. If properly done, you will see all the longs expressed in hexadecimal.

    If you are interested in seeing the numbers again in base 10 so you don't have to do any conversions yourself to check and see correctness, you should fold output into a structure instance again and print that.

    Code:
    while there is more stuff in output:
       for each long in the structure:
          fold the next sizeof(long) bytes of output into the long (maybe newlong <<= output[i]; )
          print the long
       end for
    endwhile
    This way you visually confirm that output contains your structure.

    And usually when I work with bytes, I use unsigned char, since that will remove the purpose of the sign bit and guarantee the bit-for-bit correctness of what is represented. I don't know if that makes a difference here, yet.
    Last edited by whiteflags; 12-14-2010 at 04:40 PM.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Quote Originally Posted by whiteflags View Post
    >> Thanks i use this (as you suggest) to output the result and it's not ok :

    The code that you posted will stop on the first zero byte, similar to %s. Again, if this is for verification, you most likely want to see if the numbers correspond with the numbers in the structure. The reason you can't print the bytes of a long with %s is because you may have some zero bytes in the actual number, depending on how it is expressed in base 2. If properly done, you will see all the longs expressed in hexadecimal.

    If you are interested in seeing the numbers again in base 10 so you don't have to do any conversions yourself to check and see correctness, you should fold output into a structure instance again and print that.

    Code:
    while there is more stuff in output:
       for each long in the structure:
          fold the next sizeof(long) bytes of output into the long (maybe newlong <<= output[i]; )
          print the long
       end for
    endwhile
    This way you visually confirm that output contains your structure.

    And usually when I work with bytes, I use unsigned char, since that will remove the purpose of the sign bit and guarantee the bit-for-bit correctness of what is represented. I don't know if that makes a difference here, yet.
    Thanks i used your code and it works .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM