Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
supplementary C frequent answers
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Peter Pichler, post: 1694788"] Yes, but... malloc() returns an address properly aligned for any type. In other words, even if you cast void* to any wider type*, thus losing the lowest bits of the address, it's no real loss because those bits must have been zeros anyway. You have to cast again b efore assigning to a pointer, otherwise it would not compile at all. NB: I understand that we are talking about cases like double *p = (int*)malloc(sizeof *p); This would not compile, unless your compiler is seriously broken. To make it compile, you would have to cast again, e.g. double *p = (double*)(int*)malloc(sizeof *p); I cannot see any possible problems with that, other than it is UGLY, provided that stdlib.h is #included. The problems that Ben encountered may have been something like double *p = (double*)malloc(sizeof(int)); Because *p used to be int*, but changed to double*, and the programmer failed to notice that (s)he had to change the parameter as well. This may even work by accident, if the implementation allocates memory in segments, making such bugs even more difficult to spot. Peter Pichler [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
supplementary C frequent answers
Top