what is difference between sizeof and strlen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anton Petrusevich

    #31
    Re: what is difference between sizeof and strlen

    Lawrence Kirby wrote:
    [color=blue]
    > By writing void main() your program has undefined behaviour even if it
    > never returns or exits at all. There is no guarantee that will even be
    > called successfully at program startup - you havn't provided a function
    > with a type that is compatible with what the implementation expects to
    > call.[/color]

    2 variants: either it works or it does not. And only one of them will be
    chosen on some particular system/compilator. I would not call it "undefined
    behaviour", it's quite defined for a platform. And I just want to know the
    name of the platform where it does not work. Could you enlighten me?
    --
    Anton Petrusevich

    Comment

    • Anton Petrusevich

      #32
      Re: what is difference between sizeof and strlen

      Keith Thompson wrote:
      [color=blue]
      > When your
      > program misbehaves mysteriously, using int main(void) means there's
      > one less thing to worry about when you're tracking down the problem.[/color]

      As I understand it: run-time either calls void main() or it does not. It
      can't be "mysterious ", I see it as a "defined" behaviour.
      [color=blue]
      > With void main(), if you post your code here it's *guaranteed* that
      > people will jump down your throat about it.[/color]

      Oh, yes, I see it now. I would call it "religious thing". I am not
      religious, though I write "int main()" too. I just don't pay so big
      attention to such things. I am a newbie in this conference and I am very
      surprised seeing such a "religious thing" here.
      --
      Anton Petrusevich

      Comment

      • Richard Tobin

        #33
        Re: what is difference between sizeof and strlen

        In article <dd7sp7$ukj$01$ 1@news.t-online.com>,
        Anton Petrusevich <casus@att-ltd.biz> wrote:[color=blue]
        >I would not call it "undefined behaviour"[/color]

        "Undefined behaviour" is a technical term in the C standard. It's not
        really relevant whether *you* would call it that.

        -- Richard

        Comment

        • Anton Petrusevich

          #34
          Re: what is difference between sizeof and strlen

          Richard Tobin wrote:
          [color=blue]
          > "Undefined behaviour" is a technical term in the C standard. It's not
          > really relevant whether *you* would call it that.[/color]

          I don't get it. I thought, and I still continue to think, that "undefined
          behaviour" means just that: when the behaviour is undefined. If the
          behaviour is dependent on some defined thing (platform) then it's
          "defined". You (or I) just need to know the platform. Of course, there's no
          such (defined) thing as "platform" in C standard, so they say "undefined
          behaviour". "int main()" is a tough religion, isn't it?
          --
          Anton Petrusevich

          Comment

          • Stephane Zuckerman

            #35
            Re: what is difference between sizeof and strlen

            > > "Undefined behaviour" is a technical term in the C standard. It's not[color=blue][color=green]
            > > really relevant whether *you* would call it that.[/color]
            >
            > I don't get it. I thought, and I still continue to think, that "undefined
            > behaviour" means just that: when the behaviour is undefined. If the
            > behaviour is dependent on some defined thing (platform) then it's
            > "defined".[/color]

            I think what you really missed is that this newsgroup is about ANSI/ISO C,
            that is to say, C as a normalized language. Of course you are right : for
            a given architecture, and a given compiler, the behaviour is predictable
            to a certain extent. But then, we are not talking about portable C, or
            ANSI C anymore. Let's go even further : is the behaviour of your compiler
            documented ? If not, that means that it can change when the next version
            comes out -- and that means modifying your code if you have to make it
            compile with this brand new compiler, maybe forcing you to maintain two
            slightly different versions of the same program.
            [color=blue]
            > You (or I) just need to know the platform. Of course, there's no
            > such (defined) thing as "platform" in C standard, so they say "undefined
            > behaviour". "int main()" is a tough religion, isn't it?[/color]
            No, it is just the basis. If you allow yourself to write "void main()" out
            of lazyness, what will you do with things that are really complicated ?

            Lots and lots of C code can be made portable, hence helping you to avoid
            rewriting too much code. This is truly a blessing (to stay with a
            religious tone ;-)), and you should be happy this is so.

            Once again, you are totally free to write non-standard C. This is just...
            Not ANSI C, and therefore, not C, as the standard knows it.

            --
            "Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
            que je veux !"
            "The obvious mathematical breakthrough would be development of an easy
            way to factor large prime numbers." (Bill Gates, The Road Ahead)

            Comment

            • Richard Tobin

              #36
              Re: what is difference between sizeof and strlen

              In article <dd80vt$cpg$00$ 1@news.t-online.com>,
              Anton Petrusevich <casus@att-ltd.biz> wrote:
              [color=blue]
              >I don't get it. I thought, and I still continue to think, that "undefined
              >behaviour" means just that: when the behaviour is undefined.[/color]

              What the C standard means by it is that C standard places no
              constraints on what will happen.
              [color=blue]
              >If the
              >behaviour is dependent on some defined thing (platform) then it's
              >"defined".[/color]

              If the C standard requires the implementation to specify what will happen,
              it's "implementa tion defined".
              [color=blue]
              >Of course, there's no
              >such (defined) thing as "platform" in C standard, so they say "undefined
              >behaviour".[/color]

              There's also "implementa tion defined" as I mentioned above, but this
              isn't a case of it.

              -- Richard

              Comment

              • Anton Petrusevich

                #37
                Re: what is difference between sizeof and strlen

                Stephane Zuckerman wrote:
                [color=blue]
                > Once again, you are totally free to write non-standard C. This is just...
                > Not ANSI C, and therefore, not C, as the standard knows it.[/color]

                Thanks, this is the best answer :)
                --
                Anton Petrusevich

                Comment

                • Chris F.A. Johnson

                  #38
                  Re: what is difference between sizeof and strlen

                  On 2005-08-08, Anton Petrusevich wrote:[color=blue]
                  > Keith Thompson wrote:
                  >[color=green]
                  >> When your
                  >> program misbehaves mysteriously, using int main(void) means there's
                  >> one less thing to worry about when you're tracking down the problem.[/color]
                  >
                  > As I understand it: run-time either calls void main() or it does not. It
                  > can't be "mysterious ", I see it as a "defined" behaviour.
                  >[color=green]
                  >> With void main(), if you post your code here it's *guaranteed* that
                  >> people will jump down your throat about it.[/color]
                  >
                  > Oh, yes, I see it now. I would call it "religious thing". I am not
                  > religious, though I write "int main()" too. I just don't pay so big
                  > attention to such things. I am a newbie in this conference and I am very
                  > surprised seeing such a "religious thing" here.[/color]

                  int main(void) is guaranteed to work.

                  void main() is not guaranteed to work.

                  What's religious about that?

                  --
                  Chris F.A. Johnson <http://cfaj.freeshell. org>
                  =============== =============== =============== =============== ======
                  Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
                  <http://www.torfree.net/~chris/books/cfaj/ssr.html>

                  Comment

                  • Keith Thompson

                    #39
                    Re: what is difference between sizeof and strlen

                    Anton Petrusevich <casus@att-ltd.biz> writes:[color=blue]
                    > Keith Thompson wrote:
                    >[color=green]
                    >> When your
                    >> program misbehaves mysteriously, using int main(void) means there's
                    >> one less thing to worry about when you're tracking down the problem.[/color]
                    >
                    > As I understand it: run-time either calls void main() or it does not. It
                    > can't be "mysterious ", I see it as a "defined" behaviour.[/color]

                    You understand it incorrectly. The standard says that main shall
                    be defined with no parameters:
                    int main(void); /* or equivalent */
                    or with two parameters:
                    int main(int argc, char *argv[]); /* or equivalent */
                    or in some other implementation-defined manner. If an
                    implementation' s documentation does not explicitly state that it
                    supports some other definition, then there is no "other
                    implementation-defined manner", and the two standard definitions are
                    the only ones allowed.

                    Since this is a "shall" requirement that's not part of a constraint,
                    violating it invokes undefined behavior.

                    A conforming implementation could reject "void main(void){}", or it
                    could cause it to behave in any perverse manner it chooses.

                    An implementation is allowed to support void main(void), but even
                    if it does, the following:

                    void main(void)
                    {
                    /* whatever */
                    }

                    is almost certainly equivalent to this:

                    int main(void)
                    {
                    /* whatever */
                    return 0;
                    }

                    and since the latter is portable, there is no advantage in using the
                    former.
                    [color=blue][color=green]
                    >> With void main(), if you post your code here it's *guaranteed* that
                    >> people will jump down your throat about it.[/color]
                    >
                    > Oh, yes, I see it now. I would call it "religious thing". I am not
                    > religious, though I write "int main()" too. I just don't pay so big
                    > attention to such things. I am a newbie in this conference and I am very
                    > surprised seeing such a "religious thing" here.[/color]

                    There's nothing religious about it. It's a matter of correctness
                    about which there are strong feelings, largely because certain people
                    and/or institutions have encouraged something that is incorrect for no
                    good reason. Perhaps the word "religious" doesn't mean what you think
                    it means. (And if you want to discuss that further, please take it to
                    alt.usage.engli sh.)

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • Martin Ambuhl

                      #40
                      Re: what is difference between sizeof and strlen

                      Anton Petrusevich wrote:
                      [color=blue]
                      > Oh, yes, I see it now.[/color]

                      No, you don't.
                      [color=blue]
                      > I would call it "religious thing".[/color]

                      Then you would be wrong. Using a language as it is defined is not
                      "religious. " Insisting that using it in a way not allowed by its
                      definition is mystical, religious, and stupid. You have chosen the
                      wrong camp to label.
                      [color=blue]
                      > I am not
                      > religious, though I write "int main()" too. I just don't pay so big
                      > attention to such things.[/color]

                      No one should need to pay much attention. It is only when the most
                      basic aspect of writing correct programs is ignored that anyone pays
                      attention. When people start with the quasi-anarchist use of the word
                      "religious" for using a language as it is defined, people get pissed.
                      [color=blue]
                      > I am a newbie in this conference and I am very
                      > surprised seeing such a "religious thing" here.[/color]

                      Get a clue.

                      Comment

                      • Martin Ambuhl

                        #41
                        Re: what is difference between sizeof and strlen

                        Anton Petrusevich wrote:
                        [color=blue]
                        > "int main()" is a tough religion, isn't it?[/color]

                        How long are you going to keep this crap up? Soon your pseudo-anarchism
                        and childish name-calling will land you in multiple killfiles.

                        Comment

                        • Keith Thompson

                          #42
                          Re: what is difference between sizeof and strlen

                          Anton Petrusevich <casus@att-ltd.biz> writes:[color=blue]
                          > Lawrence Kirby wrote:
                          >[color=green]
                          >> By writing void main() your program has undefined behaviour even if it
                          >> never returns or exits at all. There is no guarantee that will even be
                          >> called successfully at program startup - you havn't provided a function
                          >> with a type that is compatible with what the implementation expects to
                          >> call.[/color]
                          >
                          > 2 variants: either it works or it does not. And only one of them will be
                          > chosen on some particular system/compilator. I would not call it "undefined
                          > behaviour", it's quite defined for a platform. And I just want to know the
                          > name of the platform where it does not work. Could you enlighten me?[/color]

                          void main() is actually an unusual case, and perhaps not the best one
                          for illustrating the concept of undefined behavior.

                          The standard explicitly allows an implementation to define the
                          behavior of alternative definitions of main(), including void
                          main(void), but it does not require any implementation to do so. For
                          an implementation whose documentation *doesn't* mention void main(void),
                          the behavior of
                          void main(void){}
                          is undefined (see also "nasal demons"). For an implementation that
                          *does* explicitly document it, the behavior is implementation-defined.

                          In a sense, the extra permission given by the standard is redundant,
                          since an implementation is free to document anything it likes,
                          including defining the behavior of constructs that, as far as the
                          standard is concerned, invoke undefined behavior.

                          In a way, it's similar to what happens on integer overflow. The
                          following code fragment:

                          int i = 32767;
                          i ++;

                          invokes undefined behavior under an implementation with
                          INT_MAX == 32767, but is well defined where INT_MAX > 32767.

                          Bottom line: always use one of the standard definitions of main()
                          unless you have a very good reason not to. Saving a few keystrokes is
                          not a very good reason. Using a freestanding implementation (most
                          likely an embedded system) may be a very good reason.

                          In response to comments made elsewhere in this thread, none of this
                          has anything whatsoever to do with "religion". We are simply giving
                          you the best advice we can. If this comes across as "You must do
                          things this way because you'll go to Hell if you don't!", please feel
                          free to ask for clarification. There's almost always a valid reason
                          for the advice we give, but we don't always necessarily make that
                          sufficiently clear.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Kenneth Brody

                            #43
                            Re: what is difference between sizeof and strlen

                            Anton Petrusevich wrote:[color=blue]
                            >
                            > Richard Tobin wrote:
                            >[color=green]
                            > > "Undefined behaviour" is a technical term in the C standard. It's not
                            > > really relevant whether *you* would call it that.[/color]
                            >
                            > I don't get it. I thought, and I still continue to think, that "undefined
                            > behaviour" means just that: when the behaviour is undefined. If the
                            > behaviour is dependent on some defined thing (platform) then it's
                            > "defined". You (or I) just need to know the platform. Of course, there's no
                            > such (defined) thing as "platform" in C standard, so they say "undefined
                            > behaviour". "int main()" is a tough religion, isn't it?[/color]

                            If the standard says that the implementation needs to define something
                            (for example, the size of an int), then it's "implementa tion defined".
                            If the standard places no constraints whatsoever (such as "i = i++")
                            then it's "undefined behavior".

                            Yes, a particular implementation may define what the "undefined behavior"
                            is on that aprticular implementation, but it's still undefined as far as
                            the standard goes.

                            Yes, the odds are that a given implementation' s actions and a piece of
                            code that invokes undefined behavior will probably be consistent, even
                            if the implementors don't define it. But nothing requires it to be,
                            and it may change in the next release, even if it's a minor update,
                            like an update from 5.1.234.98 to 5.1.234.99 release. (Even an
                            implementation defined behavior can change, but it's not likely to
                            change in anything but a major version upgrad.)

                            For example:

                            #include <stdio.h>

                            int main(int argc,char *argv[])
                            {
                            float a, b, c;

                            c = a / b;

                            printf("%ld\n", c);

                            exit(EXIT_SUCCE SS);
                            }

                            It may be that the particular implementation ensures that you will
                            get some sort of "divide by zero" abort. On the other hand, many
                            platforms are likely to give you "random" results, depending on
                            what happens to be in the memory used for the "a" and "b" floats.
                            Even if the platform has well-defined behavior for divide-by-zero,
                            floating point over/underflow, and so on, the only "definition " to
                            the behavior would begin "if the memory happens to contain..."

                            --
                            +-------------------------+--------------------+-----------------------------+
                            | Kenneth J. Brody | www.hvcomputer.com | |
                            | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                            +-------------------------+--------------------+-----------------------------+
                            Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                            Comment

                            • Kenny McCormack

                              #44
                              Re: what is difference between sizeof and strlen

                              In article <dd7v3j$265l$3@ pc-news.cogsci.ed. ac.uk>,
                              Richard Tobin <richard@cogsci .ed.ac.uk> wrote:[color=blue]
                              >In article <dd7sp7$ukj$01$ 1@news.t-online.com>,
                              >Anton Petrusevich <casus@att-ltd.biz> wrote:[color=green]
                              >>I would not call it "undefined behaviour"[/color]
                              >
                              >"Undefined behaviour" is a technical term in the C standard. It's not
                              >really relevant whether *you* would call it that.
                              >
                              >-- Richard[/color]

                              Probably not relevant to *you* (Richard), but certainly relevant to Anton.

                              I'd wager, in fact, that whether or not Richard thinks something is
                              "undefined behaviour" is about as relevant to Anton as is whether or not
                              Anton thinks something is "undefined behaviour" is to Richard.

                              Comment

                              • Kenny McCormack

                                #45
                                Re: what is difference between sizeof and strlen

                                In article <gsMJe.5212$ns. 55@newsread1.ne ws.atl.earthlin k.net>,
                                Martin Ambuhl <mambuhl@earthl ink.net> wrote:[color=blue]
                                >Anton Petrusevich wrote:
                                >[color=green]
                                >> "int main()" is a tough religion, isn't it?[/color]
                                >
                                >How long are you going to keep this crap up? Soon your pseudo-anarchism
                                >and childish name-calling will land you in multiple killfiles.[/color]

                                Define "pseudo-anarchism" in this context. In the sense that you are using
                                it, how is it different from (real) anarchism? Why the distinction?

                                Comment

                                Working...