[freetds] tdsstring.c ansi alias problem

ZIGLIO Frediano Frediano.Ziglio at vodafoneomnitel.it
Mon May 5 11:54:54 EDT 2003


> >
> > D0:[CRAIG.FREETDS-0_62_DEV_20030503.SRC.TDS]TDSSTRING.C;1, 
> > which uses the type "pointer to struct DSTR_CHAR".  
> ...
> > 
> > DSTR
> > tds_dstr_set(DSTR * s, char *src)
> > {
> >         if (*(char **) s != tds_str_empty)
> >                 free(*(char **) s);
> >         *(char **) s = src;
> >         return *s;
> > }
> > 
> > 
> > I guess I don't understand the purpose of casting the 
> > left-hand side of "*(char **) s = src;" 
> 
> Where is DSTR_CHAR defined?  I find only a forward 
> declaration in tds.h.
> I've looked in unixodbc and on my Win32 box:
> 
> $ grep DSTR_CHAR include/* src/*/*.c win32/*
> include/tds.h:struct DSTR_CHAR;
> include/tds.h:typedef struct DSTR_CHAR *DSTR;
> 

Yes, it's true. It's an undefined structure. I use a pointer to undefined structure to be more type safe using C...

> I can't even explain how the above compiles.  That doesn't 
> prevent me from
> thinking it's ugly, and afaik "free(*(char **) s);" is identical to
> "free(s);" in all cases, as far as the compiler and free store are
> concerned.  
> 

Yes and not. 
You can do a "free(*(char **) s);" but doing so you can get a core cause some string (empty ones) point to a static buffer and not to an allocated buffer... Also in such way I can extend dstring in future (like adding lenght and buffer size) without breaking source compatibility.

> There must be a better way to do this than casting the living 
> daylights out
> of it....
> 

I did this to avoid free(dstring) or other wrong usage.
If "*(char **) s = src;" is not OK perhaps it's a priority problem... *((char**)s) = src should work better.

freddy77



More information about the FreeTDS mailing list