L
Linny
Hi All,
I am pasting a piece of code which executes fine on 32 bit system but
fails with a segmentation fault when compiled 64 bit compiler.I am
using a HP-UX C compiler on PA-RISC system. This code was picked up
from a document mentioning portability issues from 32 to 64 bit
systems.
But when I include the system file <malloc.h> , the code executes fine
on both the systems.
#include <stdio.h>
int main(int argc, char **argv)
{
char mystring1[10] = "foo";
char *mystring2;
mystring2 = (char *)malloc(sizeof(char)*(long)10);
strcpy(mystring2, "bar\n\0");
printf("%s%s", mystring1, mystring2);
return 0;
}
Output on 32 bit
foo bar
I'll tried to debug it but was unable to target the cause. pasting a
clip of the gdb output.
Program received signal SIGSEGV, Segmentation fault.
0x800003ffff743ac8 in strlen+0x10 () from /usr/lib/pa20_64/libc.2
(gdb) bt
#0 0x800003ffff743ac8 in strlen+0x10 () from /usr/lib/pa20_64/libc.2
#1 0x4000000000001f08 in main (argc=1, argv=0x800003ffff7f07a0) at
test1.c:8
I assume it is to do with the malloc argument 10 which is typecasted to
long but unable to reason out. Can anyone explain the details?
I am pasting a piece of code which executes fine on 32 bit system but
fails with a segmentation fault when compiled 64 bit compiler.I am
using a HP-UX C compiler on PA-RISC system. This code was picked up
from a document mentioning portability issues from 32 to 64 bit
systems.
But when I include the system file <malloc.h> , the code executes fine
on both the systems.
#include <stdio.h>
int main(int argc, char **argv)
{
char mystring1[10] = "foo";
char *mystring2;
mystring2 = (char *)malloc(sizeof(char)*(long)10);
strcpy(mystring2, "bar\n\0");
printf("%s%s", mystring1, mystring2);
return 0;
}
Output on 32 bit
foo bar
I'll tried to debug it but was unable to target the cause. pasting a
clip of the gdb output.
Program received signal SIGSEGV, Segmentation fault.
0x800003ffff743ac8 in strlen+0x10 () from /usr/lib/pa20_64/libc.2
(gdb) bt
#0 0x800003ffff743ac8 in strlen+0x10 () from /usr/lib/pa20_64/libc.2
#1 0x4000000000001f08 in main (argc=1, argv=0x800003ffff7f07a0) at
test1.c:8
I assume it is to do with the malloc argument 10 which is typecasted to
long but unable to reason out. Can anyone explain the details?