Reputation: 59
I found similar questions to mine, but I still can't figure out why my code is acting the way it is.
I have a struct called "rectangle", and here is the code that I am trying to run:
Rectangle *newRect = (Rectangle *)malloc(sizeof(Rectangle));
Rectangle *newRect2 = (Rectangle *)malloc(sizeof(Rectangle));
printf("rect1: %p rect2: %p",newRect,newRect2);
It outputs the same address for both of them, what am I doing wrong?
Thanks!
Upvotes: 2
Views: 2026
Reputation: 78963
You probably don't compile with all warnings on, and forgot to include stdlib.h
. malloc
is then by some compilers interpreted to return int
instead of void*
. On a 64 bit architecture this looses significant information and thus at the end you see the same value in your print.
Don't cast the return of malloc
.
Upvotes: 2