Returning int pointer ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Ahern

    #16
    Re: Returning int pointer ?

    s0suk3@gmail.co m wrote:
    On Oct 18, 2:41?pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
    wrote:
    Mahendra wrote:
    I have following code snippet -
    int *createIncidenc eMatrix(int numEdges, int numVertices) {
    ? ? int *p = (int *) calloc((numEdge s*numVertices), sizeof(int));
    Don't cast the results of memory allocation functions.
    Why specifically memory allocation functions? That argument would
    apply to any function that returns a void *. For example, you wouldn't
    want to cast a memcpy() call either, simply because it's not
    necessary. However, you're probably just saying it because of
    comp.lang.c's old myth that malloc() shouldn't be cast (for whatever
    reason).
    Old myth? I _continue_ to see newly written code that crashes because it was
    developed on a system w/ 32-bit pointers and somebody--the same person or
    co-worker--compiles it on a system where pointers are 64-bits, and the
    original author forgot to include stdlib.h.

    I fail to see how a rule which is more-or-less "let the computer do the work
    for you" could possibly be reasonably chastized generally. It's the epitome
    of good sense, and consonant with the entire purpose of compilers; indeed,
    computers in general!

    The same goes for recommending `sizeof object' rather than `sizeof (type)',
    although this latter style is less bug prone in practice than casting
    malloc().

    In my experience, those who cast malloc() are C++ programmers. One of the
    defining distinctions between C and C++ is that C++ has stronger typing.
    Casting malloc() betrays poor comprehension of the C type system.

    Comment

    • Peter Nilsson

      #17
      Re: Returning int pointer ?

      Richard Heathfield <r...@see.sig.i nvalidwrote:
      Malcolm McLean said:
      "Richard Heathfield" <r...@see.sig.i nvalidwrote
      The number of people who actually need to do that
      is vanishingly small -
      In the same way that C90 programmers are vanishingly small,
      but there's still _LOTS_ of them.
      as far as I know, only two people in this group (P J
      Plauger and Ian Collins) have ever put forward a reason
      for writing such code that bears more than ten seconds
      scrutiny,
      It may be more accurate to say you never gave the reasons
      more than ten seconds scrutiny until they where the ones
      saying it.
      and even then not everyone is convinced that those
      reasons are necessarily good ones.
      To my knowledge, neither of them have asked you to be
      convinced. The irony for me is that Ian's reasons are the
      least convincing I've heard. Of course, I don't have to be
      convinced by them. I just understand them.
      If you use both C and C++ it often useful to be able to
      snip and paste functions between two source files.
      >
      It's more useful to compile your C functions with a
      C compiler, put them into a C library, and link them to
      your C++ program in the canonical way.
      What is the canonical way? The C++ standard doesn't state
      you can link any and all C object files. That's one of the
      reasons why many decided to hang the sense of it and make
      their C code compilable as C++.

      Plauger went to far as to have the linkage of the C
      standard library implementation defined, precisely so he
      (and other implementors) could have their C standard
      library compilable as C++.

      --
      Peter

      Comment

      • Richard Heathfield

        #18
        Re: Returning int pointer ?

        Peter Nilsson said:
        Richard Heathfield <r...@see.sig.i nvalidwrote:
        >Malcolm McLean said:
        "Richard Heathfield" <r...@see.sig.i nvalidwrote
        The number of people who actually need to do that
        is vanishingly small -
        >
        In the same way that C90 programmers are vanishingly small,
        but there's still _LOTS_ of them.
        >
        as far as I know, only two people in this group (P J
        Plauger and Ian Collins) have ever put forward a reason
        for writing such code that bears more than ten seconds
        scrutiny,
        >
        It may be more accurate to say you never gave the reasons
        more than ten seconds scrutiny until they where the ones
        saying it.
        Um, no, it would be more accurate to say that I know all (or at least many)
        of the dodgy reasons because I used to believe them myself. Plauger and
        Collins were the first two (and the only two, in getting on for a decade
        of clc articles) who could actually come up with a reason that I hadn't
        already examined and discarded.

        and even then not everyone is convinced that those
        reasons are necessarily good ones.
        >
        To my knowledge, neither of them have asked you to be
        convinced.
        I know that. Nevertheless, they posted them publicly, so why not discuss
        them publicly?
        The irony for me is that Ian's reasons are the
        least convincing I've heard. Of course, I don't have to be
        convinced by them. I just understand them.
        I *think* I understand Ian's reason. To me, it flies in the face of good
        testing practice, however, so personally I'm not convinced - but it was
        the first reason I've ever heard of (apart from Plauger's) that wasn't one
        I'd already considered and rejected.
        If you use both C and C++ it often useful to be able to
        snip and paste functions between two source files.
        >>
        >It's more useful to compile your C functions with a
        >C compiler, put them into a C library, and link them to
        >your C++ program in the canonical way.
        >
        What is the canonical way?
        All right, *a* canonical way. Or possibly apocryphal.
        The C++ standard doesn't state
        you can link any and all C object files. That's one of the
        reasons why many decided to hang the sense of it and make
        their C code compilable as C++.
        Perhaps I'm betraying my ignorance, then, but I never had a problem linking
        C to C++ (which I've done a great many times).
        Plauger went to far as to have the linkage of the C
        standard library implementation defined, precisely so he
        (and other implementors) could have their C standard
        library compilable as C++.
        <shrug>

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Ian Collins

          #19
          Re: Returning int pointer ?

          Richard Heathfield wrote:
          Peter Nilsson said:
          >
          >The irony for me is that Ian's reasons are the
          >least convincing I've heard. Of course, I don't have to be
          >convinced by them. I just understand them.
          >
          I *think* I understand Ian's reason. To me, it flies in the face of good
          testing practice, however, so personally I'm not convinced - but it was
          the first reason I've ever heard of (apart from Plauger's) that wasn't one
          I'd already considered and rejected.
          >
          One has to differentiate between simulation and testing.

          I tend to work with complex real-time systems with many components, each
          with its own software. One example being DC power systems. The system
          comprises rectifiers with their control software and the system
          controllers with their software.

          The rectifier designers like to model the rectifier's control circuits
          and the software bods like to verify the *dynamic* behaviour of the
          control software, often before the hardware is available. Even with
          real hardware, very loud and smelly things can happen if the software
          and hardware don't get along.

          The system designers like to model the *dynamic* behaviour of the system
          as governed by the controller software.

          So we have layers of simulations at each stage.

          Being able to simulate hardware before it is built reduces the all
          important time to market.

          --
          Ian Collins

          Comment

          • Malcolm McLean

            #20
            Re: Returning int pointer ?

            "Richard Heathfield" <rjh@see.sig.in validwrote in message
            Malcolm McLean said:
            >
            >Often you want smallish functions to be static (file scope) to avoid
            >creating dependencies between files.
            >
            At the expense of having code duplicated all over the shop. I'll take the
            dependencies any day.
            >
            It depends on the size and complexity of the function. It's very convenient
            to have a "leaf" source file, that is to say one that doesn't depend on
            anything other than maybe some standard libraries.
            One problem is that the #include directive makes C source sticky. You want a
            trivial function, but if it is in a file that #includes another header,
            pretty soon you are copying over large chunks of souercecode, merely to call
            the trivial function.
            >
            >Or you are writing a small one or
            >two file program in C++ and want to include a function for it. Or you
            >might need boilerplate code in which a few of the constants etc might
            >need to be
            >dited
            >
            Edited? Well, that's why generality is so important.
            >
            Let's give an example, the windows RegisterClassEx fucntion. It takes
            several arguements, including a message handler, or callback function. Now
            it seldom makes sense to export this message handler, it is called
            internally by the program. The function also takes cursors, styles,
            background brushes, icons, window names, most of which you only use
            occasionally. Specifying everything as a parameter makes the high-level
            fucntion too complex to call, and in any case the callback can't be passed
            in if it is private.
            So boilerplate code is the answer.
            >- I'm thinking particularly of Windows programming where you need
            >repetitive code to register Window types, open dialogues, and the like,
            >with just a few of the parameters changed to meet your needs.
            >
            Functions rock. Parameterisatio n makes them rock even more.
            >
            But you can't parameterise everything. Or we'd just need one function.

            int cinterpreter(ch ar *source);


            --
            Free games and programming goodies.



            Comment

            • Richard Heathfield

              #21
              Re: Returning int pointer ?

              Malcolm McLean said:
              "Richard Heathfield" <rjh@see.sig.in validwrote in message
              >Malcolm McLean said:
              >>
              >>Often you want smallish functions to be static (file scope) to avoid
              >>creating dependencies between files.
              >>
              >At the expense of having code duplicated all over the shop. I'll take
              >the dependencies any day.
              >>
              It depends on the size and complexity of the function. It's very
              convenient to have a "leaf" source file, that is to say one that doesn't
              depend on anything other than maybe some standard libraries.
              Yes, of course - and I would guess that most clc-ers are well used to such
              source.
              One problem is that the #include directive makes C source sticky. You
              want a
              trivial function, but if it is in a file that #includes another header,
              pretty soon you are copying over large chunks of souercecode, merely to
              call the trivial function.
              Your way, yes. My way, you just link in the appropriate library or
              libraries. No copying involved.
              >>Or you are writing a small one or
              >>two file program in C++ and want to include a function for it. Or you
              >>might need boilerplate code in which a few of the constants etc might
              >>need to be
              >>dited
              >>
              >Edited? Well, that's why generality is so important.
              >>
              Let's give an example, the windows RegisterClassEx fucntion. It takes
              several arguements, including a message handler, or callback function.
              Now it seldom makes sense to export this message handler, it is called
              internally by the program. The function also takes cursors, styles,
              background brushes, icons, window names, most of which you only use
              occasionally. Specifying everything as a parameter makes the high-level
              fucntion too complex to call, and in any case the callback can't be
              passed in if it is private.
              So boilerplate code is the answer.
              No, a struct is the answer.

              <snip>

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Malcolm McLean

                #22
                Re: Returning int pointer ?


                "Richard Heathfield" <rjh@see.sig.in validwrote in message
                Malcolm McLean said:
                >
                >One problem is that the #include directive makes C source sticky. You
                >want a
                >trivial function, but if it is in a file that #includes another header,
                >pretty soon you are copying over large chunks of souercecode, merely to
                >call the trivial function.
                >
                Your way, yes. My way, you just link in the appropriate library or
                libraries. No copying involved.
                >
                Then you make the C source dependent on not just another source, but on a
                whole library. So the code is useless to anyone who doesn't want to link
                that library.
                >
                >So boilerplate code is the answer.
                >
                No, a struct is the answer.
                >
                You're kind of right. If a fucntion needs a huge number of parameters it is
                better to wrap them up in a struct than try to pass them as independent
                parameters.
                However this doesn't really solve the problem. You;'ve got to dig out the
                struct documentention. Then each field of the struct is assigned to in a
                different line of the caller. So effectively you need a "wrapper" caller
                fucntion to call the struct function, and you're back to boilerplate code
                again.
                Very few people write GUI code like Windows programs from scratch. Typically
                you modify another program, or use some tool that generate source for you.
                This is distinctly non-ideal, but no-one has yet produced the easy to
                program GUI.

                --
                Free games and programming goodies.


                Comment

                • Richard Heathfield

                  #23
                  Re: Returning int pointer ?

                  Malcolm McLean said:
                  >
                  "Richard Heathfield" <rjh@see.sig.in validwrote in message
                  >Malcolm McLean said:
                  >>
                  >>One problem is that the #include directive makes C source sticky. You
                  >>want a
                  >>trivial function, but if it is in a file that #includes another
                  >>header, pretty soon you are copying over large chunks of souercecode,
                  >>merely to call the trivial function.
                  >>
                  >Your way, yes. My way, you just link in the appropriate library or
                  >libraries. No copying involved.
                  >>
                  Then you make the C source dependent on not just another source, but on a
                  whole library. So the code is useless to anyone who doesn't want to link
                  that library.
                  Sorry, you lost me. Either you need the functionality or you don't. If you
                  don't need it, don't use it. The issue, for those who do need it, is
                  whether you recompile that source over and over, every time, or whether
                  you compile it *once* and simply link it every time.

                  <snip>
                  This is distinctly non-ideal, but no-one has yet produced
                  the easy to program GUI.
                  I guess you've never come across C++ Builder, then.

                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999

                  Comment

                  • Malcolm McLean

                    #24
                    Re: Returning int pointer ?


                    "Richard Heathfield" <rjh@see.sig.in validwrote in message
                    Malcolm McLean said:
                    >
                    >Then you make the C source dependent on not just another source, but on a
                    >whole library. So the code is useless to anyone who doesn't want to link
                    >that library.
                    >
                    Sorry, you lost me. Either you need the functionality or you don't. If you
                    don't need it, don't use it. The issue, for those who do need it, is
                    whether you recompile that source over and over, every time, or whether
                    you compile it *once* and simply link it every time.
                    >
                    One does not "simply" link it. It's simple enough if the program only needs
                    to be compiled on your personal machine. But if you are called upon to
                    provide source, you need the source of the library, maybe just for one
                    function. Even worse, libraries are sticky, #including headers from other
                    libraries. You can end up providing tens of C source files, just to get a
                    defintion on "Point3d", if you're not careful.
                    That's why sensible programmers always try to keep the dependency tree down.
                    If something can be a leaf source, make it leaf, even if that means cut and
                    pasting a few functions into it.
                    >
                    >This is distinctly non-ideal, but no-one has yet produced
                    >the easy to program GUI.
                    >
                    I guess you've never come across C++ Builder, then.
                    >
                    I'm not familiar with the program. However typically these sorts of packages
                    come with graphical editing tools that spit out C source / some sort of
                    configuration file. Essentially that is an admission that the C interface to
                    the GUI is too complicated for the human programmer.
                    I've got mixed feelings about such tools. Whilst point and clicking is
                    easier than typing text, you've lost a layer of control.

                    --
                    Free games and programming goodies.


                    Comment

                    • Richard Heathfield

                      #25
                      Re: Returning int pointer ?

                      Malcolm McLean said:
                      >
                      "Richard Heathfield" <rjh@see.sig.in validwrote in message
                      >Malcolm McLean said:
                      >>
                      >>Then you make the C source dependent on not just another source, but on
                      >>a whole library. So the code is useless to anyone who doesn't want to
                      >>link that library.
                      >>
                      >Sorry, you lost me. Either you need the functionality or you don't. If
                      >you don't need it, don't use it. The issue, for those who do need it, is
                      >whether you recompile that source over and over, every time, or whether
                      >you compile it *once* and simply link it every time.
                      >>
                      One does not "simply" link it.
                      Er, yes, one does.
                      It's simple enough if the program only
                      needs to be compiled on your personal machine. But if you are called upon
                      to provide source, you need the source of the library, maybe just for one
                      function.
                      If the function can be built into a library all on its own, there's your
                      answer. If not, i.e. if it depends on the functionality of the remainder
                      of the library, then you're going to need to link the remainder of the
                      library too. You can't get something for nothing. That isn't a problem;
                      it's just reality. And if you have to provide source, okay, you have to
                      provide source. But if you have to provide source, presumably people are
                      going to read it, and you're going to look a right 'nana if you've got
                      copied-and-pasted bits all over the place.
                      Even worse, libraries are sticky, #including headers from other
                      libraries.
                      Again, that's not "even worse" - it's simply the reality of building
                      complex components out of simple components. If the function you're
                      providing *needs* all that stuff underneath it, then it needs all that
                      stuff underneath it and there's nothing you can do about it. And if it
                      doesn't, fine, chop the extra stuff out.
                      That's why sensible programmers always try to keep the dependency tree
                      down. If something can be a leaf source, make it leaf, even if that means
                      cut and pasting a few functions into it.
                      I disagree that this is sensible advice. Duplicated source is such stuff as
                      maintenance nightmares are made on.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Malcolm McLean

                        #26
                        Re: Returning int pointer ?

                        "Richard Heathfield" <rjh@see.sig.in validwrote in message news:
                        Malcolm McLean said:
                        >
                        >That's why sensible programmers always try to keep the dependency tree
                        >down. If something can be a leaf source, make it leaf, even if that means
                        >cut and pasting a few functions into it.
                        >
                        I disagree that this is sensible advice. Duplicated source is such stuff
                        as
                        maintenance nightmares are made on.
                        >
                        Take this:
                        Several functions in our library call mystrdup() to duplicate a string. The
                        code is pretty tivial.
                        Now one question is whether it is sensible to have a library with only one
                        book, or in this case, only one pamphlet. If we go thsi route we ahve a
                        library called mystrdup.lib, a header, my mystrdup.h, and a C source file
                        mystrdup.c. Every module #includes mystrdup.h, and we link the library at
                        compile time.

                        The alternative is to have several functions in the library. So let's say we
                        also have a function

                        int writeasciiz(cha r *str, FILE *fp)

                        This can go into the same library as mystrdup. However now we've created a
                        dependency on stdio.h. So nothing that calls mystrdup() can now be used in
                        an environment where there is no stdio.

                        Let's say we've got another function

                        int myisvowel(int ch)

                        Some bright spark decides that myisvowel returns a boolean. Let's doccument
                        this. In mystringlib.h
                        typedef int boolean;

                        boolean myisvowel(int ch)

                        disaster! Now everything that includes that header has a definition for a
                        boolean type in scope.
                        OK, we decide that mystringlib.h isn't the right place for the boolean
                        defintion. So we define mytypes.h

                        typedef int boolean;

                        and in mystringlib.h

                        #include "mytypes.h"

                        This is what I mean about C being sticky. You've ended up with a nightmare,
                        just to get one measly little string duplication function.

                        Finally terrible news comes in. We've implemented writeasciiz as

                        int writeasciiz(cha r *str, FILE *fp)
                        {
                        while(*str)
                        fputc(*str++, fp);
                        fputc(0, fp);
                        return ferror(fp) ? EOF : 0;
                        }

                        Of course this won't cut the mustard when we move to a non-ascii machine.

                        Fortunately there's a function alreadty written for us

                        int mytoascii(char ch)

                        so we can hack into writeasciiz to call mytoascii. But mystring.lib is now
                        dependent on non_ascii_suppo rt.lib.
                        Even worse, mytoascii() is a little bit slow, as some callers of writeasciiz
                        complain about the reduced perfomance.

                        --
                        Free games and programming goodies.



                        Comment

                        • Richard Heathfield

                          #27
                          Re: Returning int pointer ?

                          Malcolm McLean said:
                          "Richard Heathfield" <rjh@see.sig.in validwrote in message news:
                          >Malcolm McLean said:
                          >>
                          >>That's why sensible programmers always try to keep the dependency tree
                          >>down. If something can be a leaf source, make it leaf, even if that
                          >>means cut and pasting a few functions into it.
                          >>
                          >I disagree that this is sensible advice. Duplicated source is such stuff
                          >as maintenance nightmares are made on.
                          >>
                          Take this:
                          Several functions in our library call mystrdup() to duplicate a string.
                          The code is pretty tivial.
                          Now one question is whether it is sensible to have a library with only
                          one book, or in this case, only one pamphlet.
                          The great thing about libraries is that you can stuff all kinds of junk in
                          there and, if you're careful, at link time the only stuff you'll have
                          linked to your program is the stuff your program actually needs.
                          If we go thsi route we ahve
                          a library called mystrdup.lib, a header, my mystrdup.h, and a C source
                          file mystrdup.c. Every module #includes mystrdup.h, and we link the
                          library at compile time.
                          Well, link time, but yes.
                          >
                          The alternative is to have several functions in the library. So let's say
                          we also have a function
                          >
                          int writeasciiz(cha r *str, FILE *fp)
                          >
                          This can go into the same library as mystrdup. However now we've created
                          a dependency on stdio.h. So nothing that calls mystrdup() can now be used
                          in an environment where there is no stdio.
                          Firstly, do you really want to put a string-duplicating routine in the same
                          library as a string-displaying routine? (Maybe you do, if it's a string
                          library.)

                          Secondly, if writeasciiz is in a separate source file to mystrdup, a good
                          linker will be able to link mystrdup without linking writeasciiz, so the
                          dependency you mention will only be created if it is needed. If it's
                          needed, it's needed, and you'd better have a stdio, not because the
                          library needs it but because the application does (otherwise it wouldn't
                          be calling writeasciiz).
                          Let's say we've got another function
                          >
                          int myisvowel(int ch)
                          >
                          Some bright spark decides that myisvowel returns a boolean. Let's
                          doccument this. In mystringlib.h
                          typedef int boolean;
                          >
                          boolean myisvowel(int ch)
                          >
                          disaster!
                          Why are you putting an int-handling routine in a string library?
                          Now everything that includes that header has a definition for a
                          boolean type in scope.
                          OK, we decide that mystringlib.h isn't the right place for the boolean
                          defintion.
                          Nor is libmystring.a!

                          So we define mytypes.h
                          >
                          typedef int boolean;
                          >
                          and in mystringlib.h
                          >
                          #include "mytypes.h"
                          >
                          This is what I mean about C being sticky. You've ended up with a
                          nightmare, just to get one measly little string duplication function.
                          No, you've created the nightmare through poor design, poor modularisation,
                          and an overly simplistic attitude towards libraries.
                          >
                          Finally terrible news comes in. We've implemented writeasciiz as
                          >
                          int writeasciiz(cha r *str, FILE *fp)
                          {
                          while(*str)
                          fputc(*str++, fp);
                          fputc(0, fp);
                          return ferror(fp) ? EOF : 0;
                          }
                          >
                          Of course this won't cut the mustard when we move to a non-ascii machine.
                          So write a portable version!
                          Fortunately there's a function alreadty written for us
                          >
                          int mytoascii(char ch)
                          >
                          so we can hack into writeasciiz to call mytoascii. But mystring.lib is
                          now dependent on non_ascii_suppo rt.lib.
                          It is certainly possible to create an overly complex situation using
                          libraries. But you don't need libraries for that! All you need is to adopt
                          a "never go back" philosophy such as the one you've been espousing.
                          Even worse, mytoascii() is a little bit slow, as some callers of
                          writeasciiz complain about the reduced perfomance.
                          Words fail me. And this is me saying it.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • CBFalconer

                            #28
                            Re: Returning int pointer ?

                            Malcolm McLean wrote:
                            >
                            .... snip ...
                            >
                            Finally terrible news comes in. We've implemented writeasciiz as
                            >
                            int writeasciiz(cha r *str, FILE *fp) {
                            while (*str)
                            fputc(*str++, fp);
                            fputc(0, fp);
                            return ferror(fp) ? EOF : 0;
                            }
                            >
                            Of course this won't cut the mustard when we move to a non-ascii
                            machine.
                            Why not? The only dependency is on '\0' being a string end marker.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.

                            Comment

                            • Richard Heathfield

                              #29
                              Re: Returning int pointer ?

                              CBFalconer said:
                              Malcolm McLean wrote:
                              >>
                              ... snip ...
                              >>
                              >Finally terrible news comes in. We've implemented writeasciiz as
                              >>
                              >int writeasciiz(cha r *str, FILE *fp) {
                              > while (*str)
                              > fputc(*str++, fp);
                              > fputc(0, fp);
                              > return ferror(fp) ? EOF : 0;
                              >}
                              >>
                              >Of course this won't cut the mustard when we move to a non-ascii
                              >machine.
                              >
                              Why not?
                              Because on a non-ASCII machine, it won't write ASCII. It will write the
                              native character set. For example, on a typical IBM mainframe using
                              EBCDIC, if you call it thusly:

                              writeasciiz("He llo", stdout);

                              instead of getting the required output - byte values 72, 101, 108, 108,
                              111, and 0 - written to the stream, you'll get 200, 133, 147, 147, 150,
                              and 0.

                              The function writes, not ASCII, but native. If native is ASCII, great. But
                              if native is non-ASCII, then the function doesn't do what it says on the
                              tin.

                              The only dependency is on '\0' being a string end marker.
                              No, that bit is portable. The Standard says that '\0' marks the end of a
                              string.

                              --
                              Richard Heathfield <http://www.cpax.org.uk >
                              Email: -http://www. +rjh@
                              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                              "Usenet is a strange place" - dmr 29 July 1999

                              Comment

                              • Keith Thompson

                                #30
                                Re: Returning int pointer ?

                                Richard Heathfield <rjh@see.sig.in validwrites:
                                CBFalconer said:
                                >Malcolm McLean wrote:
                                >>>
                                >... snip ...
                                >>>
                                >>Finally terrible news comes in. We've implemented writeasciiz as
                                >>>
                                >>int writeasciiz(cha r *str, FILE *fp) {
                                >> while (*str)
                                >> fputc(*str++, fp);
                                >> fputc(0, fp);
                                >> return ferror(fp) ? EOF : 0;
                                >>}
                                >>>
                                >>Of course this won't cut the mustard when we move to a non-ascii
                                >>machine.
                                >>
                                >Why not?
                                >
                                Because on a non-ASCII machine, it won't write ASCII. It will write the
                                native character set.
                                It will write whatever you ask it to write.
                                For example, on a typical IBM mainframe using
                                EBCDIC, if you call it thusly:
                                >
                                writeasciiz("He llo", stdout);
                                >
                                instead of getting the required output - byte values 72, 101, 108, 108,
                                111, and 0 - written to the stream, you'll get 200, 133, 147, 147, 150,
                                and 0.
                                And if you call it thusly:

                                char hello[] = { 72, 101, 108, 108, 111, 0);
                                writeasciiz(hel lo, stdout);

                                it will write "Hello" in ASCII.
                                The function writes, not ASCII, but native. If native is ASCII, great. But
                                if native is non-ASCII, then the function doesn't do what it says on the
                                tin.
                                It writes native only if you ask it to write native.

                                I admit that, given the name, confusion is likely.

                                [...]

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

                                Comment

                                Working...