Vimal said:
it should really be int main(void)
As Ravi noted, remove the cast. And make sure you have included stdlib.h.
Also, check if malloc has succeeded before using it.
In addition, without the cast the compiler was *required* to complain at
you because int and pointers are not assignment compatible. If param put
in the cast to shut the compiler up then it just goes to show *why* you
should never put in a cast to shut the compiler up, you only put in
casts (or make any other change) when you understand *why* the compiler
complained and exactly what the effect will be.
The generally preferred form for calling malloc around here is
p = malloc(N * sizeof *p);
where N is the number of items you want space for. So when, as in this
case, you want space for one item you can simplify it to
p = malloc(sizeof *p);
Far less typing, the compiler is *required* to complain if you forget to
include stdlib.h (unless you have done something else really stupid) and
easier to maintain since it does not need changing if you have to change
p from int* to long* or anything else.
All the advice from Ravi and Vimal was correct, although they have not
explained why, so I'll add that information.
The ANSI standard for C that was release in 1989 specified that if the
compiler has not seen a declaration for a function (or if it saw a
declaration that did not specify a return type) that it should assume
that the function returned an int. It also specified that it was
undefined behaviour (i.e. *anything* can happen) if this assumption was
made but the function actually returned something else.
On your 32 bit platform int and void* are both 32 bits and the same
method is being used to return either type so it just happens to do what
you expect.
On your 64 bit platform you probably have int as 32 bits and void* as 64
bits. So malloc returns a 64 bit pointer, but when main was compiled the
compiler assumed it would be getting a 32 bit int, so it only grabs half
of the address and then converts that in to a pointer which obviously
produces a garbage value. E.g. malloc returns a pointer with the bit
pattern of 0x1122334455667788 but main only "sees" 0x11223344 and
converts that to address 0x0000000011223344 which you are not allowed to
access.
It can also fail for other reasons on platforms where int and void* are
the same size, for example if addresses are returned in an address
register and integers in a data register, which does happen on some systems.
The morals of the story are *always* include the correct headers to
provide prototypes for the functions you will be using otherwise
anything might happen and don't go adding casts just to shut the
compiler up.
PS, Ravi, please put your reply *under* the text you are replying to not
above. It makes it *far* easier for people to follow the thread and is
generally considered here (and in a lot of other places) to be the
polite way to post.