The following C program segfaults of IA-64, but works fine on IA-32.
Code:int main() { int* p; p = (int*)malloc(sizeof(int)); *p = 10; return 0; } Why does it happen so?
The following C program segfaults of IA-64, but works fine on IA-32.
Code:int main() { int* p; p = (int*)malloc(sizeof(int)); *p = 10; return 0; } Why does it happen so?
you don't have to cast malloc explicitly since p is declared as an int pointer I believe the cast is implicit. (I could be wrong on this).
However your error is because you set *p = 10. You are setting the address of p to 10. You can't do that.
I suggest trying with a more correct example:
Code:#include <stdlib.h> int main() { int* p = malloc(sizeof(*p)); if (p) { *p = 10; } free(p); return 0; }No, that assigns the value of 10 to the int pointed to by p, so it is perfectly fine, unless the pointer is a null pointer.Originally Posted by Bladactania
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No, "*p = 10" writes 10 to the memory block pointed to by 10.
I don't see anything wrong with it (and it does work on my 64-bit Linux).
BTW, I think you meant x86-64 instead of IA-64 (Intel Itanium).
How are you running it? Command line or through an IDE? Are you sure you are running the code? (did the program build successfully?)
Never use malloc() without checking its return value.
But you are right. malloc() returns void*, which is supposed to implicitly cast to any other pointer type.you don't have to cast malloc explicitly since p is declared as an int pointer I believe the cast is implicit. (I could be wrong on this).
laserlight's call to malloc() is perfect, since it doesn't make any assumptions about the type of p by making clever use of sizeof.
Greets,
Philip
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
void* will implicitly cast to any pointer type, provided the compiler knows you have a void* to begin with; if you do not #include <stdlib.h>, then the compiler has no idea that a void* is involved (since it will assume an implicit int return). Presumably a pointer is different sizes on a 32-bit machine and a 64-bit machine, hence the "not always working" aspect of your code.
Perhaps the same reason given here
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
No matter what compiler is beign used - if there is no stdlib include, malloc will be seen to return an integer - 32-bits. The cast will make sure the compiler is still happy to convert an integer to a pointer, and will do so by sign-extending the 32-bit value to 64-bit. It will thus give a invalid memory address [unless the first or last 2GB of virtual space is accessible, and if I remember rightly, most Linux distributions explicitly make those spaces UNAVAILABLE for this very reason - catching pointers that have "lost the upper 32 bits"]
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.