Name to method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • superheathen@yahoo.ca

    Name to method?

    Hi

    I'm reading from a database that stores information as an integer
    representing a char array of ints, it is created in the following
    way:

    unsigned char examplearray[] = {4, 3, 2, 1};
    unsigned int exampleint = *(unsigned int *)examplearray;

    and back again using:

    unsigned char examplearray2[4];
    *(unsigned int*)examplearr ay2 = exampleint;

    it works, I just have no clue how it works. Does this technique have a
    name so I can look into it?
  • Walter Roberson

    #2
    Re: Name to method?

    In article <0cd27cba-0ba4-4163-906c-a25953bf50b5@u1 0g2000prn.googl egroups.com>,
    <superheathen@y ahoo.cawrote:
    >I'm reading from a database that stores information as an integer
    >representing a char array of ints, it is created in the following
    >way:
    >unsigned char examplearray[] = {4, 3, 2, 1};
    >unsigned int exampleint = *(unsigned int *)examplearray;
    >and back again using:
    >unsigned char examplearray2[4];
    >*(unsigned int*)examplearr ay2 = exampleint;
    >it works, I just have no clue how it works. Does this technique have a
    >name so I can look into it?
    It is sometimes called "type punning".
    --
    "Pray do not take the pains / To set me right. /
    In vain my faults ye quote; / I wrote as others wrote /
    On Sunium's hight." -- Walter Savage Landor

    Comment

    • superheathen@yahoo.ca

      #3
      Re: Name to method?

      i'm still suck.

      Maybe someone can explain the math in the (de)conversion?

      Comment

      • ts.death.angel@gmail.com

        #4
        Re: Name to method?

        On Feb 11, 1:44 am, superheat...@ya hoo.ca wrote:
        i'm still suck.
        >
        Maybe someone can explain the math in the (de)conversion?
        There's not really much math involved, you're just swapping around
        pointers. Here's a rewrite that may be easier to understand:
        unsigned char examplearray[] = {4, 3, 2, 1};
        unsigned int *pointerint = examplearray; /* pointerint points to
        examplearray (which is 32-bits; the size of an int) */
        unsigned int exampleint = *pointerint; /* sets new int exampleint to
        what pointerint points to */

        Comment

        • superheathen@yahoo.ca

          #5
          Re: Name to method?

          On Feb 10, 4:59 pm, ts.death.an...@ gmail.com wrote:
          On Feb 11, 1:44 am, superheat...@ya hoo.ca wrote:
          >
          i'm still suck.
          >
          Maybe someone can explain the math in the (de)conversion?
          >
          There's not really much math involved, you're just swapping around
          pointers. Here's a rewrite that may be easier to understand:
          unsigned char examplearray[] = {4, 3, 2, 1};
          unsigned int *pointerint = examplearray; /* pointerint points to
          examplearray (which is 32-bits; the size of an int) */
          unsigned int exampleint = *pointerint; /* sets new int exampleint to
          what pointerint points to */
          There's got to be a little, in the example I provided the integer
          isn't a pointer or array. (though I tried using the example you
          provided and got:
          warning: initialization from incompatible pointer type) Using some
          casting , it somehow takes the array and converts it to 16909060 (how
          does it get this number?) and then using 16909060 is able to
          reconstruct the array. To me it'd make more sense if it did use
          pointers instead of the casting.

          Sorry if I'm too dense to get what you're getting at.

          Comment

          • superheathen@yahoo.ca

            #6
            Re: Name to method?

            On Feb 10, 5:27 pm, superheat...@ya hoo.ca wrote:
            On Feb 10, 4:59 pm, ts.death.an...@ gmail.com wrote:On Feb 11, 1:44 am, superheat...@ya hoo.ca wrote:
            >
            i'm still suck.
            >
            Maybe someone can explain the math in the (de)conversion?
            >
            There's not really much math involved, you're just swapping around
            pointers. Here's a rewrite that may be easier to understand:
            unsigned char examplearray[] = {4, 3, 2, 1};
            unsigned int *pointerint = examplearray; /* pointerint points to
            examplearray (which is 32-bits; the size of an int) */
            unsigned int exampleint = *pointerint; /* sets new int exampleint to
            what pointerint points to */
            >
            There's got to be a little, in the example I provided the integer
            isn't a pointer or array. (though I tried using the example you
            provided and got:
            warning: initialization from incompatible pointer type) Using some
            casting , it somehow takes the array and converts it to 16909060 (how
            does it get this number?) and then using 16909060 is able to
            reconstruct the array. To me it'd make more sense if it did use
            pointers instead of the casting.
            >
            Sorry if I'm too dense to get what you're getting at.
            also, if it were some sort of memory location, wouldn't it be
            subjected to change each compile, rendering it unable to read the
            database?

            Comment

            • Jack Klein

              #7
              Re: Name to method?

              On Sun, 10 Feb 2008 16:02:36 -0800 (PST), superheathen@ya hoo.ca wrote
              in comp.lang.c:
              Hi
              >
              I'm reading from a database that stores information as an integer
              representing a char array of ints, it is created in the following
              way:
              >
              unsigned char examplearray[] = {4, 3, 2, 1};
              unsigned int exampleint = *(unsigned int *)examplearray;
              >
              and back again using:
              >
              unsigned char examplearray2[4];
              *(unsigned int*)examplearr ay2 = exampleint;
              >
              it works, I just have no clue how it works. Does this technique have a
              name so I can look into it?
              It might happen to "work" for your expectation of "work" on the
              particular platform where you are using it. The C standard makes no
              such guarantee, because the behavior is undefined. On some platforms,
              if "examplearr ay" does not have the proper alignment, trying to access
              it as an unsigned int will generate a hardware trap.

              What you have here is an example of poorly written code by a
              programmer who isn't anywhere near as knowledgeable as he/she thinks.

              --
              Jack Klein
              Home: http://JK-Technology.Com
              FAQs for
              comp.lang.c http://c-faq.com/
              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
              alt.comp.lang.l earn.c-c++

              Comment

              • Kenny McCormack

                #8
                Re: Name to method?

                In article <1aivq312kd1ofj 0vg324232s2fqsa c4c6l@4ax.com>,
                Jack Klein <jackklein@spam cop.netblathere d:
                ....
                >What you have here is an example of poorly written code by a
                >programmer who isn't anywhere near as knowledgeable as he/she thinks.
                Yeah, that guy, Linus Torvalds, a real idiot. Probably lost his first
                (and only) programming job - probably homeless and out on the street by now.

                Yeah, I hear he used to do a lot of that sort of thing - type punning,
                and god knows what else.

                Comment

                Working...