[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Language War (Re: "C" Manual)



Gary Turner wrote:
> 
> On Fri, 04 Jan 2002 14:28:46 -0500, dman wrote:
> 
> >On Thu, Jan 03, 2002 at 09:39:09PM -0600, Gary Turner wrote:
> >| On Thu, 03 Jan 2002 17:34:00 -0600 (CST), Richard Cobbe wrote:
> >| >
> >| >Lo, on Thursday, January 3, Erik Steffl did write:
> >| >
> >| >> what's the difference? the point is you can assign almost anything to
> >| >> anything, and yet there is no segfault - i.e. the strength of types has
> >| >> nothing (sort of) to do with segfaults... the resource allocation is
> >| >> crucial...
> >| >
> >| >Type safety (plus dynamic allocation) implies advanced memory
> >| >management.  The converse is not true: you can slap Boehm's conservative
> >| >GC onto a C++ program, but you can still get segmentation faults:
> >| >
> >| >    char str[] = { 'b', 'a', 'd', ' ', 's', 't', 'r', 'i', 'n', 'g' };
> >| >    // note the lack of a terminating '\0'!
> >| >    cout << str;
> >| >
> >|
> >| >No allocation issues involved.  As Ben Collins pointed out elsewhere in
> >| >this thread (bit of a tree-shaped thread, isn't it?), this won't
> >| >necessarily cause a segfault, but it can.  It's also a violation of
> >| >type-safety: cout expects a null-terminated string, and as far as the
> >| >compiler is concerned, str fits this.  However, there's no runtime check
> >| >in the output routine to verify that this is, in fact, the case.  Ooops.
> >|
> >| Neophyte that I am, I feel like I'm bringing a knife to this gunfight.
> >| This example looks to be a cheat, in that you've defined an array and
> >| then treated it as a string (legal).  Had you defined a string, it would
> >| be null terminated and index addressable.
> >
> >He did define a string.  In C++ there are 3 ways of defining a string
> >(in C there are 2).  There is "char[]", "char*" and "std::string".
> Isn't 'char*' redundant, since an array var is a pointer by definition?
> If I'm showing my ignorance again, I apologize.

  it's not exactly the same, one thing is that you cannot change where
the array points to, the other difference is how they are treated by
sizeof (there might be other differences).

> >The latter is the best way because it provides the most protection.
> >
> I disagree.  He defined an array of characters, just as
>     int a[] = 1,2,3;
> is an array of integers.
> To define a string he should say
>     char a[] = "bad string";
> or
>     char a[11];

  the last one does not zero-terminate the array.

  and btw in all cases the array of characters was defined. in some
cases the array is zero-terminated and can be used with certain
functions that assume it to be zero-terminated. but they are all of same
type.

...
> >I think that would be an allocation error since it is illegal to
> >access memory outside the allocated bounds.
> >
> It would be nice if c/c++ included array bounds checking, but since it
> doesn't, the programmer *must* check and control it himself.  The array
> is properly allocated.  It is the programmer who decided to go into
> unmapped territory.  If only Picard would release...

  in c++ you can (should) use the template class (or string, which is
template instantiated for char) instead of array of characters. the
string class takes cares of checking the length etc.

	erik



Reply to: