Hello.
I am sorry for all the confusion around this. In C you can do pretty much anything. But the char * construct, I think they did it like that deliberately. I oppose to this, because a showel is a showel not matter what you call it, on the other hand, there are some subtle differences somewhere, which I can’t remember, nor are willing to look up in the two annoted copies of the C-Standard I have.
Just lets agree upon that Objective-C with its id, and isa, runtime and reflection is so much more pleasant to deal with, comparing to whatever. And with strongly typed languages, though it is weakly typed in its underpinnings, the likelihood of getting into trouble like those rants has been about, are far less.
I like C, but I find it hard to write something bigger than say 5000 lines with it as you have to figure out your own encapsulating principles and such, and know where you broke them. Key to success with C is to have the modules form an acyclic graph more or less (To avoid too much spaghetti code).
Here are some routines I use in C for dealing with memory that can be useful.
[code]void *
yrealloc( void *ptr, size_t sz, const char *fromHandler, const char * forVariable )
{
void *p = realloc(ptr, sz ) ;
if (p == NULL ) {
yerror( YREALLOC_ERR, fromHandler, forVariable, YX_EXTERNAL_CAUSE ) ;
}
return p ;
}
/* allocates memory.
if the request for memory can’t be satisified
then depending on whether we are in the ui or not,
a failure message will be printed there.
A failure message will be printed at exit anyway.
*/
void *
ymalloc( size_t sz,const char *fromHandler, const char *forVariable )
{
void *p = malloc(sz ) ;
if (p == NULL ) {
yerror( YMALLOC_ERR, fromHandler, forVariable, YX_EXTERNAL_CAUSE ) ;
}
return p ;
}[/code]
The yerror formats a string with the handler using a string from an error strings table (YMALLOC_ERR and YREALLOC_ERR) , and the variable, before it prints it to stderr, maybe prints it on screen first, if a screen is active. Then a global “cleanup” routine is called before we exit.(YX_EXTERNAL_CAUSE=1) So we kind of sidestep the normal flow, but cleans up when we are done with the fatal error.
This is an idiom that works very well. You get proper error handling while your code reads like a text book example. I use y as prefix since everybody else uses x. And hahaha, I have included stdlib.h and mem.h in the file where those are declared, so I doesn’t fall into the previously described trap even if I don’t cast malloc and realloc internally in the routines.