pointer arithmatic

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    #1

    pointer arithmatic

    Can I subtract two pointers of same type that are pointing to the two
    different location of memory allocated by malloc.

    eg.
    #include <stdlib.h>
    int main(void)
    {

    unsigned char *c_ptr;
    unsigned char *c_ptr1;
    unsigned char *c_ptr2;
    int diff;

    c_ptr = malloc(100 * sizeof(char));
    c_ptr1 = c_ptr + 5;
    c_ptr2 = c_ptr + 10;
    diff = c_ptr2 - c_ptr1; /* Is it valid ? */
    }
    Is the subtraction ( c_ptr2 - c_ptr1 ) valid ?
    Is it guaranteed that "diff" will always be 5 ?

  • Robert Gamble

    #2
    Re: pointer arithmatic



    junky_fellow@ya hoo.co.in wrote:[color=blue]
    > Can I subtract two pointers of same type that are pointing to the two
    > different location of memory allocated by malloc.[/color]

    Yes, as long as they both point to some place in the same object
    allocated by malloc.
    [color=blue]
    > eg.
    > #include <stdlib.h>
    > int main(void)
    > {
    >
    > unsigned char *c_ptr;
    > unsigned char *c_ptr1;
    > unsigned char *c_ptr2;
    > int diff;
    >
    > c_ptr = malloc(100 * sizeof(char));
    > c_ptr1 = c_ptr + 5;
    > c_ptr2 = c_ptr + 10;
    > diff = c_ptr2 - c_ptr1; /* Is it valid ? */[/color]

    Missing return statement here.
    [color=blue]
    > }
    > Is the subtraction ( c_ptr2 - c_ptr1 ) valid ?[/color]

    Yes.
    [color=blue]
    > Is it guaranteed that "diff" will always be 5 ?[/color]

    Yes.

    Robert Gamble

    Comment

    • CBFalconer

      #3
      Re: pointer arithmatic

      junky_fellow@ya hoo.co.in wrote:[color=blue]
      >
      > Can I subtract two pointers of same type that are pointing to the
      > two different location of memory allocated by malloc.[/color]

      No.

      --
      "I conclude that there are two ways of constructing a software
      design: One way is to make it so simple that there are obviously
      no deficiencies and the other way is to make it so complicated
      that there are no obvious deficiencies." -- C. A. R. Hoare


      Comment

      • Bob

        #4
        Re: pointer arithmatic

        Well, at least your answer is concise. Were it but also correct. The
        integer difference between two pointers may be meaningless, but pointer
        subtraction is always valid.

        Comment

        • S.Tobias

          #5
          Re: pointer arithmatic

          [please don't snip context and attributions]

          CBFalconer wrote:[color=blue]
          > junky_fellow@ya hoo.co.in wrote:[color=green]
          > >
          > > Can I subtract two pointers of same type that are pointing to the
          > > two different location of memory allocated by malloc.[/color]
          >
          > No.[/color]

          Bob <rwillia1@txued .com> wrote:[color=blue]
          > Well, at least your answer is concise. Were it but also correct. The
          > integer difference between two pointers may be meaningless, but pointer
          > subtraction is always valid.[/color]

          No, not always. Pointer difference is defined only for arrays.

          struct s { int a, b; } s;
          int *p1, *p2;
          p1 = &s.a;
          p2 = &s.b;
          p2 < p1; //valid, false
          p2 - p1; //invalid, UB

          --
          Stan Tobias
          mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

          Comment

          • pete

            #6
            Re: pointer arithmatic

            junky_fellow@ya hoo.co.in wrote:
            [color=blue]
            > int diff;[/color]

            #include <stddef.h>

            ptrdiff_t diff;

            --
            pete

            Comment

            • Keith Thompson

              #7
              Re: pointer arithmatic

              CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
              > junky_fellow@ya hoo.co.in wrote:[color=green]
              >>
              >> Can I subtract two pointers of same type that are pointing to the
              >> two different location of memory allocated by malloc.[/color]
              >
              > No.[/color]

              That's a correct answer to the question as stated, but the sample code
              indicates that he was really asking about two different pointers into
              the same malloc()ed object. The answer to the question he *meant* to
              ask is yes.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Kevin D. Quitt

                #8
                Re: pointer arithmatic

                On 2 Jun 2005 06:22:30 -0700, junky_fellow@ya hoo.co.in wrote:[color=blue]
                >Is it guaranteed that "diff" will always be 5 ?[/color]

                Given the other conditions noted in this thread, yes, no matter what size object the
                pointers are referring to. ptr +5 adds 5 to the pointer if it's char pointer, but would
                add 10 is it's a pointer to (16 bit) short. The difference will always reflect the index
                values, but the actual difference in memory address.


                --
                #include <standard.discl aimer>
                _
                Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up

                Comment

                • CBFalconer

                  #9
                  Re: pointer arithmatic

                  Bob wrote:[color=blue]
                  >
                  > Well, at least your answer is concise. Were it but also correct.
                  > The integer difference between two pointers may be meaningless,
                  > but pointer subtraction is always valid.[/color]

                  Since you didn't bother to quote anything, all I have to go on is
                  your erroneous statement above. Pointer subtraction is only valid
                  between two pointers to the same object, or portions of that
                  object. It always returns an integer, of the signed type ptrdiff_t
                  as defined in <stddef.h>.

                  So, in general, pointer subtraction serves no useful purpose.

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson


                  Comment

                  • Keith Thompson

                    #10
                    Re: pointer arithmatic

                    CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
                    > Bob wrote:[color=green]
                    >>
                    >> Well, at least your answer is concise. Were it but also correct.
                    >> The integer difference between two pointers may be meaningless,
                    >> but pointer subtraction is always valid.[/color]
                    >
                    > Since you didn't bother to quote anything, all I have to go on is
                    > your erroneous statement above. Pointer subtraction is only valid
                    > between two pointers to the same object, or portions of that
                    > object. It always returns an integer, of the signed type ptrdiff_t
                    > as defined in <stddef.h>.
                    >
                    > So, in general, pointer subtraction serves no useful purpose.[/color]

                    Unfortunately, the phrase "in general" is a remarkably efficient way
                    to generate ambiguous sentences.

                    Pointer subtraction is useful in some cases, and is not useful (and in
                    fact invokes undefined behavior) in others. Do you agree?

                    (And you should add "or just past the end of the object" above.)

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • glen herrmannsfeldt

                      #11
                      Re: pointer arithmatic

                      junky_fellow@ya hoo.co.in wrote:
                      [color=blue]
                      > Can I subtract two pointers of same type that are pointing to the two
                      > different location of memory allocated by malloc.[/color]

                      (snip example)

                      If they were returned by the same malloc(), or are pointers to the
                      same static or automatic array, and, if cast, have been cast to the
                      same type. You can do some interesting things...

                      char a[1000], *b, *c;
                      int *i, *j;
                      b=a+3;
                      c=a+9;
                      i=(int*)a;
                      j=(int*)b;

                      now, what do you expect for j-i?

                      (even though it shouldn't matter, assume a processor without
                      restrictions on alignment.)

                      -- glen

                      Comment

                      • Zoran Cutura

                        #12
                        Re: pointer arithmatic

                        glen herrmannsfeldt <gah@ugcs.calte ch.edu> wrote:[color=blue]
                        > junky_fellow@ya hoo.co.in wrote:
                        >[color=green]
                        >> Can I subtract two pointers of same type that are pointing to the two
                        >> different location of memory allocated by malloc.[/color]
                        >
                        > (snip example)
                        >
                        > If they were returned by the same malloc(), or are pointers to the
                        > same static or automatic array, and, if cast, have been cast to the
                        > same type. You can do some interesting things...
                        >
                        > char a[1000], *b, *c;
                        > int *i, *j;
                        > b=a+3;
                        > c=a+9;
                        > i=(int*)a;
                        > j=(int*)b;
                        >
                        > now, what do you expect for j-i?
                        >
                        > (even though it shouldn't matter, assume a processor without
                        > restrictions on alignment.)[/color]

                        I suppose "3 for sizeof(int) == 2" but I might be wrong.
                        But what if sizeof(int) is 4? I think the behaviour is undefined.
                        The result of i-j would not be representable in a ptrdiff_t object and
                        therefor we'ld get nasal deamons.


                        --
                        Z (zoran.cutura@w eb.de)
                        "LISP is worth learning for the profound enlightenment experience
                        you will have when you finally get it; that experience will make you
                        a better programmer for the rest of your days." -- Eric S. Raymond

                        Comment

                        Working...