Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Aug 2009
Posts: 2
#1: Aug 13 '09
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:

Expand|Select|Wrap|Line Numbers
  1. short some_func() { ... }
  2. unsigned some_other_func() { ... }
  3. int n = 128;
  4.  
  5. char * return_func() {
  6.   char * result = malloc( sizeof( short ) * 2 + sizeof( unsigned ) * n );
  7.   int pos = 0;
  8.   short s = some_func();
  9.   memcpy( &result[pos], &s, sizeof(short) );
  10.   pos += sizeof(short);
  11.   s = some_func();
  12.   memcpy( &result[pos], &s, sizeof(short) );
  13.   pos += sizeof(short);
  14.  
  15.   int i;
  16.   for( i=0; i<n; i++ ) {
  17.     unsigned u = some_other_func();
  18.     memcpy( &result[pos], &u, sizeof( unsigned ) );
  19.     pos += sizeof( unsigned );
  20.   }
  21.  
  22.   return result;
  23. }
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's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 13 '09

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


Quote:

Originally Posted by Bats View Post

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
Newbie
 
Join Date: Aug 2009
Posts: 2
#3: Aug 13 '09

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


if I add this before the return:

Expand|Select|Wrap|Line Numbers
  1.   int len = strlen(result);
  2.   printf( "len: %d\n", len );
I see
Expand|Select|Wrap|Line Numbers
  1. len: 2
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#4: Aug 13 '09

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


What does XSUB expect in this string to interpret is an array of numbers?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Aug 13 '09

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


Quote:

Originally Posted by Bats View Post

if I add this before the return:

Expand|Select|Wrap|Line Numbers
  1.   int len = strlen(result);
  2.   printf( "len: %d\n", len );
I see
Expand|Select|Wrap|Line Numbers
  1. 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
Reply