Is Python type safe?

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

    #16
    Re: Is Python type safe?

    [color=blue][color=green]
    >> Sorry to follow up to my own post, but unions also create type
    >> unsafety.[/color][/color]

    Roy> Of course you can. Given C's roots as a high level assembly
    Roy> language, why would you expect anything else?

    Nope. But the user asked if C++ was type safe. The presence of unions
    indicates that it's not, at least in some situations.

    Roy> I was once asked on an interview how I would tell, inside of a C
    Roy> program, if I was on a big endian or a small endian machine. I
    Roy> said I'd create a union of a long and a char[4], set the long equal
    Roy> to 1, and see which char it showed up in. The interviewer looked
    Roy> shocked, thought about it for a while, and finally said something
    Roy> like, "yeah, I guess that would work".

    Sure. There are plenty of reasons to use unions. They can be a
    double-edged sword though.

    Skip

    Comment

    • Carl Banks

      #17
      Re: Is Python type safe?

      Roy Smith wrote:[color=blue]
      > I was once asked on an interview how I would tell, inside of a C
      > program, if I was on a big endian or a small endian machine. I said I'd
      > create a union of a long and a char[4], set the long equal to 1, and see
      > which char it showed up in. The interviewer looked shocked, thought
      > about it for a while, and finally said something like, "yeah, I guess
      > that would work".
      >
      > To this day, I have no idea what other plan he had in mind.[/color]


      Here's my guess:

      long i = 0;
      *(short*)&i = 1;

      if (i == 1) { printf("It's whatever one Intel uses.\n"); }
      else { printf("It's the other one.\n"); }


      --
      CARL BANKS http://www.aerojockey.com/software
      "If you believe in yourself, drink your school, stay on drugs, and
      don't do milk, you can get work."
      -- Parody of Mr. T from a Robert Smigel Cartoon

      Comment

      • Erik Max Francis

        #18
        Re: Is Python type safe?

        Carl Banks wrote:
        [color=blue]
        > Here's my guess:
        >
        > long i = 0;
        > *(short*)&i = 1;
        >
        > if (i == 1) { printf("It's whatever one Intel uses.\n"); }
        > else { printf("It's the other one.\n"); }[/color]

        You meant char, not short.

        And none of the solutions so far acknowledge the fact that none of these
        types have absolute sizes as defined in the Standard. If I were
        conducting the interview, I'd take points off if that weren't at least
        acknowledged.

        --
        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
        \__/ Love is when you wake up in the morning and have a big smile.
        -- Anggun

        Comment

        • Joe Mason

          #19
          Re: Is Python type safe?

          In article <roy-0B9FC3.17134816 032004@reader1. panix.com>, Roy Smith wrote:[color=blue]
          > I was once asked on an interview how I would tell, inside of a C
          > program, if I was on a big endian or a small endian machine. I said I'd
          > create a union of a long and a char[4], set the long equal to 1, and see
          > which char it showed up in. The interviewer looked shocked, thought
          > about it for a while, and finally said something like, "yeah, I guess
          > that would work".
          >
          > To this day, I have no idea what other plan he had in mind.[/color]

          unsigned short x = 1;
          if (x == ntohs(x)) printf("Network byte order!\n");
          else printf("Somethi ng else!\n");

          Joe

          Comment

          Working...