comp.lang.c
[Top] [All Lists]

Word alignment - Why doesn't this crash?

Subject: Word alignment - Why doesn't this crash?
From:
Date: Mon, 31 Mar 2008 03:46:03 -0700 PDT
Newsgroups: comp.lang.c

Hi,

I try to confirm my understanding of word alignment by writing a
program that screw this particular matter up.

I run linux on:
Intel(R) Core(TM)2 CPU         T7600  @ 2.33GHz

my compiler is:
gcc version 4.1.2

I compile with no arguments other than source file name and target
name (-o ptr_test).

I write this program but it doesn't crash. Is it because I'm testing
the wrong thing (my understanding is wrong) or is it because my
environment is very forgiving?

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

int main( int argc, char** argv )
{
  char* ptr = malloc( 32 );

  ptr++;

  *(int*)ptr = 20;

  printf("ptr: %d\n", *ptr );

  *(int*)ptr = *(int*)ptr + 20;


  printf("ptr: %d\n", *ptr );

  return(0);
}


Thanks
/Sune

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy