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


Groups > comp.lang.c > #27006

Re: Who owns the variable in my header file ?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Who owns the variable in my header file ?
Date 2012-10-04 01:30 -0700
Organization None to speak of
Message-ID <ln7gr6prk8.fsf@nuthaus.mib.org> (permalink)
References <EK-dnVoFlczbHfHNnZ2dnUVZ8hidnZ2d@bt.com> <k4i2a7$uhj$1@dont-email.me> <n8CdnWrDGMuGBPHNnZ2dnUVZ8gednZ2d@bt.com> <k4i7dk$1fu$1@dont-email.me> <ddydnU95dYxYovDNnZ2dnUVZ8gOdnZ2d@bt.com>

Show all headers | View raw


lipska the kat <lipskathekat@yahoo.co.uk> writes:
[...]
> Of course, the point I was trying to make is that if my program is 
> behaving in an 'undefined' way then I might expect 10 runs with 
> identical data to provide different results. I'm in no way sufficiently 
> knowledgeable about C to assume otherwise. I suppose it depends on what 
> you mean by undefined.

No, that's not what undefined means.  The C standard's definition of
*undefined behavior* is:

    behavior, upon use of a nonportable or erroneous program construct
    or of erroneous data, for which this International Standard imposes
    no requirements

    NOTE Possible undefined behavior ranges from ignoring the
    situation completely with unpredictable results, to behaving
    during translation or program execution in a documented manner
    characteristic of the environment (with or without the issuance
    of a diagnostic message), to terminating a translation or
    execution (with the issuance of a diagnostic message).

> If I have a program that reverses it's input a line at a time (ex 1-19 K 
> and R second edition for example) and I try it with as many different 
> inputs as my feeble brain can devise and the results are what I expect 
> then what can I assume from this. In other languages I have used (10s of 
> KLOC running daily without error) I would assume that the program was 
> 'correct'.

C, as the saying goes, gives you enough rope to shoot yourself in the
foot.  I'll show you a concrete example:

    #include <stdio.h>
    
    static void write_array(int *arr) {
        for (int i = 0; i <= 5; i ++) {
            arr[i] = i;
        }
    }
    
    static void read_array(int *arr) {
        for (int i = 0; i <= 5; i ++) {
            printf("%d", arr[i]);
            putchar(i == 5 ? '\n' : ' ');
        }
    }
    
    int main(void) {
        int x[5] = { 0 };
        int y[5] = { 0 };
        int z[5] = { 0 };
    
        write_array(y);
        read_array(y);
    
        return 0;
    }

The array y is defined to have 5 elements, but the program attempts to
store 6 int values in it, and then retrieve and print those 6 values.
Accessing y[5] has undefined behavior, since it's outside the bounds of
the array.  But since y is surrounded in memory by two other arrays, x
and z, it's likely that y[5] refers to an element of one of those other
two arrays.  (There's no guarantee that x, y, and z are allocated in any
particular order, or even that they're adjacent, but it's likely that
one of them immediately follows y in memory.)

I can compile and run this program 100 times, and it's very likely to
produce the same output every time:

    0 1 2 3 4 5

That's just one of the infinitely many things that can happen when the
language standard "imposes no requirements".

(A sufficiently clever optimizing compiler might cause it to produce
different output, or to crash, or even to be rejected at compile time.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-03 19:13 +0100
  Re: Who owns the variable in my header file ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-10-03 15:05 -0400
    Re: Who owns the variable in my header file ? pacman@kosh.dhis.org (Alan Curry) - 2012-10-03 19:17 +0000
      Re: Who owns the variable in my header file ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-10-03 15:55 -0400
    Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-03 20:59 +0100
      Re: Who owns the variable in my header file ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-10-03 16:32 -0400
        Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 08:50 +0100
          Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-04 01:30 -0700
            Re: Who owns the variable in my header file ? Angel <angel+news@spamcop.net> - 2012-10-04 10:17 +0000
            Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 12:02 +0100
              Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-04 16:20 -0700
                Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-05 08:44 +0100
                Re: Who owns the variable in my header file ? Chicken McNuggets <chicken@mcnuggets.com> - 2012-10-07 16:48 +0100
          Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-04 07:13 -0400
      Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-04 00:34 -0400
        Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 09:02 +0100
          Re: Who owns the variable in my header file ? David Brown <david@westcontrol.removethisbit.com> - 2012-10-04 10:14 +0200
          Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-04 08:30 -0400
            Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 14:18 +0100
            Re: Who owns the variable in my header file ? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-06 02:30 -0700
              Re: Who owns the variable in my header file ? Les Cargill <lcargill99@comcast.com> - 2012-10-06 08:01 -0500
              Re: Who owns the variable in my header file ? Richard Damon <news.x.richarddamon@xoxy.net> - 2012-10-06 21:30 -0400
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-07 11:40 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-07 10:56 -0400
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-07 21:26 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-07 19:58 -0400
                Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-07 11:47 -0700
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-07 21:53 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-07 20:04 -0400
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-08 02:14 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-07 21:47 -0400
                Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-08 00:33 -0700
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-08 11:13 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-08 07:40 -0400
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-08 13:13 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-08 10:08 -0400
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-08 16:20 +0100
                Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-08 12:45 -0400
                Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-08 12:29 -0700
                Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-08 22:28 +0100
                Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-08 15:47 -0700
                Re: Who owns the variable in my header file ? Richard Damon <news.x.richarddamon@xoxy.net> - 2012-10-07 21:16 -0400
          Re: Who owns the variable in my header file ? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-04 08:35 -0600
          Re: Who owns the variable in my header file ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-04 22:31 +0100
            Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-05 08:58 +0100
        Re: Who owns the variable in my header file ? gordonb.mhaw9@burditt.org (Gordon Burditt) - 2012-10-06 20:19 -0500
          Re: Who owns the variable in my header file ? Ian Collins <ian-news@hotmail.com> - 2012-10-07 15:19 +1300
          Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-07 18:22 +0100
            Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-07 14:38 -0400
      Re: Who owns the variable in my header file ? Kaz Kylheku <kaz@kylheku.com> - 2012-10-04 05:14 +0000
  Re: Who owns the variable in my header file ? Kaz Kylheku <kaz@kylheku.com> - 2012-10-03 19:40 +0000
  Re: Who owns the variable in my header file ? Ike Naar <ike@sverige.freeshell.org> - 2012-10-03 19:52 +0000
  Re: Who owns the variable in my header file ? falk@rahul.net (Edward A. Falk) - 2012-10-03 22:36 +0000
    Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-03 16:21 -0700
      Re: Who owns the variable in my header file ? falk@rahul.net (Edward A. Falk) - 2012-10-04 19:01 +0000
      Re: Who owns the variable in my header file ? David Thompson <dave.thompson2@verizon.net> - 2012-10-11 03:09 -0400
        Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-11 13:30 -0700
    Re: Who owns the variable in my header file ? Kaz Kylheku <kaz@kylheku.com> - 2012-10-04 01:43 +0000
    Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 09:24 +0100
  Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 11:23 +0100
    Re: Who owns the variable in my header file ? David Brown <david@westcontrol.removethisbit.com> - 2012-10-04 13:26 +0200
      Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 15:52 +0100
        Re: Who owns the variable in my header file ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-10-04 11:40 -0400
          Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-04 17:00 +0100
        Re: Who owns the variable in my header file ? hormelfree@gmail.com - 2012-10-04 17:15 -0700
          Re: Who owns the variable in my header file ? Ike Naar <ike@iceland.freeshell.org> - 2012-10-05 06:51 +0000
            Re: Who owns the variable in my header file ? David Brown <david@westcontrol.removethisbit.com> - 2012-10-05 09:35 +0200
          Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-05 09:11 +0100
            Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-05 08:04 -0400
    Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-04 08:43 -0400
    Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-04 16:08 -0700
      Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-05 09:27 +0100
        Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-05 02:16 -0700
        Re: Who owns the variable in my header file ? Angel <angel+news@spamcop.net> - 2012-10-05 11:38 +0000
          Re: Who owns the variable in my header file ? Nobody <nobody@nowhere.com> - 2012-10-05 16:13 +0100
            Re: Who owns the variable in my header file ? Angel <angel+news@spamcop.net> - 2012-10-05 16:26 +0000
  Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-08 19:02 +0100
    Re: Who owns the variable in my header file ? James Kuyper <jameskuyper@verizon.net> - 2012-10-08 14:29 -0400
      Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-08 20:15 +0100
      Re: Who owns the variable in my header file ? Keith Thompson <kst-u@mib.org> - 2012-10-08 14:04 -0700
    Re: Who owns the variable in my header file ? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-08 22:47 -0600
      Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-09 09:04 +0100
        Re: Who owns the variable in my header file ? "BartC" <bc@freeuk.com> - 2012-10-09 10:30 +0100
          Re: Who owns the variable in my header file ? lipska the kat <lipskathekat@yahoo.co.uk> - 2012-10-09 11:48 +0100

csiph-web