Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #104842

Re: writing a c program to substitute text

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: writing a c program to substitute text
Date 2017-02-27 13:11 +0100
Organization A noiseless patient Spider
Message-ID <o914th$6pv$1@dont-email.me> (permalink)
References (4 earlier) <o8vplm$o5a$1@dont-email.me> <a935c0ac-022a-4ca5-9e7a-a821785d9e68@googlegroups.com> <87bmto8kys.fsf@bsb.me.uk> <o90ntq$q79$1@dont-email.me> <9eb280b4-776c-4461-9d7c-161b1698d1b4@googlegroups.com>

Show all headers | View raw


On 27/02/17 11:12, Malcolm McLean wrote:
> On Monday, February 27, 2017 at 8:29:43 AM UTC, David Brown wrote:
>> On 27/02/17 02:46, Ben Bacarisse wrote:
>>> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>>>
>>> You are in danger of talking at cross-purposes.  Your idea of a safe
>>> object is one that is safe regardless of what is done with it.  David's
>>> view of a safe object is one that is (or maybe can be) used safely.
>>>
>>
>> Thank you for that insight.  I hadn't thought of it from that point of
>> view, and it gives some reasoning behind Malcolm's claims.
>>
> Ben's right. Of course char *arrow = "arrow" is safe in the sense that it
> doesn't cause an immediate memory error, and in skilled hands it can
> be used safely.

What do you mean, "in skilled hands"?  It is hard to be /unsafe/ with
this method - you need "skilled hands" to break it.  Of course, that
assumes you are able to correctly write "/const/ char* arrow", rather
than fail to copy the example.

>>
>> I think we can work with a rough definition - something is "safe" for a
>> particular purpose if you can use it reliably for that purpose without
>> having unexpected side-effects.  The /purpose/ is key here.  A car may
>> have airbags, seatbelts, automatic braking and all sorts of features to
>> make it safe for driving - but it is still not going to be very safe as
>> a boat.
>>
> I'd say you've got engineered safety - which a car has, thousands of them 
> drive around the city every day with acceptable consequences - and inherent
> safety. A car is inherently dangerous, because there's no way of telling it
> not to crash into a pedestrian, other than the engineered system of roads
> and pavements and traffic lights. A bike is inherently safe, because even if it 
> does crash into a pedestrian, it doesn't have the mass or speed to cause
> a fatal injury, at least usually,

Fair enough.  That is going into more detail than I did, but I agree
with it.

>>
>> In this case, the OP was asking about hard-coded strings between quotes.
>>  In C, you can read these - you cannot write them.  So "safe" here means
>> being able to reliably access them for reading, with a minimum of
>> unexpected side-effects (such as running out of memory, corrupting
>> structures, leaking resources, etc.).  Using a const*char pointer to
>> "arrow", or using "const char s[] = "arrow";" with program lifetime
>> (file scope, or function static), will give you that with the most
>> rigorously controlled memory model C can provide.
>>
> But what if you try to free it? Or what if you want to strip spaces, which 90%
> of the time will be a no-op? 

You can't "free" it - but there is no logical reason why you would try
to do so.  The same applies to any "rigorously managed" string system -
you would never call "free" from user code.  If user code is calling
functions like malloc or free directly, it is not "rigorously managed".

You cannot strip spaces from the string either - it is read-only.  It is
a fixed "hard-coded" string, just as the OP wanted.

>>
>> If the user wants to modify the string, then it's a different matter -
>> and more complex code is needed to be sure it is done safely.  Malcolm
>> is still wrong about the heap being somehow "safer" than the stack, however.
>>
> ava is easier that C++, largely because Java doesn't allow objects other than
> primitive and handles on the stack.

No, that is not the case - it is totally wrong.

First, Java is "easier" than C++ for some things, harder for others.  It
is true that it is easier to write Java code without the typical C
mistakes of failing to free heap memory or trying to free it twice.  But
modern C++ also makes it easy to write such code, using unique_ptr and
shared_ptr.  The key to Java's memory handling is its garbage collection
so that user code does not have to "free" anything, and thus cannot make
mistakes with "free" - the fact that most objects are put on the heap is
completely incidental.

In C, the safest place to put run-time data is on the stack - precisely
because you don't have to worry about freeing the memory.  (It is also
more efficient, which is a nice bonus.)  Of course you must be careful
about the sizes of data to allocate, and you typically have no
information about allocation failure.  And the allocated memory does not
live beyond the lifetime of the function.  But when stack data can be
used, it is safer than the heap because it is simpler.

> C++ is harder to program because a object
> could be on the heap or on the stack.

That does not make it harder to program.

> C++ people have evolved ways of dealing
> with that, but it's engineered safety.
> 

The C++ language has evolved more convenient ways of handling memory
safety.  Because it is part of the language, it is at least as much
"inherent safety" as the Java method - both languages have been
"engineered" to make them as safe as practical for users.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-26 04:14 -0800
  Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-26 04:55 -0800
  Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-26 14:28 +0100
    Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 03:58 -0800
      Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:11 +0000
        Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 13:33 +0000
          Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 17:12 +0000
            Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 17:59 +0000
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:05 +0100
              Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 18:37 +0000
                Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 11:40 -0800
        Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:03 +0100
        Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 05:33 -0800
          Re: writing a c program to substitute text "D. Aaron Sawyer" <aaron@110.net> - 2017-03-01 10:32 -0400
            Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 06:59 -0800
              Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 15:24 +0000
                Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:47 -0800
                Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 11:05 +0000
      Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 13:33 +0000
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:10 -0800
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:07 +0100
            Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 18:23 +0000
      Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 14:46 +0100
        Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:27 -0500
          Re: writing a c program to substitute text GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-27 15:28 +0100
            Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:38 -0500
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 15:44 +0100
            Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 10:36 -0500
            Re: writing a c program to substitute text Anglican <h.toms@qmul.ac.uk> - 2017-02-27 15:50 +0000
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 17:13 +0100
              Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 16:30 -0800
          Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:08 -0800
            Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:20 -0800
              Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:24 -0800
        Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:00 +0100
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:22 -0800
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:11 +0100
      Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-02-28 15:41 -0800
        solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:25 -0800
          Re: solved Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-03-02 11:28 +0100
          Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 10:54 +0000
            Re: solved Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-03-02 15:28 +0100
              Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 14:51 +0000
            Re: solved Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-03-02 14:18 -0800
              Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 23:55 +0000
                Re: solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-14 18:32 -0700
        Re: writing a c program to substitute text Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-17 07:04 -0700
    Re: writing a c program to substitute text jladasky@itu.edu - 2017-02-27 13:48 -0800
      Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 14:16 -0800
        Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 08:47 +0100
          Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 01:59 -0800
            Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 11:39 +0100
            Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-28 12:03 +0000
              Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:33 +0000
                Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:43 +0100
                Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:59 +0000
                Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 04:26 -0800
      Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 23:29 +0100
      Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 15:10 -0800
  Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 06:42 -0800
    Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-26 16:54 +0000
      [OT] "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 00:56 +0800
    Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 13:36 -0800
      Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 14:53 -0800
        Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 00:53 +0100
          Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 17:30 -0800
            Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 17:44 -0800
            Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 01:46 +0000
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 09:29 +0100
                Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 02:12 -0800
                Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 13:11 +0100
    Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 04:41 -0800
      Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 05:04 -0800
        Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:25 +0000
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:56 -0800
  Re: writing a c program to substitute text "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 01:00 +0800
  Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-26 18:55 -0500
    Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 04:20 -0800
      Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:38 +0100
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:33 -0800
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 19:38 +0100
            Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 11:40 -0800
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 21:03 +0100
            Re: gcc optimization levels Noob <root@127.0.0.1> - 2017-02-28 22:55 +0100
      Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-28 11:43 -0500
      Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:46 -0800

csiph-web