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


Groups > comp.lang.c > #147697

Re: What are the odds? (a continuation of One works ...)

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: What are the odds? (a continuation of One works ...)
Date 2019-11-06 19:26 -0800
Organization None to speak of
Message-ID <lny2ws4ba3.fsf@kst-u.example.com> (permalink)
References (2 earlier) <fQBwF.59470$Ko9.7309@fx48.iad> <vvv5sehh7oil3eb5dft8dun7s4t1rbt4h7@4ax.com> <voGwF.93833$vD7.57507@fx06.iad> <g4g6se5u1ggsmecioggck2k7ve59pfdbbp@4ax.com> <joIwF.86203$FF4.79851@fx40.iad>

Show all headers | View raw


DFS <nospam@dfs.com> writes:
[...]
> char charset[62]
> ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
> int charsetLen = strlen(charset);
> printf("%d \n",charsetLen);
> 62

That still has undefined behavior.  It appears to "work" only because,
most likely, there just happens to be a null byte in memory immediately
after charset.

I just compiled and ran your on my system.  I got 62 with gcc and clang,
and 65 with tcc.  There is not necessarily any particular reason for
this, and it could just as easily produce different results tomorrow.

> char charset[63] =
> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
> int charsetLen = strlen(charset);
> printf("%d \n",charsetLen);
> 62

That's well defined, but as I mentioned elsethread your (incorrect)
strlen call is the only thing that relies on charset containing a valid
string.

If you define charset using [] you can get its size using sizeof.

>> When you tested the code, did you not notice extraneous lines of 
>> output after the line for "9"?
>
> Not seeing that.  Compiled and ran on tcc/Windows and gcc (MSYS2) and 
> there are no extraneous lines of output after the output for 9.

That's one of thep problems with undefined behavior: it can evade
testing.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

Re: What are the odds? (a continuation of One works ...) Barry Schwarz <schwarzb@delq.com> - 2019-11-06 14:23 -0800
  Re: What are the odds? (a continuation of One works ...) Bart <bc@freeuk.com> - 2019-11-06 22:35 +0000
    Re: What are the odds? (a continuation of One works ...) luser droog <luser.droog@gmail.com> - 2019-11-06 17:09 -0800
    Re: What are the odds? (a continuation of One works ...) Keith Thompson <kst-u@mib.org> - 2019-11-06 19:19 -0800
      Re: What are the odds? (a continuation of One works ...) Ian Collins <ian-news@hotmail.com> - 2019-11-07 16:30 +1300
        Re: What are the odds? (a continuation of One works ...) Bart <bc@freeuk.com> - 2019-11-07 10:59 +0000
        Re: What are the odds? (a continuation of One works ...) David Brown <david.brown@hesbynett.no> - 2019-11-07 14:27 +0100
  Re: What are the odds? (a continuation of One works ...) DFS <nospam@dfs.com> - 2019-11-06 18:09 -0500
    Re: What are the odds? (a continuation of One works ...) Keith Thompson <kst-u@mib.org> - 2019-11-06 19:26 -0800
      Re: What are the odds? (a continuation of One works ...) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-07 11:57 +0000

csiph-web