UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Divide 64bit interger type into 32bit integers

#1 2009-02-04 01:15 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Divide 64bit interger type into 32bit integers

Hello!

Can someone tell me why this code doesn't work?

#include <stdio.h>
#include <string.h>

int main (void) {
    long long int a = 2111222333444555666;
    long long int *ptr_a = &a;
    long long int x;
    long long int *ptr_x = &x;
    int b;
    int c;
    
    printf ("%lld, %d\n", a, sizeof (long long));
    
    memmove (&b, ptr_a, 4);
    memmove (&c, ptr_a+4, 4);
    
    printf ("b: %d, c: %d\n", b, c);
    
    memmove (ptr_x, &b, 4);
    memmove (ptr_x+4, &c, 4);
    
    printf ("%lld, %d\n", x, sizeof (long long int));
    
    return 0;
}

Offline

#2 2009-02-04 02:12 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Divide 64bit interger type into 32bit integers

memmove (&c, ptr_a+4, 4);
...
    memmove (ptr_x+4, &c, 4);

It's pointer arithmetic biting you...  When you do "ptr + someval", it means to actually
increment the pointer value by "someval * sizeof (*ptr)"...  So, in this case, you're
incrementing those pointer by "4 * sizeof (long long)", ie. 32...  So, you're pointing way
out of range into random crap off your stack...  You want to make those pointers be
simple "char *" pointers, to get a simple one-to-one incrementation in your pointer
arithmetic...

Offline

#3 2009-02-04 03:16 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Divide 64bit interger type into 32bit integers

Ok, I changed 'ptr_x' and 'ptr_a' to 32bit integer pointers and also fixed memory copy instructions to:

memmove (&c, ptr_a+1, 4);
...
memmove (ptr_x+1, &c, 4);

Now code works fine too.

Offline

#4 2009-02-04 03:57 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Divide 64bit interger type into 32bit integers

Btw, memcpy() will be slightly more efficient than memmove()... memmove() is only necessary if the source and destination regions of memory are overlapping.

Offline

#5 2009-02-05 07:36 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: Divide 64bit interger type into 32bit integers

why not just use a union to do the job? With one long long int and an array of 2 ints? I am quite sure I once used sth like that a couple of years ago.

Offline

#6 2009-02-05 04:01 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Divide 64bit interger type into 32bit integers

Offline

#7 2009-02-06 09:07 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: Divide 64bit interger type into 32bit integers

:) And it's zero copy ....

Offline

  • Index
  • » C
  • » Divide 64bit interger type into 32bit integers

Board footer

Powered by FluxBB