develooper Front page | perl.perl5.porters | Postings from January 2008

Re: C for Perl programmers?

Thread Previous | Thread Next
From:
Marvin Humphrey
Date:
January 18, 2008 19:26
Subject:
Re: C for Perl programmers?
Message ID:
BA753516-2649-4950-A790-6D89B9D57545@rectangular.com

On Jan 18, 2008, at 6:55 PM, Dave Rolsky wrote:

> People always talk about boundary alignments, and other gibberish.

   http://en.wikipedia.org/wiki/Data_structure_alignment

Here's an example of an alignment problem:

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

     int main() {
         int   foo          = 1000;
         char *bar          = (char*)malloc( sizeof(int) + 1 );
         char *bar_plus_one = bar + 1;
         int   baz;

         memcpy(bar_plus_one, &foo, sizeof(int));
         baz = *(int*)bar_plus_one;  /* boom? */

         printf("%d\n", baz);
         return 0;
     }

bar_plus_one isn't word-aligned.  That code will work on some  
systems, but not others.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About