|
|
 |
 |
 |
 |
free or not pointer to char returned
Hi, Some functions, for example ctime of time.h, return a pointer to char. Is it my job to free it or not? How could I know if it's a chunk of memory allocated with malloc or a tab? Thanks for your reply, -AJ
Antoine Junod <t @tots-ns.net> writes: > Some functions, for example ctime of time.h, return a pointer to > char. Is it my job to free it or not? How could I know if it's a chunk > of memory allocated with malloc or a tab? (A "tab"?) It depends on the function. In the case of ctime(), it returns the address of a static object. -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson <k @mib.org> writes: > Antoine Junod <t @tots-ns.net> writes: > > Some functions, for example ctime of time.h, return a pointer to > > char. Is it my job to free it or not? How could I know if it's a chunk > > of memory allocated with malloc or a tab? > (A "tab"?)
array, sorry. > It depends on the function. > In the case of ctime(), it returns the address of a static object.
Ok thanks. I didn't read the man page carefully enough :) -AJ
Antoine Junod wrote: > Keith Thompson <k @mib.org> writes: >> Antoine Junod <t@tots-ns.net> writes: >>> Some functions, for example ctime of time.h, return a pointer to >>> char. Is it my job to free it or not? How could I know if it's a chunk >>> of memory allocated with malloc or a tab? >> (A "tab"?) > array, sorry. >> It depends on the function. >> In the case of ctime(), it returns the address of a static object. > Ok thanks. I didn't read the man page carefully enough :)
The only Standard library functions that require you to decide when to free allocated memory are malloc(), calloc(), and realloc(). Other Standard library functions that return pointers to "library objects" -- fopen(), for example -- take care of their own memory management, and you must not free the pointers they return. For non-Standard functions, read the documentation. -- Eric Sosman esos@acm-dot-org.invalid
Antoine Junod <t @tots-ns.net> wrote: > Ok thanks. I didn't read the man page carefully enough :) Just as an aside, man pages alone are not always sufficient to discern the details concerning function X. Case in point being certain man pages that editorialize against using strtok(), which can be a perfectly reasonable tool choice. -- C. Benson Manica | I *should* know what I'm talking about - if I cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Christopher Benson-Manica <a @otaku.freeshell.org> writes: > Just as an aside, man pages alone are not always sufficient to discern > the details concerning function X. Case in point being certain man > pages that editorialize against using strtok(), which can be a > perfectly reasonable tool choice. It can be. But strtok() has at least these problems: * It merges adjacent delimiters. If you use a comma as your delimiter, then "a,,b,c" will be divided into three tokens, not four. This is often the wrong thing to do. In fact, it is only the right thing to do, in my experience, when the delimiter set contains white space (for dividing a string into "words") or it is known in advance that there will be no adjacent delimiters. * The identity of the delimiter is lost, because it is changed to a null terminator. * It modifies the string that it tokenizes. This is bad because it forces you to make a copy of the string if you want to use it later. It also means that you can't tokenize a string literal with it; this is not necessarily something you'd want to do all the time but it is surprising. * It can only be used once at a time. If a sequence of strtok() calls is ongoing and another one is started, the state of the first one is lost. This isn't a problem for small programs but it is easy to lose track of such things in hierarchies of nested functions in large programs. In other words, strtok() breaks encapsulation. -- Comp-sci PhD expected before end of 2007 Seeking industrial or academic position *outside California* in 2008
Ben Pfaff <b @cs.stanford.edu> wrote: > It can be. But strtok() has at least these problems: > (snip) Yes, strtok() has more than its share of idiosyncrancies, meaning that it isn't as widely applicable as it could be. But as Brian Rodenborn said four years ago (in response to a naive question ultimately posed by me, incidentally - I'm enough embarrassed by my inexperience at the time to not want to post the link), "The prohibitions on the use of strtok() can be somewhat hysterical at times." The lesson to OP is that when man strtok tells you to "never use this function", it's not wise to treat it as gospel truth. -- C. Benson Manica | I *should* know what I'm talking about - if I cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Christopher Benson-Manica wrote: > Ben Pfaff <b @cs.stanford.edu> wrote: >> It can be. But strtok() has at least these problems: >> (snip) > Yes, strtok() has more than its share of idiosyncrancies, meaning > that it isn't as widely applicable as it could be. But as Brian > Rodenborn said four years ago (in response to a naive question > ultimately posed by me, incidentally - I'm enough embarrassed by > my inexperience at the time to not want to post the link), "The > prohibitions on the use of strtok() can be somewhat hysterical at > times." The lesson to OP is that when man strtok tells you to > "never use this function", it's not wise to treat it as gospel > truth.
So try this one: /* ------- file toksplit.c ----------*/ #include "toksplit.h" /* copy over the next token from an input string, after skipping leading blanks (or other whitespace?). The token is terminated by the first appearance of tokchar, or by the end of the source string. The caller must supply sufficient space in token to receive any token, Otherwise tokens will be truncated. Returns: a pointer past the terminating tokchar. This will happily return an infinity of empty tokens if called with src pointing to the end of a string. Tokens will never include a copy of tokchar. A better name would be "strtkn", except that is reserved for the system namespace. Change to that at your risk. released to Public Domain, by C.B. Falconer. Published 2006-02-20. Attribution appreciated. Revised 2006-06-13 */ const char *toksplit(const char *src, /* Source of tokens */ char tokchar, /* token delimiting char */ char *token, /* receiver of parsed token */ size_t lgh) /* length token can receive */ /* not including final '\0' */ { if (src) { while (' ' == *src) src++; while (*src && (tokchar != *src)) { if (lgh) { *token++ = *src; --lgh; } src++; } if (*src && (tokchar == *src)) src++; } *token = '\0'; return src; } /* toksplit */
#ifdef TESTING #include <stdio.h> #define ABRsize 6 /* length of acceptable token abbreviations */ /* ---------------- */ static void showtoken(int i, char *tok) { putchar(i + '1'); putchar(':'); puts(tok); } /* showtoken */
/* ---------------- */ int main(void) { char teststring[] = "This is a test, ,, abbrev, more"; const char *t, *s = teststring; int i; char token[ABRsize + 1]; puts(teststring); t = s; for (i = 0; i < 4; i++) { t = toksplit(t, ',', token, ABRsize); showtoken(i, token); } puts("\nHow to detect 'no more tokens' while truncating"); t = s; i = 0; while (*t) { t = toksplit(t, ',', token, 3); showtoken(i, token); i++; } puts("\nUsing blanks as token delimiters"); t = s; i = 0; while (*t) { t = toksplit(t, ' ', token, ABRsize); showtoken(i, token); i++; } return 0; } /* main */
#endif /* ------- end file toksplit.c ----------*/ /* ------- file toksplit.h ----------*/ #ifndef H_toksplit_h # define H_toksplit_h # ifdef __cplusplus extern "C" { # endif #include <stddef.h> /* copy over the next token from an input string, after skipping leading blanks (or other whitespace?). The token is terminated by the first appearance of tokchar, or by the end of the source string. The caller must supply sufficient space in token to receive any token, Otherwise tokens will be truncated. Returns: a pointer past the terminating tokchar. This will happily return an infinity of empty tokens if called with src pointing to the end of a string. Tokens will never include a copy of tokchar. released to Public Domain, by C.B. Falconer. Published 2006-02-20. Attribution appreciated. */ const char *toksplit(const char *src, /* Source of tokens */ char tokchar, /* token delimiting char */ char *token, /* receiver of parsed token */ size_t lgh); /* length token can receive */ /* not including final '\0' */ # ifdef __cplusplus } # endif #endif /* ------- end file toksplit.h ----------*/ -- <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <http://www.securityfocus.com/columnists/423> <http://www.aaxnet.com/editor/edit043.html> <http://kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com
CBFalconer wrote On 05/24/07 21:59,: > /* ------- file toksplit.c ----------*/ > #include "toksplit.h" > [...] > A better name would be "strtkn", except that is reserved > for the system namespace. Change to that at your risk. > [...] > const char *toksplit(const char *src, /* Source of tokens */ > char tokchar, /* token delimiting char */ > char *token, /* receiver of parsed token */ > size_t lgh) /* length token can receive */ > /* not including final '\0' */
Um, er, out of frying pan and into fire. `toksplit' with external linkage is a reserved identifier (7.26.2). I suggest renaming the function to `noalias' ;-) -- Eric.Sos@sun.com
Eric Sosman <Eric.Sos @sun.com> writes: [...] > Um, er, out of frying pan and into fire. `toksplit' > with external linkage is a reserved identifier (7.26.2). > I suggest renaming the function to `noalias' ;-)
That joke must go. This is non-negotiable. -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Eric Sosman wrote: > CBFalconer wrote On 05/24/07 21:59,: >> /* ------- file toksplit.c ----------*/ >> #include "toksplit.h" >> [...] >> A better name would be "strtkn", except that is reserved >> for the system namespace. Change to that at your risk. >> [...] >> const char *toksplit(const char *src, /* Source of tokens */ >> char tokchar, /* token delimiting char */ >> char *token, /* receiver of parsed token */ >> size_t lgh) /* length token can receive */ >> /* not including final '\0' */ > Um, er, out of frying pan and into fire. `toksplit' > with external linkage is a reserved identifier (7.26.2). > I suggest renaming the function to `noalias' ;-)
I see no such restricion in N869. Can you be more explicit? -- <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <http://www.securityfocus.com/columnists/423> <http://www.aaxnet.com/editor/edit043.html> <http://kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com
CBFalconer wrote: > Eric Sosman wrote: >> CBFalconer wrote On 05/24/07 21:59,: >>> /* ------- file toksplit.c ----------*/ >>> #include "toksplit.h" >>> [...] >>> A better name would be "strtkn", except that is reserved >>> for the system namespace. Change to that at your risk. >>> [...] >>> const char *toksplit(const char *src, /* Source of tokens */ >>> char tokchar, /* token delimiting char */ >>> char *token, /* receiver of parsed token */ >>> size_t lgh) /* length token can receive */ >>> /* not including final '\0' */ >> Um, er, out of frying pan and into fire. `toksplit' >> with external linkage is a reserved identifier (7.26.2). >> I suggest renaming the function to `noalias' ;-) > I see no such restricion in N869. Can you be more explicit?
"to" followed by a lowercase letter means toksplit is technically reserved for current implementations and future standards as a function declared in <ctype.h> (see 7.26.2).
Harald van D?k wrote: > CBFalconer wrote: >> Eric Sosman wrote: >>> CBFalconer wrote On 05/24/07 21:59,: >>>> /* ------- file toksplit.c ----------*/ >>>> #include "toksplit.h" >>>> [...] >>>> A better name would be "strtkn", except that is reserved >>>> for the system namespace. Change to that at your risk. >>>> [...] >>>> const char *toksplit(const char *src, /* Source of tokens */ >>>> char tokchar, /* token delimiting char */ >>>> char *token, /* receiver of parsed token */ >>>> size_t lgh) /* length token can receive */ >>>> /* not including final '\0' */ >>> Um, er, out of frying pan and into fire. `toksplit' >>> with external linkage is a reserved identifier (7.26.2). >>> I suggest renaming the function to `noalias' ;-) >> I see no such restricion in N869. Can you be more explicit? > "to" followed by a lowercase letter means toksplit is technically > reserved for current implementations and future standards as a > function declared in <ctype.h> (see 7.26.2).
Ulp. Time to rename to "tknsplit". -- <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <http://www.securityfocus.com/columnists/423> <http://www.aaxnet.com/editor/edit043.html> <http://kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com
CBFalconer wrote: > Harald van D?k wrote: >> "to" followed by a lowercase letter means toksplit is technically >> reserved for current implementations and future standards as a >> function declared in <ctype.h> (see 7.26.2). > Ulp. Time to rename to "tknsplit".
Or `splitToken` [or `split_token`, depending on rainfall]. -- More Rain Here Hedgehog "No-one here is exactly what he appears." G'kar, /Babylon 5/
Chris Dollin wrote: > CBFalconer wrote: >> Harald van D?k wrote: >>> "to" followed by a lowercase letter means toksplit is technically >>> reserved for current implementations and future standards as a >>> function declared in <ctype.h> (see 7.26.2). >> Ulp. Time to rename to "tknsplit". > Or `splitToken` [or `split_token`, depending on rainfall].
I hate underscores. They require finding a key, and a shift! -- <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <http://www.securityfocus.com/columnists/423> <http://www.aaxnet.com/editor/edit043.html> <http://kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com
On Sun, 27 May 2007 09:05:09 -0400, CBFalconer <cbfalco @yahoo.com> wrote: >Chris Dollin wrote: >>CBFalconer wrote: >>>Harald van D?k wrote: >>>> "to" followed by a lowercase letter means toksplit is technically >>>> reserved for current implementations and future standards as a >>>> function declared in <ctype.h> (see 7.26.2). >>> Ulp. Time to rename to "tknsplit". >> Or `splitToken` [or `split_token`, depending on rainfall]. > I hate underscores. They require finding a key, and a shift!
But but, they look so "niiiiiiice!" (Borat mode) :)
CBFalconer wrote On 05/25/07 12:09,:
> Eric Sosman wrote: >>CBFalconer wrote On 05/24/07 21:59,: >>>/* ------- file toksplit.c ----------*/ >>>#include "toksplit.h" >>>[...] >>> A better name would be "strtkn", except that is reserved >>> for the system namespace. Change to that at your risk. >>>[...] >>>const char *toksplit(const char *src, /* Source of tokens */ >>> char tokchar, /* token delimiting char */ >>> char *token, /* receiver of parsed token */ >>> size_t lgh) /* length token can receive */ >>> /* not including final '\0' */ >> Um, er, out of frying pan and into fire. `toksplit' >>with external linkage is a reserved identifier (7.26.2). >> I suggest renaming the function to `noalias' ;-) > I see no such restricion in N869. Can you be more explicit?
WG14/N869: 7.26 Future library directions 1) [...] All external names described below are reserved no matter what headers are included by the program. 7.26.2 Character handling <ctype.h> 1) Function names that begin with either *is* or *to*, and a lowercase letter (possibly followed by any combination of digits, letters, and underscore) may be added to the declarations in the <ctype.h> header. 7.1.2 Standard headers 6) Any declaration of a library function shall have external linkage. ISO/IEC 9899:TC2 omits the parenthesized piece of 7.26.2, but is otherwise the same. -- Eric.Sos@sun.com
CBFalconer wrote: > Chris Dollin wrote: >> CBFalconer wrote: >>> Harald van D?k wrote: >>>> "to" followed by a lowercase letter means toksplit is technically >>>> reserved for current implementations and future standards as a >>>> function declared in <ctype.h> (see 7.26.2). >>> Ulp. Time to rename to "tknsplit". >> Or `splitToken` [or `split_token`, depending on rainfall]. > I hate underscores. They require finding a key, and a shift!
But underscores in the middle of a variable name as a break character... makes the name much easier to read. It's like other types of documentation: perhaps a pain to write but a great aid to reading and understanding the program code. -- +----------------------------------------------------------------+ | Charles and Francis Richmond richmond at plano dot net | +----------------------------------------------------------------+
|
 |
 |
 |
 |
|