define a size_t constant

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

    define a size_t constant

    Is there a suffix for size_t?

    I want to define MAX_LINE_LENGTH as a size_t.
    The best I could come up with was


    #define MAX_LINE_LENGTH ((size_t)80)


    but I wonder if there's a more beautiful construct, like


    /* Error: Invalid suffix "ZU" */
    /* #define MAX_LINE_LENGTH 80ZU */
  • Harald van =?UTF-8?b?RMSzaw==?=

    #2
    Re: define a size_t constant

    On Sat, 18 Oct 2008 07:58:37 -0700, Mara Guida wrote:
    Is there a suffix for size_t?
    size_t is a typedef, not a built-in type. It is a typedef for unsigned int
    on some systems, and there the suffix is U. It is a typedef for unsigned
    long on some other systems, and there the suffix is UL. And those are not
    the only two possibilities.
    I want to define MAX_LINE_LENGTH as a size_t.
    Why do you want to do this? Does size_t offer anything of use for
    constants that the default type doesn't? Depending on why you want this,
    there may be other options.
    The best I could come up with was
    >
    #define MAX_LINE_LENGTH ((size_t)80)
    You cannot use this in #if expressions. That's fine if you don't need to,
    but macros for constants are generally useable there.
    but I wonder if there's a more beautiful construct,
    const size_t MAX_LINE_LENGTH = 80;

    It has its own limitations: you cannot use this as a static array length,
    for example. What do you want to do with MAX_LINE_LENGTH ?

    Comment

    • Mara Guida

      #3
      Re: define a size_t constant

      Harald van D©¦k wrote:
      On Sat, 18 Oct 2008 07:58:37 -0700, Mara Guida wrote:
      Is there a suffix for size_t?
      >
      size_t is a typedef, not a built-in type.
      Ah! Of course.
      I got used to thinking of size_t as a built-in type.
      I want to define MAX_LINE_LENGTH as a size_t.
      >
      Why do you want to do this? Does size_t offer anything of use for
      constants that the default type doesn't? Depending on why you want this,
      there may be other options.
      I want to minimize the number of casts and warnings of my programs.
      Some functions take a size_t as a parameter and with a plain #define
      the compiler issues a warning; other functions take an int and
      complain with a size_t.


      #include <stddef.h>
      #include <stdio.h>
      #include <stdlib.h>

      #define MAX_LINE_LENGTH _AS_INT 80
      #define MAX_LINE_LENGTH _AS_SIZE_T ((size_t)80)

      int main(void)
      {
      char *data_int;
      char *data_size_t = malloc(MAX_LINE _LENGTH_AS_SIZE _T);
      if (data_size_t) {
      /* warning: passing argument 2 of ¡®fgets¡¯ with different width
      due to prototype */
      fgets(data_size _t, MAX_LINE_LENGTH _AS_SIZE_T, stdin);
      /* fgets(data_size _t, (int)MAX_LINE_L ENGTH_AS_SIZE_T , stdin);
      */ /* ok with cast */
      free(data_size_ t);
      }

      /* warning: passing argument 1 of ¡®malloc¡¯ with different width
      due to prototype */
      data_int = malloc(MAX_LINE _LENGTH_AS_INT) ;
      /* data_int = malloc((size_t) MAX_LINE_LENGTH _AS_INT); */ /* ok
      with cast */
      if (data_int) {
      fgets(data_int, MAX_LINE_LENGTH _AS_INT, stdin);
      free(data_int);
      }
      return 0;
      }


      I think it's better to cast a size_t to int, than the other way
      around.
      but I wonder if there's a more beautiful construct,
      >
      const size_t MAX_LINE_LENGTH = 80;
      >
      It has its own limitations: you cannot use this as a static array length,
      for example. What do you want to do with MAX_LINE_LENGTH ?
      This looks good, thank you.
      I'm going to try some variations with const too.

      Comment

      • Keith Thompson

        #4
        Re: define a size_t constant

        Mara Guida <maraguida@gmai l.comwrites:
        Harald van D©¦k wrote:
        >On Sat, 18 Oct 2008 07:58:37 -0700, Mara Guida wrote:
        Is there a suffix for size_t?
        >>
        >size_t is a typedef, not a built-in type.
        Ah! Of course.
        I got used to thinking of size_t as a built-in type.
        >
        I want to define MAX_LINE_LENGTH as a size_t.
        >>
        >Why do you want to do this? Does size_t offer anything of use for
        >constants that the default type doesn't? Depending on why you want this,
        >there may be other options.
        I want to minimize the number of casts and warnings of my programs.
        Some functions take a size_t as a parameter and with a plain #define
        the compiler issues a warning; other functions take an int and
        complain with a size_t.
        [...]
        #define MAX_LINE_LENGTH _AS_INT 80
        #define MAX_LINE_LENGTH _AS_SIZE_T ((size_t)80)
        [...]
        /* warning: passing argument 1 of ¡®malloc¡¯ with different width
        due to prototype */
        data_int = malloc(MAX_LINE _LENGTH_AS_INT) ;
        /* data_int = malloc((size_t) MAX_LINE_LENGTH _AS_INT); */ /* ok
        with cast */
        [...]

        I don't think I've never seen such a warning. So simply calling

        malloc(80)

        would produce the same warning? I think your compiler is being overly
        picky.

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

        • Huibert Bol

          #5
          Re: define a size_t constant

          Keith Thompson wrote:
          I don't think I've never seen such a warning. So simply calling
          Double negatives are *so* confusing. Is it yes or no?
          >
          malloc(80)
          >
          would produce the same warning? I think your compiler is being overly
          picky.
          gcc will do that for you (if you ask politely):

          $ cat t.c
          void foo(long long a) { }
          void bar(void) { foo(1); }
          $ gcc -Wconversion -fsyntax-only t.c
          t.c: In function ‘bar’:
          t.c:2: warning: passing argument 1 of ‘foo’ with different width due to prototype

          --
          Huibert
          "Hey! HEY! Curious cat, here!" -- Krosp I (GG)

          Comment

          • Harald van =?UTF-8?b?RMSzaw==?=

            #6
            Re: define a size_t constant

            On Sat, 18 Oct 2008 09:03:00 -0700, Mara Guida wrote:
            I want to minimize the number of casts and warnings of my programs. Some
            functions take a size_t as a parameter and with a plain #define the
            compiler issues a warning; other functions take an int and complain with
            a size_t.
            [...]
            #define MAX_LINE_LENGTH _AS_INT 80
            [...]
            /* warning: passing argument 1 of ‘malloc’ with different width
            due to prototype */
            data_int = malloc(MAX_LINE _LENGTH_AS_INT)
            This is what gcc's -Wconversion or -Wtraditional-conversion (depending on
            the version) does. It is meant to help you write code that will compile
            and run correctly both on modern compilers and old compilers that don't
            support function prototypes. Your code will not work on those old
            compilers anyway, so I can't see why you would use this option. The code
            is correct, so the simple solution is to not tell your compiler to warn
            you about it.

            Comment

            • Keith Thompson

              #7
              Re: define a size_t constant

              Huibert Bol <huibert.bol@qu icknet.nlwrites :
              Keith Thompson wrote:
              >
              >I don't think I've never seen such a warning. So simply calling
              >
              Double negatives are *so* confusing. Is it yes or no?
              Sorry, that was just a typo; I meant "I don't think I've ever seen
              such a warning."

              [...]

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

              • Mara Guida

                #8
                Re: define a size_t constant

                Harald van D©¦k wrote:
                On Sat, 18 Oct 2008 09:03:00 -0700, Mara Guida wrote:
                I want to minimize the number of casts and warnings of my programs.
                [...]
                #define MAX_LINE_LENGTH _AS_INT 80
                [...]
                /* warning: passing argument 1 of ¡®malloc¡¯ with differentwidth
                due to prototype */
                data_int = malloc(MAX_LINE _LENGTH_AS_INT)
                >
                This is what gcc's -Wconversion or -Wtraditional-conversion (depending on
                the version) does.
                [...]
                Thank you for the comprehensive answer.
                I've been tweaking my compilation alias with what appears here every
                now and then. I read about what the specific option does before adding
                it in, and I don't remember thinking the -Wconversion would be bad for
                me (or maybe I skipped its description).

                Comment

                • Peter Nilsson

                  #9
                  Re: define a size_t constant

                  Harald van D©¦k <true...@gmail. comwrote:
                  ...This is what gcc's -Wconversion or -Wtraditional-
                  conversion (depending on the version) does. ...
                  The code is correct, so the simple solution is to
                  not tell your compiler to warn you about it.
                  In this case, turning it off is a viable option, but
                  it's amazing how many people still have a pathological
                  urge to modify perfectly correct code just to make it
                  compile without warnings against -W -Wall.

                  --
                  Peter

                  Comment

                  • Joachim Schmitz

                    #10
                    Re: define a size_t constant

                    Peter Nilsson wrote:
                    Harald van D©¦k <true...@gmail. comwrote:
                    >...This is what gcc's -Wconversion or -Wtraditional-
                    >conversion (depending on the version) does. ...
                    >The code is correct, so the simple solution is to
                    >not tell your compiler to warn you about it.
                    >
                    In this case, turning it off is a viable option, but
                    it's amazing how many people still have a pathological
                    urge to modify perfectly correct code just to make it
                    compile without warnings against -W -Wall.
                    -Wconversion is AFAIK not part of -W -Wall

                    Bye, Jojo


                    Comment

                    Working...