Tytuł prawie mówi wszystko, ale powtórzę pytanie ...Czy następujący program jest ściśle zgodnym programem C99?
Czy poniższy program to program ściśle zgodny z normą C99?
#include <stdlib.h>
/* Removing any pre-existing macro definition, in case one should exist in the implementation.
* Seems to be allowed under 7.1.3 para 3, as malloc does not begin with _X where X is any capital letter.
* And 7.1.4 para 1 explicitly permits #undef of such macros.
*/
#ifdef malloc
#undef malloc
#endif
/* Macro substitution has no impact on the external name malloc
* which remains accessible, e.g., via "(malloc)(s)". Such use of
* macro substitution seems enabled by 7.1.4 para 1, but not specifically
* mentioned otherwise.
*/
void * journalling_malloc(size_t size);
#define malloc(s) ((journalling_malloc)(s))
int main(void)
{
return malloc(10) == NULL ? 1 : 0;
/* Just for the sake of expanding the
* macro one time, return as exit code
* whether the allocation was performed.
*/
}
BTW, jestem świadomy, że jeden chciałby również "wrap" free(), realloc(), strdup(), calloc() i who-knows-what-else. To nie jest pytanie. Na marginesie: kto wie co jeszcze? To są wszystkie funkcje API malloc(), które mogę wyczarować z mojej głowy. –
Wystarczy wspomnieć, że ta metoda nie pomoże w używaniu malloc w żadnej zewnętrznej bibliotece. Kilka osób zapewnia alternatywne implementacje malloc, które można dodawać w czasie łącza, na przykład google pod adresem http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools – gnud