Why not static const char * const __func__ ?
Author |
Message |
qazm #1 / 16
|
 Why not static const char * const __func__ ?
Why not static const char * const __func__ instead of static const char __func__[] ? This is my actual doubt. I was wrong in my previous post. Sorry for that.
|
Tue, 30 Mar 2004 12:04:52 GMT |
|
 |
Ben Pfaf #2 / 16
|
 Why not static const char * const __func__ ?
Quote:
> Why not > static const char * const __func__ instead of > static const char __func__[] ?
That's a waste of a perfectly good pointer. You still have to allocate the same amount of space for the string, and then you need to allocate space for a pointer, too. I don't *know* that's the reason (the Rationale doesn't seem to say), but that's my suspicion. Quote: > This is my actual doubt. I was wrong in my previous post. Sorry for that.
"Question", please, not "doubt". (I am curious: is your native language something Indian? In my experience a lot of Indians seem to confuse these two words.)
|
Tue, 30 Mar 2004 12:11:17 GMT |
|
 |
Jack Klei #3 / 16
|
 Why not static const char * const __func__ ?
wrote in comp.lang.c: Quote: > Why not > static const char * const __func__ instead of > static const char __func__[] ? > This is my actual doubt. I was wrong in my previous post. Sorry for that.
It would take up more space, and gain nothing. Consider a function named "my_function". static const char __func__[] = "my_function"; ...requires 12 bytes, 11 visible characters and the '\0'. static const char * const __func__ = "my_function"; ...requires 12 bytes plus sizeof(char *), typically four bytes on most modern desk-top systems, eight bytes on some. Since the first form decays to a pointer when you need it to (for example sending it to printf() to display a debug message on the screen), what is the point of wasting the extra sizeof(char *) bytes? -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Tue, 30 Mar 2004 12:22:15 GMT |
|
 |
Kaz Kylhe #4 / 16
|
 Why not static const char * const __func__ ?
Quote:
>Why not >static const char * const __func__ instead of >static const char __func__[] ?
Indeed, why not a constant pointer rather than an array? What's the point? Arrays *are* contant pointers, don'tcha know? *duck*
|
Tue, 30 Mar 2004 12:24:48 GMT |
|
 |
Ben Pfaf #5 / 16
|
 Why not static const char * const __func__ ?
Quote:
> wrote in comp.lang.c: > > Why not > > static const char * const __func__ instead of > > static const char __func__[] ? > It would take up more space, and gain nothing.
Another idea that occurs to me now is that `sizeof array' is more useful than `sizeof pointer'. Of course this is at most a secondary reason.
|
Tue, 30 Mar 2004 12:28:00 GMT |
|
 |
Dave Vandervi #6 / 16
|
 Why not static const char * const __func__ ?
Quote:
>>Why not >>static const char * const __func__ instead of >>static const char __func__[] ? >Indeed, why not a constant pointer rather than an array? >What's the point?
'Cause if it's an array, you can do something like -------- if(sizeof __func__ < 5) fputs("The programmer who wrote this is a moron who doesn't\n" "know how to use decently long function names\n",stderr); -------- Imagine the fun that could be had by putting that in a macro defined in some library's header file. dave --
Caveat: case-insensitive string comparison implementations are so easy to write that there is almost inevitably a bug in the above code. --Richard Heathfield in comp.lang.c
|
Tue, 30 Mar 2004 12:29:26 GMT |
|
 |
Jack Klei #7 / 16
|
 Why not static const char * const __func__ ?
Kylheku) wrote in comp.lang.c: Quote:
> >Why not > >static const char * const __func__ instead of > >static const char __func__[] ? > Indeed, why not a constant pointer rather than an array? > What's the point? Arrays *are* contant pointers, don'tcha know? > *duck*
Well, er, yes, they are under the latest ASKEY standard for C, but they weren't in the pre-standard L&H C (as defined in the book "The C Programming Language, Sort Of", by Laurel & Hardy). -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Tue, 30 Mar 2004 12:56:35 GMT |
|
 |
Greg Come #8 / 16
|
 Why not static const char * const __func__ ?
Quote:
>Why not >static const char * const __func__ instead of >static const char __func__[] ? >This is my actual doubt. I was wrong in my previous post. Sorry for that.
Because the former would need to allocate a pointer and an unnamed string, whereas the latter would need to allocate just a named string. Furthermore, you may want to & it, in which case you want the address of the array, and not the pointer. -- Greg Comeau export ETA: December Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
Tue, 30 Mar 2004 22:10:17 GMT |
|
 |
Greg Come #9 / 16
|
 Why not static const char * const __func__ ?
Quote:
>> wrote in comp.lang.c: >> > Why not >> > static const char * const __func__ instead of >> > static const char __func__[] ? >> It would take up more space, and gain nothing. >Another idea that occurs to me now is that `sizeof array' is more >useful than `sizeof pointer'. Of course this is at most a >secondary reason.
It's not a secondary reason at all. It's fundamental to it IMO. -- Greg Comeau export ETA: December Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
Tue, 30 Mar 2004 22:12:26 GMT |
|
 |
qazm #10 / 16
|
 Why not static const char * const __func__ ?
Quote:
> wrote in comp.lang.c: > > Why not > > static const char * const __func__ instead of > > static const char __func__[] ? > > This is my actual doubt. I was wrong in my previous post. Sorry for that. > It would take up more space, and gain nothing. > Consider a function named "my_function". > static const char __func__[] = "my_function"; > ...requires 12 bytes, 11 visible characters and the '\0'. > static const char * const __func__ = "my_function"; > ...requires 12 bytes plus sizeof(char *), typically four bytes on most > modern desk-top systems, eight bytes on some. > Since the first form decays to a pointer when you need it to (for > example sending it to printf() to display a debug message on the > screen), what is the point of wasting the extra sizeof(char *) bytes?
Thanks a lot! This is a very lucid explanation. Throughout my code I've done like this: - const char* const FIRST = "first" ; So, everywhere I should change them to const char FIRST[] = "first" ; Then, is it true that 'const char* const' should nowhere be used and everywhere, we should change them to 'const char []' ? But, there are lot of books which use 'const char* const' or 'const char* const'. What do you for this?
|
Wed, 31 Mar 2004 10:46:11 GMT |
|
 |
Lawrence Kir #11 / 16
|
 Why not static const char * const __func__ ?
... Quote: >Thanks a lot! This is a very lucid explanation. >Throughout my code I've done like this: - >const char* const FIRST = "first" ; >So, everywhere I should change them to >const char FIRST[] = "first" ;
At block scope (and in some cases also at file scope) this would be better written as static const char FIRST[] = "first"; so that the array can be set up just once at program startup. Other than that it makes sense for most cases. It is possibly for code to depend on the existence of the pointer object but not very likely. Quote: >Then, is it true that 'const char* const' should nowhere be used and >everywhere, we should change them to 'const char []' ?
This isn't correct for all circumstances. For example I could write a qsort() comparison function as int compare(const void *v1, const void *v2) { const char *const p1 = v1; const char *const p2 = v2; return strcmp(p1, p2); Quote: }
OK, in this simple case the p1 and p2 variables aren't really necessary but it demonstrates a case where changing to const char [] would be just plain wrong. Quote: >But, there are lot of books which use 'const char* const' or 'const >char* const'. What do you for this?
Get a better book? ;-) -- -----------------------------------------
-----------------------------------------
|
Thu, 01 Apr 2004 02:32:28 GMT |
|
 |
David Thompso #12 / 16
|
 Why not static const char * const __func__ ?
Quote:
> >Why not > >static const char * const __func__ instead of > >static const char __func__[] ? ... > Because the former would need to allocate a pointer and an unnamed string, > whereas the latter would need to allocate just a named string.
Absolutely. Quote: > Furthermore, you may want to & it, in which case you want the address > of the array, and not the pointer.
Would I? Why? const char (*)[N] where you often can't (easily) know and certainly can't change N is pretty much unusable in C (in C++ it could be useful with a template<int n> ... char(*&x)[n]). OTOH const char * * could be useful in some cases, although of course you can easily construct that from this if you want it. Dave V's argument about sizeof seems stronger to me, although strlen() of a const pointer to a const known string could validly be optimized away at compile time. Another IMO useful but not compelling reason is consistency with __FILE__ which is a string literal and thus an unnamed static sort-of-const array (which of course decays to pointer). (And in C++ a really-const array which decays to nonconst!) -- - David.Thompson 1 now at worldnet.att.net
|
Fri, 09 Apr 2004 10:11:50 GMT |
|
 |
Greg Come #13 / 16
|
 Why not static const char * const __func__ ?
Quote:
>> >Why not >> >static const char * const __func__ instead of >> >static const char __func__[] ? >... >> Because the former would need to allocate a pointer and an unnamed string, >> whereas the latter would need to allocate just a named string. >Absolutely. >> Furthermore, you may want to & it, in which case you want the address >> of the array, and not the pointer. >Would I?
In some cases. Quote: >Why? const char (*)[N] where you often can't (easily) >know and certainly can't change N is pretty much unusable in C >(in C++ it could be useful with a template<int n> ... char(*&x)[n]).
Perhaps, but char * is sufficient too to point to char arrays. Quote: >OTOH const char * * could be useful in some cases, although >of course you can easily construct that from this if you want it.
Or char * :) Quote: >Dave V's argument about sizeof seems stronger to me, >although strlen() of a const pointer to a const known string >could validly be optimized away at compile time.
I dunno, as I recall it's not a matter of strength but one of ensuring a unique array. Quote: >Another IMO useful but not compelling reason is consistency >with __FILE__ which is a string literal and thus an unnamed >static sort-of-const array (which of course decays to pointer). >(And in C++ a really-const array which decays to nonconst!)
But __func__ isn't consistent with __FILE__, as both our discussions have shown. -- Greg Comeau export ETA: December See our Oct 31st special Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
Mon, 12 Apr 2004 11:11:31 GMT |
|
 |
David Thompso #14 / 16
|
 Why not static const char * const __func__ ?
... Quote: > >> >Why not > >> >static const char * const __func__ instead of > >> >static const char __func__[] ? ... > >> Furthermore, you may want to & it, in which case you want the address > >> of the array, and not the pointer. ... > >Why? const char (*)[N] where you often can't (easily) > >know and certainly can't change N is pretty much unusable in C > >(in C++ it could be useful with a template<int n> ... char(*&x)[n]). > Perhaps, but char * is sufficient too to point to char arrays.
But &array isn't char *; that's my point. Either array or pointer evaluates to [?const] char * (after decay or fetch), so that wouldn't be a basis for preferring the former over the latter. ... Quote: > >Dave V's argument about sizeof seems stronger to me, ... > I dunno, as I recall it's not a matter of strength but > one of ensuring a unique array.
Unique as in being at different addresses? OK, that's a feature, although even if an implementation merges string constants and doesn't disable/suppress it for these strings (which are obviously known special) the results can be at the same address only for functions with the same name, which can only be if they are static (internal) in different t.u.s, in which case I would think it less likely to want to compare them. And using the address of the pointer cell would still be unique. Quote: > >Another IMO useful but not compelling reason is consistency > >with __FILE__ which is a string literal and thus an unnamed > >static sort-of-const array (which of course decays to pointer). > >(And in C++ a really-const array which decays to nonconst!) > But __func__ isn't consistent with __FILE__, as both > our discussions have shown.
They are accessed differently (a supposedly ordinary identifier versus a definite macro-name) but they both yield char arrays. -- - David.Thompson 1 now at worldnet.att.net
|
Sat, 01 May 2004 10:18:57 GMT |
|
 |
Greg Come #15 / 16
|
 Why not static const char * const __func__ ?
Quote:
>... >> >> >Why not >> >> >static const char * const __func__ instead of >> >> >static const char __func__[] ? >... >> >> Furthermore, you may want to & it, in which case you want the address >> >> of the array, and not the pointer. >... >> >Why? const char (*)[N] where you often can't (easily) >> >know and certainly can't change N is pretty much unusable in C >> >(in C++ it could be useful with a template<int n> ... char(*&x)[n]). >> Perhaps, but char * is sufficient too to point to char arrays. >But &array isn't char *; that's my point.
But char *p = SomeCharArray; is and that was my point (I think, I've lost the original context since I posted what I said.) Quote: >Either array or pointer >evaluates to [?const] char * (after decay or fetch), so that >wouldn't be a basis for preferring the former over the latter.
Yes, but if you & it, you don't want the address of a pointer that point to some string literal, you want the address of the string literal. Quote: >... >> >Dave V's argument about sizeof seems stronger to me, >... >> I dunno, as I recall it's not a matter of strength but >> one of ensuring a unique array. >Unique as in being at different addresses?
Yes (as I recall what we were talking about.) Quote: >OK, that's a feature, >although even if an implementation merges string constants >and doesn't disable/suppress it for these strings (which are >obviously known special) the results can be at the same >address only for functions with the same name, which can >only be if they are static (internal) in different t.u.s, in which >case I would think it less likely to want to compare them. >And using the address of the pointer cell would still be unique.
The pointer might be unique, but the string won't be. Quote: >> >Another IMO useful but not compelling reason is consistency >> >with __FILE__ which is a string literal and thus an unnamed >> >static sort-of-const array (which of course decays to pointer). >> >(And in C++ a really-const array which decays to nonconst!) >> But __func__ isn't consistent with __FILE__, as both >> our discussions have shown. >They are accessed differently (a supposedly ordinary identifier >versus a definite macro-name) but they both yield char arrays.
They may be similar, but other aspects aren't. -- Greg Comeau export ETA: December See our Oct 31st special Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
|
Sat, 01 May 2004 10:52:58 GMT |
|
|
Page 1 of 2
|
[ 16 post ] |
|
Go to page:
[1]
[2] |
|