void pointer cast segfaults



I am trying to understand the behavior of void pointers. Can someone
explain to me why I get a segfault for the following program?

#include <stdio.h>

void* plusone(void* i){
int* arg = (int*)i;
int result = (*arg + 1);
return (void*)result;
}
int main(){
void* w1 = (void*)10;
void* result = plusone(w1);
printf("%d", *(int*)result);
}

According to gdb, the cast in the first line of plusone gives the segfault.
Why??

Markus

P.S.: I compiled with gcc 3.2.2
.



Relevant Pages

  • Re: C dangerous?
    ... > The following program may cause hardware damage, ... > void main ... I think this program would only segfault on an *operating* system... ...
    (comp.lang.c)
  • Re: Code Review...
    ... Since char* and void* are required to have the same representation, ... >If your O/S managed not to segfault then you'll see lots of crap in your ...
    (comp.lang.c)
  • Newbie question: Why does this work?
    ... This is most certainly a total newbie question, ... void insertion_sort(int a, int length) ... IMHO the segfault should occur at line 9 as on the first pass through ...
    (comp.lang.c)
  • [PATCH 2/3] s390: Replace driver_data with dev_[gs]et_drvdata
    ... More majordomo info at http://vger.kernel.org/majordomo-info.html ... Please read the FAQ at http://www.tux.org/lkml/ ... Prev by Date: ...
    (Linux-Kernel)
  • Re: [rfc][patch] mm: madvise(WILLNEED) for anonymous memory
    ... unsigned long end, const struct mm_walk *walk, ... *prev = vma; ... const struct mm_walk *walk, void *private) ...
    (Linux-Kernel)