What is the meaning of this warning, and how do I get rid of it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James H. Newman

    What is the meaning of this warning, and how do I get rid of it?

    I have a portion of code along the following
    lines:

    volatile unsigned char x ;
    unsigned int f(unsigned char *y) ;

    When I do

    unsigned int z = f(&x) ;

    the compiler issues the following warning:

    warning: passing arg 1 of `f' discards qualifiers from pointer target type

    What does this exactly mean? How do I change my code so that the
    compiler is happy about it?
  • Mark McIntyre

    #2
    Re: What is the meaning of this warning, and how do I get rid ofit?

    James H. Newman wrote:
    I have a portion of code along the following
    lines:
    >
    volatile unsigned char x ;
    unsigned int f(unsigned char *y) ;
    >
    When I do
    >
    unsigned int z = f(&x) ;
    >
    the compiler issues the following warning:
    >
    warning: passing arg 1 of `f' discards qualifiers from pointer target type
    >
    What does this exactly mean?
    It means you're discarding the "volatile" qualifier.

    How do I change my code so that the
    compiler is happy about it?
    don't use volatile?

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

    Comment

    • Walter Roberson

      #3
      Re: What is the meaning of this warning, and how do I get rid of it?

      In article <g58l50$k6r$1@r egistered.motza rella.org>,
      James H. Newman <NewJames@exici te.comwrote:
      I have a portion of code along the following
      >lines:
      volatile unsigned char x ;
      unsigned int f(unsigned char *y) ;
      >When I do
      unsigned int z = f(&x) ;
      >the compiler issues the following warning:
      >warning: passing arg 1 of `f' discards qualifiers from pointer target type
      What does this exactly mean?
      x is volatile, so &x is a pointer to a volatile variable. However,
      the receiving function is prototyped for non-volatile and so is not
      going to know to preserve volatile semantics.
      How do I change my code so that the
      >compiler is happy about it?
      You could prototype f with y being volatile. Or you could create a
      new non-volatile variable, initialize that with x's current value,
      send that to f and store the modified result after f into x. However,
      if x happens to be a memory-mapped I/O register or the like, or happens
      to be set by a signal processing routine, then that probably won't
      give you are result you want.
      --
      Current spam load: 750-800 messages per day (March 4, 2008)

      Comment

      • santosh

        #4
        Re: What is the meaning of this warning, and how do I get rid of it?

        James H. Newman wrote:
        I have a portion of code along the following
        lines:
        >
        volatile unsigned char x ;
        unsigned int f(unsigned char *y) ;
        >
        When I do
        >
        unsigned int z = f(&x) ;
        >
        the compiler issues the following warning:
        >
        warning: passing arg 1 of `f' discards qualifiers from pointer target
        type
        It means what it says. Since 'f' is prototyped as taking an unsigned
        char *, the volatile qualifier of 'x' is discarded when it is passed
        to 'f'. Also note that the expression &x is actually of type volatile
        unsigned char ** - not the type that you want to be passing to 'f'.
        What does this exactly mean? How do I change my code so that
        the compiler is happy about it?
        Prototype and define 'f' as:

        unsigned int f(volatile unsigned char *y) { /* ... */ }

        If 'f' needs to change the caller's copy of it's argument then you
        probably want:

        unsigned int f(volatile unsigned char **y) { /* ... */ }

        Comment

        • Raymond Martineau

          #5
          Re: What is the meaning of this warning, and how do I get rid of it?

          On Sat, 12 Jul 2008 03:45:17 +0530, santosh <santosh.k83@gm ail.com>
          wrote:
          >James H. Newman wrote:
          >
          > I have a portion of code along the following
          >lines:
          >>
          > volatile unsigned char x ;
          > unsigned int f(unsigned char *y) ;
          >>
          >When I do
          >>
          > unsigned int z = f(&x) ;
          >>
          [...]
          >
          >If 'f' needs to change the caller's copy of it's argument then you
          >probably want:
          >
          unsigned int f(volatile unsigned char **y) { /* ... */ }
          That would require x to be a pointer. Currently, it's simply an
          unsigned char.

          Comment

          • Barry Schwarz

            #6
            Re: What is the meaning of this warning, and how do I get rid of it?

            On Sat, 12 Jul 2008 03:45:17 +0530, santosh <santosh.k83@gm ail.com>
            wrote:
            >James H. Newman wrote:
            >
            > I have a portion of code along the following
            >lines:
            >>
            > volatile unsigned char x ;
            > unsigned int f(unsigned char *y) ;
            >>
            >When I do
            >>
            > unsigned int z = f(&x) ;
            >>
            >the compiler issues the following warning:
            >>
            >warning: passing arg 1 of `f' discards qualifiers from pointer target
            >type
            >
            >It means what it says. Since 'f' is prototyped as taking an unsigned
            >char *, the volatile qualifier of 'x' is discarded when it is passed
            >to 'f'. Also note that the expression &x is actually of type volatile
            >unsigned char ** - not the type that you want to be passing to 'f'.
            Where did the double asterisks come from? There is only one & and x
            is not a pointer.


            Remove del for email

            Comment

            • Ron Ford

              #7
              Re: What is the meaning of this warning, and how do I get rid of it?

              On Fri, 11 Jul 2008 22:15:12 +0000 (UTC), Walter Roberson posted:
              In article <g58l50$k6r$1@r egistered.motza rella.org>,
              James H. Newman <NewJames@exici te.comwrote:
              > I have a portion of code along the following
              >>lines:
              >
              > volatile unsigned char x ;
              > unsigned int f(unsigned char *y) ;
              >
              >>When I do
              >
              > unsigned int z = f(&x) ;
              >
              >>the compiler issues the following warning:
              >
              >>warning: passing arg 1 of `f' discards qualifiers from pointer target type
              >
              > What does this exactly mean?
              >
              x is volatile, so &x is a pointer to a volatile variable. However,
              the receiving function is prototyped for non-volatile and so is not
              going to know to preserve volatile semantics.
              >
              >How do I change my code so that the
              >>compiler is happy about it?
              >
              You could prototype f with y being volatile. Or you could create a
              new non-volatile variable, initialize that with x's current value,
              send that to f and store the modified result after f into x. However,
              if x happens to be a memory-mapped I/O register or the like, or happens
              to be set by a signal processing routine, then that probably won't
              give you are result you want.
              A question for you, Walter.

              Since C is completely safe, why does one use the 'volatile' function
              specifier?
              --
              Temptation is a woman's weapon and man's excuse.
              H. L. Mencken

              Comment

              • Ian Collins

                #8
                Re: What is the meaning of this warning, and how do I get rid ofit?

                Ron Ford wrote:
                >
                Since C is completely safe, why does one use the 'volatile' function
                specifier?
                Who says C is completely safe?

                volatile is a variable qualifier. It can not be applied to a function.

                volatile tells the compiler not to optimise access to a variable. The
                value of a volatile variable may be changed by an external source (a
                hardware register for example).

                --
                Ian Collins.

                Comment

                • santosh

                  #9
                  Re: What is the meaning of this warning, and how do I get rid of it?

                  Raymond Martineau wrote:
                  On Sat, 12 Jul 2008 03:45:17 +0530, santosh <santosh.k83@gm ail.com>
                  wrote:
                  >
                  >>James H. Newman wrote:
                  >>
                  >> I have a portion of code along the following
                  >>lines:
                  >>>
                  >> volatile unsigned char x ;
                  >> unsigned int f(unsigned char *y) ;
                  >>>
                  >>When I do
                  >>>
                  >> unsigned int z = f(&x) ;
                  >>>
                  [...]
                  >>
                  >>If 'f' needs to change the caller's copy of it's argument then you
                  >>probably want:
                  >>
                  > unsigned int f(volatile unsigned char **y) { /* ... */ }
                  >
                  That would require x to be a pointer. Currently, it's simply an
                  unsigned char.
                  Sorry, I made a mistake. I deserve it for posting at 03:45!

                  Comment

                  • Walter Roberson

                    #10
                    Re: What is the meaning of this warning, and how do I get rid of it?

                    In article <x367lnt93ys$.4 d2mywqvuflr.dlg @40tude.net>,
                    Ron Ford <wade196884@net zero.netwrote:
                    >A question for you, Walter.
                    >Since C is completely safe, why does one use the 'volatile' function
                    >specifier?
                    I don't recall ever saying that "C is completely safe". It was
                    certainly never designed to be "completely safe".
                    --
                    "Product of a myriad various minds and contending tongues, compact of
                    obscure and minute association, a language has its own abundant and
                    often recondite laws, in the habitual and summary recognition of
                    which scholarship consists." -- Walter Pater

                    Comment

                    • Ron Ford

                      #11
                      Re: What is the meaning of this warning, and how do I get rid of it?

                      On Sat, 12 Jul 2008 13:59:11 +0000 (UTC), Walter Roberson posted:
                      In article <x367lnt93ys$.4 d2mywqvuflr.dlg @40tude.net>,
                      Ron Ford <wade196884@net zero.netwrote:
                      >
                      >>A question for you, Walter.
                      >
                      >>Since C is completely safe, why does one use the 'volatile' function
                      >>specifier?
                      >
                      I don't recall ever saying that "C is completely safe". It was
                      certainly never designed to be "completely safe".
                      No, I was engaging in hyperbole. I think of that which is "volatile" to be
                      opposed to that which is "safe." I've never used this qualifier in my own
                      code.

                      So I spent some time with K&R, thinking I would there find the reason to
                      use the "volatile." Instaed I find §A8.2 in the footnote: "the const and
                      volatile properties are new with the ANSI standard. The purpose of const
                      ..... The purpose of volatile is to force an implementation to suppress
                      optimization that could otherwise occur...."

                      I'm not really any closer than a half hour ago. What is the difference
                      between type specifiers and type qualifiers?
                      --
                      Time stays, we go.
                      H. L. Mencken

                      Comment

                      • rahul

                        #12
                        Re: What is the meaning of this warning, and how do I get rid of it?

                        On Jul 14, 10:27 am, Ron Ford <r...@nowhere.n etwrote:
                        So I spent some time with K&R, thinking I would there find the reason to
                        use the "volatile."  
                        Generally volatile is used in embedded system programming. Just a
                        hypothetical code:

                        #define READY 0x01
                        volatile char *mem = 0x1234; /*reading from some memory-mapped device.
                        When device is ready, it writes READY here
                        *assume initially it is not READY. */
                        /* some code */

                        while (READY != *mem) {
                        continue; /* busy waiting */
                        }

                        If it is not declared as volatile, the compiler may read the value
                        only once and the while loop becomes an infinite loop.

                        Comment

                        • Keith Thompson

                          #13
                          Re: What is the meaning of this warning, and how do I get rid of it?

                          Ron Ford <ron@nowhere.ne twrites:
                          On Sat, 12 Jul 2008 13:59:11 +0000 (UTC), Walter Roberson posted:
                          >In article <x367lnt93ys$.4 d2mywqvuflr.dlg @40tude.net>,
                          >Ron Ford <wade196884@net zero.netwrote:
                          >>
                          >>>A question for you, Walter.
                          >>
                          >>>Since C is completely safe, why does one use the 'volatile' function
                          >>>specifier?
                          >>
                          >I don't recall ever saying that "C is completely safe". It was
                          >certainly never designed to be "completely safe".
                          >
                          No, I was engaging in hyperbole. I think of that which is
                          "volatile" to be opposed to that which is "safe." I've never used
                          this qualifier in my own code.
                          The English meaning of the term doesn't tell you much about what it
                          means in C. "volatile" is not the opposite of "safe" in this context.
                          So I spent some time with K&R, thinking I would there find the reason to
                          use the "volatile." Instaed I find §A8.2 in the footnote: "the const and
                          volatile properties are new with the ANSI standard. The purpose of const
                          .... The purpose of volatile is to force an implementation to suppress
                          optimization that could otherwise occur...."
                          If a program reads the value of a variable, and the compiler is able
                          to determine what its current value happens to be, it can generate
                          code that doesn't actually load the variable's value. For example:

                          ...
                          int x = 42;
                          printf("x = %d\n", x);
                          ...

                          The compiler can legitimately replace the printf() call with
                          puts("x = 42");
                          because it *knows* what the result of evaluating x would have been.

                          If x were qualified with "volatile", it would not be permitted to
                          perform that optimization.

                          The same applies to writes as well as reads.
                          I'm not really any closer than a half hour ago. What is the difference
                          between type specifiers and type qualifiers?
                          The latest draft of the standard is at
                          <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.
                          It defines both terms, in 6.7.2 and 6.7.3, respectively.

                          The type specifiers are void, char, short, int, long, and so forth.

                          The type qualifiers are const, restrict, and volatile (restrict is new
                          in C99).

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

                          • Nick Keighley

                            #14
                            Re: What is the meaning of this warning, and how do I get rid of it?

                            On 14 Jul, 08:46, Keith Thompson <ks...@mib.orgw rote:
                            Ron Ford <r...@nowhere.n etwrites:
                            On Sat, 12 Jul 2008 13:59:11 +0000 (UTC), Walter Roberson posted:
                            In article <x367lnt93ys$.4 d2mywqvuflr.... @40tude.net>,
                            Ron Ford <wade196...@net zero.netwrote:
                            >>A question for you, Walter.
                            >
                            >>Since C is completely safe, why does one use the 'volatile' function
                            >>specifier?
                            since when has C been "completely safe". What does it mean?
                            I don't recall ever saying that "C is completely safe". It was
                            certainly never designed to be "completely safe".
                            >
                            No, I was engaging in hyperbole.
                            I think you need a new dictionary. You seem to be using non-standard
                            meanings for "safe", "volatile" and "hyperbole"

                            I think of that which is
                            "volatile" to be opposed to that which is "safe."
                            why do you think this?

                            I've never used this qualifier in my own code.
                            nor me
                            The English meaning of the term doesn't tell you much about what it
                            means in C. "volatile" is not the opposite of "safe" in this context.
                            "volatile" isn't the opposite of "safe" in English either!

                            So I spent some time with K&R, thinking I would there find the reason to
                            use the "volatile." Instaed I find §A8.2 in the footnote: "the const and
                            volatile properties are new with the ANSI standard. The purpose of const
                            .... The purpose of volatile is to force an implementation to suppress
                            optimization that could otherwise occur...."
                            if you wanted to know what volatile meant in C why didn't
                            you just ask that?

                            <snip>

                            --
                            Nick Keighley

                            "Resistance is futile. Read the C-faq."
                            -- James Hu (c.l.c.)

                            Comment

                            • CBFalconer

                              #15
                              Re: What is the meaning of this warning, and how do I get rid of it?

                              Keith Thompson wrote:
                              Ron Ford <ron@nowhere.ne twrites:
                              >
                              .... snip ...
                              >
                              >So I spent some time with K&R, thinking I would there find the
                              >reason to use the "volatile." Instaed I find §A8.2 in the
                              >footnote: "the const and volatile properties are new with the
                              >ANSI standard. The purpose of const .... The purpose of
                              >volatile is to force an implementation to suppress optimization
                              >that could otherwise occur...."
                              >
                              If a program reads the value of a variable, and the compiler is
                              able to determine what its current value happens to be, it can
                              generate code that doesn't actually load the variable's value.
                              For example:
                              >
                              ...
                              int x = 42;
                              printf("x = %d\n", x);
                              ...
                              >
                              The compiler can legitimately replace the printf() call with
                              puts("x = 42");
                              because it *knows* what the result of evaluating x would have
                              been. If x were qualified with "volatile", it would not be
                              permitted to perform that optimization.
                              However this doesn't explain the necessity for volatile. If, for
                              example, that memory location holds the 'ready' status for data on
                              a peripheral, code is going to have to wait for the signal to
                              become true. Without the volatile it would read the signal once,
                              and then just remain in the testing loop, because it knows the
                              answer. With volatile, the system is forced to reread the ready
                              status on each pass through the loop.

                              Volatile means "something other than this code can alter this
                              value".

                              --
                              [mail]: Chuck F (cbfalconer at maineline dot net)
                              [page]: <http://cbfalconer.home .att.net>
                              Try the download section.


                              Comment

                              Working...