about pointer

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

    #1

    about pointer

    hai,
    any can tell what is the advantge of using pointers?any increase in
    speed or execution time?
    if you know please tell what are the real time applications of data
    structure?


    thanks in advance

  • carlos@colorado.edu

    #2
    Re: about pointer

    System programming, I guess. When I had to deal with
    that level in the early 70s, the only choices were Fortran IV and
    assembly. In assembly pointers are natural, but were missing
    from Fortran except for vendor specific extensions such as %LOC.
    C began as a "high level assembly language" and still is.

    Comment

    • carlos@colorado.edu

      #3
      Re: about pointer

      More specifically, many computers of that period (late 60s ->
      mid 70s), during which C emerged, had "load indirect"
      assembly instructors. For example (Univac 11xx)

      LA,U A1,R4

      loads into the A1 register the contents of the word address
      currently in register R4, so "R4" can be thought of as a pointer.
      The idea of C was to associate symbolic names with these
      operations instead of specific register names, and let the
      compiler do the translations.

      Comment

      • Daniel Fischer

        #4
        Re: about pointer

        On Tue, 15 Nov 2005 19:21:11 -0800, venkatesh wrote:
        [color=blue]
        > any can tell what is the advantge of using pointers?[/color]

        You can use strings :P

        And a number of more complicated data structures than strings too, of
        course.


        Daniel

        Comment

        • Nick Keighley

          #5
          Re: about pointer

          venkatesh wrote:
          [color=blue]
          > any can tell what is the advantge of using pointers?any increase in
          > speed or execution time?
          > if you know please tell what are the real time applications of data
          > structure?[/color]

          it's more about expressivenes than speed. This sort of
          micro-optimisation is generally
          unimportant with modern compilers.

          Uses for pointers:-
          1. C always passes by value the only way to get the effect of pass by
          reference is by using pointers.
          2. certain data structures are expressed most naturally using pointers.
          Eg. trees and linked lists
          3. certain algorithms are more clearly expressed using pointers
          4. the only way a variable can refer to a function is by using a
          pointer to a function

          In short don't use pointers "for efficiency" but for clarity.


          --
          Nick Keighley

          "premature optimisation is the root of all evil"

          Comment

          • pemo

            #6
            Re: about pointer


            "venkatesh" <pvenkatesh2k4@ gmail.com> wrote in message
            news:1132111271 .570627.311540@ o13g2000cwo.goo glegroups.com.. .[color=blue]
            > hai,
            > any can tell what is the advantge of using pointers?any increase in
            > speed or execution time?
            > if you know please tell what are the real time applications of data
            > structure?[/color]

            One case where they're useful, in terms of efficiency is where structures
            are passed to functions.

            Normally, as C only has pass by value, a copy of the structure has to be
            made, the copy is what's passed to the function - obviously, if the
            structure is large, this takes time (well, it *takes time* even if it's
            small of course). So, you could elect to pass structures *by address*
            instead.

            The down side of that is that the called function can now crap on your
            structure, whereas before, it could only crap on a copy.


            Comment

            • John Bode

              #7
              Re: about pointer


              venkatesh wrote:[color=blue]
              > hai,
              > any can tell what is the advantge of using pointers?any increase in
              > speed or execution time?
              > if you know please tell what are the real time applications of data
              > structure?
              >
              >
              > thanks in advance[/color]

              Pointers are necessary for a number of operations:

              1. If you want to write to a function parameter and have that change
              reflected in the caller, you must use pointers:

              void swap_wrong(int a, int b)
              {
              int tmp = b;
              b = a;
              a = tmp;
              }

              void swap_right(int *a, int *b)
              {
              int tmp = *b;
              *b = *a;
              *a = tmp;
              }

              int main(void)
              {
              int x = 1, y = 2;
              printf("before swap_wrong: x = %d, y = %d\n", x, y);
              swap_wrong(x, y);
              printf("after swap_wrong: x = %d, y = %d\n", x, y);
              swap_right(&x, &y);
              printf("after swap_right: x = %d, y = %d\n", x, y);
              return 0;
              }


              2. The only way to track dynamically allocated memory is through a
              pointer:

              char *newString = malloc(newStrin gSize);

              3. The only way to create struct types that refer to instances of
              themselves is to use a pointer:

              struct tree {
              int value;
              struct tree *left;
              struct tree *right;
              };

              4. If you want to associate specific behaviors with specific data, you
              can use pointers to functions:

              /**
              * Routines to parse data and calibration files for various
              * scientific instruments
              */
              int parseGraDat(cha r *fileName) {...}
              int parseGraCal(cha r *fileName) {...}
              int parseMstDat(cha r *fileName) {...}
              int parseMstCal(cha r *fileName) {...}
              int parsePwvDat(cha r *fileName) {...}
              int parsePwvCal(cha r *fileName) {...}

              /**
              * Lookup table type to associate parse functions
              * with file types and extensions
              */
              struct parserLookup {
              char *instrumentType ;
              char *extension;
              int (*parser)(char *fileName);
              };

              /**
              * Lookup table instance
              */
              struct parseLookup lookupTable[] = {
              {"GRA", "dat", parseGraDat},
              {"GRA", "cal", parseGraCal},
              {"MST", "dat", parseMstDat},
              ...
              };

              ...
              for (file = getFirstFileNam e(); file != NULL; file =
              getNextFileName ())
              {
              int i = getLookupIndex( lookupTable, file);

              if (i >= 0)
              {
              if ((*lookupTable[i].parser)(file) != 1)
              {
              printf("Error parsing %s\n", file);
              }
              }
              }

              5. All array types are converted to pointer types if the array appears
              as a function parameter:

              int foo(int *arr) {...}

              int main(void)
              {
              int bar[10];
              ...
              if (foo(bar)) {...}
              ...
              return 0;
              }

              Technically, all array types are converted to pointer types if the
              array appears in any context other than an array definition or as a
              sizeof operand (I think there's one more that I'm forgetting).

              Pointers *can* offer some optimization; instead of passing large
              structs as function parameters, you can pass a pointer to the struct,
              saving some overhead.

              Comment

              • carlos@colorado.edu

                #8
                Re: about pointer

                Are pointers necessary beyond system programming? There
                are languages without them, e.g the Algol descendants and
                some C supersets.

                Comment

                • Skarmander

                  #9
                  Re: about pointer

                  carlos@colorado .edu wrote:[color=blue]
                  > Are pointers necessary beyond system programming? There
                  > are languages without them, e.g the Algol descendants and
                  > some C supersets.
                  >[/color]
                  Happy are those who answer their own questions...

                  No, pointers are not necessary. Functions are not necessary either. Both
                  have their uses, though.

                  Many imperative languages that do not have pointers have references --
                  that includes Algol 68. (And, incidentally, C is an Algol descendant
                  too, if not a direct one.)

                  Pointers as used to access random parts of memory (whether belonging to
                  declared objects or not) do not have use outside systems programming.
                  References (which pointers can implement) are useful in a general
                  algorithmic context.

                  S.

                  Comment

                  • Ben Pfaff

                    #10
                    Re: about pointer

                    carlos@colorado .edu writes:
                    [color=blue]
                    > Are pointers necessary beyond system programming? There
                    > are languages without them, e.g the Algol descendants and
                    > some C supersets.[/color]

                    If you want to do any but the simplest programming in C, you
                    pretty much have to use pointers. Other languages may not have
                    them, but they usually have other facilities that substitute.

                    I'm not sure what you mean by "the Algol descendants" here. C is
                    an Algol descendant. So is Pascal. Both have pointers. Also,
                    any superset of C would have everything that C has, so "C
                    supersets" have pointers.
                    --
                    "C has its problems, but a language designed from scratch would have some too,
                    and we know C's problems."
                    --Bjarne Stroustrup

                    Comment

                    • osmium

                      #11
                      Re: about pointer

                      "Ben Pfaff" writes:

                      [color=blue][color=green]
                      >> Are pointers necessary beyond system programming? There
                      >> are languages without them, e.g the Algol descendants and
                      >> some C supersets.[/color]
                      >
                      > If you want to do any but the simplest programming in C, you
                      > pretty much have to use pointers. Other languages may not have
                      > them, but they usually have other facilities that substitute.
                      >
                      > I'm not sure what you mean by "the Algol descendants" here. C is
                      > an Algol descendant. So is Pascal. Both have pointers. Also,
                      > any superset of C would have everything that C has, so "C
                      > supersets" have pointers.[/color]

                      I took it that he meant Algol 68.


                      Comment

                      • carlos@colorado.edu

                        #12
                        Re: about pointer

                        >  Also, any superset of C would have everything that C has, so "C[color=blue]
                        > supersets" have pointers.[/color]

                        Wrong choice of words. I should had said C-family languages, such
                        as Java. I dont know if Eiffel qualifies in this class.

                        Comment

                        • Chris Torek

                          #13
                          Re: about pointer

                          In article <1132161541.272 677.29020@g43g2 000cwa.googlegr oups.com>
                          <carlos@colorad o.edu> wrote:[color=blue]
                          >Are pointers necessary beyond system programming?[/color]

                          Are arrays necessary? Instead of op(a[i]) you can always write:

                          switch (i) {
                          case 0: op(a0); break;
                          case 1: op(a1); break;
                          case 2: op(a2); break;
                          ...
                          case 999: op(a999); break;
                          }

                          Obviously, then, arrays are not necessary.

                          They sure are convenient though. Of course, if you write:

                          op(a[i]);

                          and "i" is not a valid index into the array, things could go wrong.

                          Are pointers necessary? Instead of op(*p) you can always write:

                          switch (integer_substi tute_for_p) {
                          ...
                          }

                          Obviously, then, pointers are not necessary.

                          They sure are convenient though. Of course, if you write:

                          op(*p);

                          and p is not a valid pointer, things could go wrong.
                          --
                          In-Real-Life: Chris Torek, Wind River Systems
                          Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                          email: forget about it http://web.torek.net/torek/index.html
                          Reading email is like searching for food in the garbage, thanks to spammers.

                          Comment

                          • Skarmander

                            #14
                            Re: about pointer

                            carlos@colorado .edu wrote:[color=blue][color=green]
                            >>�Also, any superset of C would have everything that C has, so "C
                            >>supersets" have pointers.[/color]
                            >
                            >
                            > Wrong choice of words. I should had said C-family languages, such
                            > as Java. I dont know if Eiffel qualifies in this class.
                            >[/color]
                            No, it does not. Eiffel takes cues from Algol, Ada and Pascal, and does
                            not resemble C/C++ at all.

                            S.

                            Comment

                            • carlos@colorado.edu

                              #15
                              Re: about pointer

                              In the last analysis all you need are 0s and 1s. (Seymour
                              allegedly programmed the 6600 directly in octal - lucky guy)
                              In some locales, though, you have only 0s available - then
                              you would need big-endian 0s and little endian 0s.

                              Comment

                              Working...