bnpph wrote:stdup is not ISO C and I don't agree with it.
All of string.h's functions are designed where they pass a destination pointer. They also require your destination is large enough to store the result.
If you want to do what you're doing, it would make more sense to create a new type (FILENAME) as typedef of char*, then rename your function "createFilenameFromFile" or something, and then add a "deleteFilename" function. This is how many libraries do it.
Or, you can use C++ and wrap it in a class. That would be even better.
So hang on, you want me to typedef a new type FILENAME, then write a function that does what?
Code: Select all
void deleteFileName(FILENAME file)
{
free(file);
file = NULL;
}
Id prefer not to, I have seen this in some source, but I can do that in the code thx. If this was something that I had to type a lot I would consider it. I get what you are aiming at, I just think that its hardly a good reason.
Here is some Quake3 source, now im not trying to advocate that its the best code in the world, or that their practices are law. Just wanted to point out that people do what you seem to think is a bad practice.
Code: Select all
char *copystring(const char *s)
{
char *b;
b = malloc(strlen(s)+1);
strcpy (b, s);
return b;
}
Code: Select all
/*
================
AllocBspFace
================
*/
bspface_t *AllocBspFace( void ) {
bspface_t *f;
f = malloc(sizeof(*f));
memset( f, 0, sizeof(*f) );
return f;
}
Code: Select all
winding_t *WindingFromDrawSurf( mapDrawSurface_t *ds ) {
winding_t *w;
int i;
w = AllocWinding( ds->numVerts );
w->numpoints = ds->numVerts;
for ( i = 0 ; i < ds->numVerts ; i++ ) {
VectorCopy( ds->verts[i].xyz, w->p[i] );
}
return w;
}
Anyways, this is hardly important enough to worry about it, to each his own.