how to save 2 shorts and a bunch of unsigneds in a char* array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bats
    New Member
    • Aug 2009
    • 2

    how to save 2 shorts and a bunch of unsigneds in a char* array?

    I'm writing an XSUB to glue an existing C module to my perl code, but I'm kind of new to C.
    I want to save 2 shorts and n unsigneds in a char * array (so I can return the array to the perl module and store it in a mysql table right away). I have looked into memcpy, but it doesn't seem to do what I want. This is what I am trying right now:

    Code:
    short some_func() { ... }
    unsigned some_other_func() { ... }
    int n = 128;
    
    char * return_func() {
      char * result = malloc( sizeof( short ) * 2 + sizeof( unsigned ) * n );
      int pos = 0;
      short s = some_func();
      memcpy( &result[pos], &s, sizeof(short) );
      pos += sizeof(short);
      s = some_func();
      memcpy( &result[pos], &s, sizeof(short) );
      pos += sizeof(short);
    
      int i;
      for( i=0; i<n; i++ ) {
        unsigned u = some_other_func();
        memcpy( &result[pos], &u, sizeof( unsigned ) );
        pos += sizeof( unsigned );
      }
    
      return result;
    }
    Is there a better way to do this, without memcpy?
    Or should i call memcpy in a different way?
    Google couldn't help me out.

    Thanks in advance.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Bats
    I'm writing an XSUB to glue an existing C module to my perl code, but I'm kind of new to C.
    I want to save 2 shorts and n unsigneds in a char * array (so I can return the array to the perl module and store it in a mysql table right away). I have looked into memcpy, but it doesn't seem to do what I want. This is what I am trying right now
    What doesn't it do what you want? Or what does it do that you don't want? Your program fragment seems ok to me.

    kind regards,

    Jos

    Comment

    • Bats
      New Member
      • Aug 2009
      • 2

      #3
      if I add this before the return:

      Code:
        int len = strlen(result);
        printf( "len: %d\n", len );
      I see
      Code:
      len: 2

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        What does XSUB expect in this string to interpret is an array of numbers?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Bats
          if I add this before the return:

          Code:
            int len = strlen(result);
            printf( "len: %d\n", len );
          I see
          Code:
          len: 2
          The result is not a string because you have copied two shorts to the start of the buffer; if one byte of those shorts contains a nul byte it will be considered the end of the 'string'.

          kind regards,

          Jos

          Comment

          Working...