Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Allocating memory for strings

Thread Tools

Allocating memory for strings

David Thompson
Posts: n/a
On Sun, 07 Oct 2007 16:35:29 +0200, Army1987 <(E-Mail Removed)>

> On Sat, 06 Oct 2007 13:07:24 -0700, Keith Thompson wrote:
> > Except that string literals aren't const. (Attempting to modify a
> > string literal invokes undefined behavior, but only because the
> > standard explicitly says so.) It would be better if string literals
> > *were* const, but that would have broken existing code back in 1989
> > when the ANSI standard first introduced the "const" keyword.

> They could have made string literals const while allowing implicit
> conversion of const char * to char *,

That would have been much _more_ dangerous; it would allow undetected
screwups even for program(mer)s that went to the effort of using
(adding) the new 'const' qualification. Even if you limited it to
occurring during decay, const char [] to char *, as C++ does.

C++ doesn't actually solve the problem for C strings, and really
can't. What it does do is provide a whole new set of std::*string's,
with lots of new features including correct constificationosity.

> and make the return type of
> strchr, strstr etc. const char *, as in C++.

No, in C++ there are BOTH const and nonconst versions, overloaded.
Which C++ can easily do because it already needed and had overloading.
C89 didn't, and the committee apparently judged that adding it just
for this would have been overkill. Although a decade later they did
require effective overloading, at least of builtins, for <tgmath.h>.

- formerly david.thompson1 || achar(64) ||
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocating Memory And Strings Mike C Programming 6 02-28-2009 03:03 PM
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM