p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])

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

    p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])

    what's this?
  • Richard Heathfield

    #2
    Re: p=(char *)malloc((sizeo f(float))["\000\006\ 010\013\015\100 "])

    fuzhen said:
    what's this?
    Checking the subject line, I guess you are referring to this:

    p=(char *)malloc((sizeo f(float))["\000\006\010\0 13\015\100"])

    to which the answer is that it's a badly written call to malloc. But, given
    suitable furniture (a function wrapped around it, <stdlib.h>, etc - and a
    semicolon wouldn't go amiss at the end there), it's perfectly legal, provided
    sizeof(float) doesn't exceed 6 on the target system. On systems where it
    does, the behaviour is undefined.

    Let's start off by assuming sizeof(float) is 3 - which is a perfectly legal
    value for sizeof(float), and has the merit of being a little unlikely, to say
    the least.

    Thus, given that assumption, the code reduces to:

    p=(char *)malloc((3)["\000\006\010\0 13\015\100"])

    and (3) is just 3, so that gives us:

    p=(char *)malloc(3["\000\006\010\0 13\015\100"])

    Now, a[i] and *(a + i) are guaranteed to be equivalent, so let's do some
    substituting:

    p=(char *)malloc(*(3+"\ 000\006\010\013 \015\100"))

    Okay, x+y is the same as y+x, so:

    p=(char *)malloc(*("\00 0\006\010\013\0 15\100"+3))

    and *(a + i) is the same as a[i], so:

    p=(char *)malloc("\000\ 006\010\013\015 \100"[3])

    Now, "\000\006\010\0 13\015\100" is an array with values { 0, 6, 8, 11, 13, 64,
    0 }, so "\000\006\010\0 13\015\100"[3] is element 3 of that array: element 0
    is 0, element 1 is 6, element 2 is 8, element 3 is 11. So we can substitute
    that back in again:

    p=(char *)malloc(11)

    and finally we can lose the spurious and utterly pointless cast, which gives:

    p = malloc(11)

    Is that sufficiently simple for you? (Remember to replace 11 with a different
    value from the array if sizeof(float) doesn't happen to be 3 on your system.)

    --
    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

    • MisterE

      #3
      Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])


      Is that sufficiently simple for you? (Remember to replace 11 with a
      different
      value from the array if sizeof(float) doesn't happen to be 3 on your
      system.)
      Does something like this have a legitimate use at all?


      Comment

      • Richard Heathfield

        #4
        Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

        MisterE said:
        >
        >
        >Is that sufficiently simple for you? (Remember to replace 11 with a
        >different
        >value from the array if sizeof(float) doesn't happen to be 3 on your
        >system.)
        >
        Does something like this have a legitimate use at all?
        No - confusing the newbies is about its limit, really.

        --
        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

        • Chris Dollin

          #5
          Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

          MisterE wrote:
          >
          >
          >Is that sufficiently simple for you? (Remember to replace 11 with a
          >different
          >value from the array if sizeof(float) doesn't happen to be 3 on your
          >system.)
          >
          Does something like this have a legitimate use at all?
          /Something/ like this, yes, depending on how close a similarity
          you insist on. `malloc` is, after all, a useful function.

          --
          "Never ask that question!" /Babylon 5/

          Hewlett-Packard Limited Cain Road, Bracknell, registered no:
          registered office: Berks RG12 1HN 690597 England

          Comment

          • =?iso-8859-1?Q?=D8yvind_R=F8tvold?=

            #6
            Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

            "MisterE" <MisterE@nimga. comwrites:
            >Is that sufficiently simple for you? (Remember to replace 11 with a
            >different
            >value from the array if sizeof(float) doesn't happen to be 3 on your
            >system.)
            >
            Does something like this have a legitimate use at all?
            This may save you a couple of bytes.

            Making it more readable by placing the table as a static before the
            call would probably not cost you any bytes, this would, however only
            work if this is used where you can have declarations, otherwise eg. in
            a macro, this may be a solution.

            The numbers look odd though, appearently only size 1 to 4 produces
            sensible values, what's the context here?

            --
            ... __o Øyvind
            ... _`\(, http://www.darkside.no/olr/
            ... (_)/(_) ... biciclare necesse est ...

            Comment

            • Sjouke Burry

              #7
              Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

              fuzhen wrote:
              what's this?
              Doodles from a sick programmer.

              Comment

              • Serve Lau

                #8
                Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])


                "fuzhen" <fucore@gmail.c omschreef in bericht
                news:b4f91191-dafe-4a63-84a7-82687fcfacfd@w3 4g2000prm.googl egroups.com...
                what's this?
                in what context was this used?

                Comment

                • Eric Sosman

                  #9
                  Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

                  Sjouke Burry wrote:
                  fuzhen wrote:
                  >what's this?
                  Doodles from a sick programmer.
                  Best answer I've seen.

                  --
                  Eric Sosman
                  esosman@ieee-dot-org.invalid

                  Comment

                  • rahul

                    #10
                    Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

                    On Jun 11, 4:30 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    fuzhen said:
                    >
                    what's this?
                    >
                    Checking the subject line, I guess you are referring to this:
                    >
                    p=(char *)malloc((sizeo f(float))["\000\006\010\0 13\015\100"])
                    >
                    to which the answer is that it's a badly written call to malloc. But, given
                    suitable furniture (a function wrapped around it, <stdlib.h>, etc - and a
                    semicolon wouldn't go amiss at the end there), it's perfectly legal, provided
                    sizeof(float) doesn't exceed 6 on the target system. On systems where it
                    does, the behaviour is undefined.
                    >
                    Let's start off by assuming sizeof(float) is 3 - which is a perfectly legal
                    value for sizeof(float), and has the merit of being a little unlikely, to say
                    the least.
                    >
                    Thus, given that assumption, the code reduces to:
                    >
                    p=(char *)malloc((3)["\000\006\010\0 13\015\100"])
                    >
                    and (3) is just 3, so that gives us:
                    >
                    p=(char *)malloc(3["\000\006\010\0 13\015\100"])
                    >
                    Now, a[i] and *(a + i) are guaranteed to be equivalent, so let's do some
                    substituting:
                    >
                    p=(char *)malloc(*(3+"\ 000\006\010\013 \015\100"))
                    >
                    Okay, x+y is the same as y+x, so:
                    >
                    p=(char *)malloc(*("\00 0\006\010\013\0 15\100"+3))
                    >
                    and *(a + i) is the same as a[i], so:
                    >
                    p=(char *)malloc("\000\ 006\010\013\015 \100"[3])
                    >
                    Now, "\000\006\010\0 13\015\100" is an array with values { 0, 6, 8, 11, 13, 64,
                    0 }, so "\000\006\010\0 13\015\100"[3] is element 3 of that array: element 0
                    is 0, element 1 is 6, element 2 is 8, element 3 is 11. So we can substitute
                    that back in again:
                    >
                    p=(char *)malloc(11)
                    >
                    and finally we can lose the spurious and utterly pointless cast, which gives:
                    >
                    p = malloc(11)
                    >
                    Is that sufficiently simple for you? (Remember to replace 11 with a different
                    value from the array if sizeof(float) doesn't happen to be 3 on your system.)
                    >
                    --
                    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
                    This is the first time I am seeing array literals declared in this
                    manner. This is a new addition in C99, isn't it? And this cryptic
                    thing is supposed to pass array literals ( just to quote one of the
                    possible scenarios).

                    Comment

                    • Peter Nilsson

                      #11
                      Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

                      rahul wrote:
                      Richard Heathfield <r...@see.sig.i nvalidwrote:
                      Checking the subject line, I guess you are referring to this:

                      p=(char *)malloc((sizeo f(float))["\000\006\010\0 13\015\100"])
                      <snip>
                      >
                      This is the first time I am seeing array literals declared in this
                      manner. This is a new addition in C99, isn't it?
                      No. Neither string literals nor a[i] == i[a] is new.

                      --
                      Peter

                      Comment

                      • rahul

                        #12
                        Re: p=(char *)malloc((sizeo f(float))[&quot;\000\006\ 010\013\015\100 &quot;])

                        On Jun 12, 10:07 am, Peter Nilsson <ai...@acay.com .auwrote:
                        rahul wrote:
                        Richard Heathfield <r...@see.sig.i nvalidwrote:
                        Checking the subject line, I guess you are referring to this:
                        >
                        p=(char *)malloc((sizeo f(float))["\000\006\010\0 13\015\100"])
                        <snip>
                        >
                        This is the first time I am seeing array literals declared in this
                        manner. This is a new addition in C99, isn't it?
                        >
                        No. Neither string literals nor a[i] == i[a] is new.
                        >
                        --
                        Peter
                        Thanks Peter,
                        I missed that part in the FAQ. I got my answer.
                        char *a = "hello";
                        printf ("%c", 2[a]);
                        The above snippet directly does
                        printf ("%c", 2["hello"]);

                        a[i] == i[a]. Hey...I knew that

                        Comment

                        Working...