size_t in c++?

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

    size_t in c++?

    In a file I have made:

    size_t bb;
    bb = 3u;
    printf("bb %d\n");

    which prints:

    bb 2280640

    But what is 'u' and why does it print the above number?


  • Ismo Salonen

    #2
    Re: size_t in c++?

    saneman wrote:
    In a file I have made:
    >
    size_t bb;
    bb = 3u;
    printf("bb %d\n");
    >
    which prints:
    >
    bb 2280640
    >
    But what is 'u' and why does it print the above number?
    >
    >
    Prints garbage from stack, there is no argument given for %d.
    Should be : printf("bb %d\n",bb);

    ismo

    Comment

    • Victor Bazarov

      #3
      Re: size_t in c++?

      saneman wrote:
      In a file I have made:
      >
      size_t bb;
      bb = 3u;
      printf("bb %d\n");
      >
      which prints:
      >
      bb 2280640
      >
      But what is 'u' and why does it print the above number?
      'u' is a suffix to make your literal *unsigned*. It prints what you see
      by *pure chance*. You provided the format specification (%d) but no
      corresponding argument, so the behaviour of your program is undefined,
      it can do whatever it wants. Search the web for "nasal demons" (with
      quotes).

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Comment

      • blargg

        #4
        Re: size_t in c++?

        In article <8UDkk.85$bN5.6 8@read4.inet.fi >, Ismo Salonen
        <nobody@another .invalidwrote:
        saneman wrote:
        size_t bb;
        bb = 3u;
        printf("bb %d\n");
        ...
        >
        Prints garbage from stack, there is no argument given for %d.
        Should be : printf("bb %d\n",bb);
        That prints 0 on one of my machines (big-endian, 16-bit int). %d specifies
        an int, but size_t isn't necessarily an (unsigned) int. Since the subject
        and group is C++, not C, the way to print it is

        std::cout << bb;

        If printf must be used for some reason, cast bb to an int, or better, use
        unsigned long:

        printf( "bb %lu\n", (unsigned long) bb );

        Comment

        • Markus Moll

          #5
          Re: size_t in c++?

          Hi

          Ismo Salonen wrote:
          saneman wrote:
          >In a file I have made:
          >>
          >size_t bb;
          >bb = 3u;
          >printf("bb %d\n");
          >
          Prints garbage from stack, there is no argument given for %d.
          Should be : printf("bb %d\n",bb);
          Even then, %d and size_t don't match. Use %zu for size_t, as it might be
          larger than an int.

          Markus

          Comment

          • Pete Becker

            #6
            Re: size_t in c++?

            On 2008-08-01 09:17:53 -0400, Markus Moll
            <markus.moll@es at.kuleuven.ac. besaid:
            Hi
            >
            Ismo Salonen wrote:
            >
            >saneman wrote:
            >>In a file I have made:
            >>>
            >>size_t bb;
            >>bb = 3u;
            >>printf("bb %d\n");
            >>
            >Prints garbage from stack, there is no argument given for %d.
            >Should be : printf("bb %d\n",bb);
            >
            Even then, %d and size_t don't match. Use %zu for size_t, as it might be
            larger than an int.
            >
            Well, yes, non-portably. "%zu" is new with C99, and the C++ standard is
            based on C90. The next version of the standard will provide "%zu".

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            • Anarki

              #7
              Re: size_t in c++?

              On Aug 1, 6:17 pm, Markus Moll <markus.m...@es at.kuleuven.ac. be>
              wrote:
              Hi
              >
              Ismo Salonen wrote:
              saneman wrote:
              In a file I have made:
              >
              size_t bb;
              bb = 3u;
              printf("bb %d\n");
              >
              Prints garbage from stack, there is no argument given for %d.
              Should be : printf("bb %d\n",bb);
              >
              Even then, %d and size_t don't match. Use %zu for size_t, as it might be
              larger than an int.
              >
              Markus
              excuse me Markus whats %zu? whats the z stand for?

              Comment

              • Ian Collins

                #8
                Re: size_t in c++?

                Anarki wrote:
                On Aug 1, 6:17 pm, Markus Moll <markus.m...@es at.kuleuven.ac. be>
                wrote:
                >Hi
                >>
                >Ismo Salonen wrote:
                >>saneman wrote:
                >>>In a file I have made:
                >>>size_t bb;
                >>>bb = 3u;
                >>>printf("bb %d\n");
                >>Prints garbage from stack, there is no argument given for %d.
                >>Should be : printf("bb %d\n",bb);
                >Even then, %d and size_t don't match. Use %zu for size_t, as it might be
                >larger than an int.
                >>
                >Markus
                >
                excuse me Markus whats %zu? whats the z stand for?
                It's the C99 format specifier for size_t.

                --
                Ian Collins.

                Comment

                Working...