By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,935 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,935 IT Pros & Developers. It's quick & easy.

about pointer

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
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.

Nov 16 '05 #2

P: n/a
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.

Nov 16 '05 #3

P: n/a
On Tue, 15 Nov 2005 19:21:11 -0800, venkatesh wrote:
any can tell what is the advantge of using pointers?


You can use strings :P

And a number of more complicated data structures than strings too, of
course.
Daniel
Nov 16 '05 #4

P: n/a
venkatesh wrote:
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?


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"

Nov 16 '05 #5

P: n/a

"venkatesh" <pv***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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?


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.
Nov 16 '05 #6

P: n/a

venkatesh wrote:
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


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(newStringSize);

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(char *fileName) {...}
int parseGraCal(char *fileName) {...}
int parseMstDat(char *fileName) {...}
int parseMstCal(char *fileName) {...}
int parsePwvDat(char *fileName) {...}
int parsePwvCal(char *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 = getFirstFileName(); 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.

Nov 16 '05 #7

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

Nov 16 '05 #8

P: n/a
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.

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.
Nov 16 '05 #9

P: n/a
ca****@colorado.edu writes:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


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
Nov 16 '05 #10

P: n/a
"Ben Pfaff" writes:

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


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.


I took it that he meant Algol 68.
Nov 16 '05 #11

P: n/a
> *Also, any superset of C would have everything that C has, so "C
supersets" have pointers.


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

Nov 16 '05 #12

P: n/a
In article <11*********************@g43g2000cwa.googlegroups. com>
<ca****@colorado.edu> wrote:
Are pointers necessary beyond system programming?


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_substitute_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.
Nov 16 '05 #13

P: n/a
ca****@colorado.edu wrote:
´┐ŻAlso, any superset of C would have everything that C has, so "C
supersets" have pointers.

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

No, it does not. Eiffel takes cues from Algol, Ada and Pascal, and does
not resemble C/C++ at all.

S.
Nov 16 '05 #14

P: n/a
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.

Nov 16 '05 #15

P: n/a
venkatesh wrote:
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?


There are very few data structures that do not involve the use of
pointers. Almost all useful C programs use some values of pointer type.
The C language is designed around pointers. Without them, it is severely
crippled.

Does that answer your question? If not, give a more specific question!

--
Simon.
Nov 16 '05 #16

P: n/a

ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


Anything involving memory management involves pointers of some form,
and memory management comes into play all over the place, not just in
systems programming.

Regardless of whether they're exposed with special syntax/semantics as
in C and C++ or hidden behind reference types as in Java, pointers are
necessary for a wide variety of operations. A host of basic data
structures such as trees and lists rely on the ability to reference
arbitrary memory locations.

Nov 16 '05 #17

P: n/a
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.
Many imperative languages that do not have pointers have references --
that includes Algol 68.


In "A Practical Guide to Algol 68" (Frank G. Pagan, 1976),
the section that discusses 'ref' has the heading

6.2.1 Pointers and Casts

and the term "pointer" is used throughout that section.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 17 '05 #18

P: n/a
Walter Roberson wrote:
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


Many imperative languages that do not have pointers have references --
that includes Algol 68.

In "A Practical Guide to Algol 68" (Frank G. Pagan, 1976),
the section that discusses 'ref' has the heading

6.2.1 Pointers and Casts

and the term "pointer" is used throughout that section.


But they *are* references, not pointers, in modern parlance, even if
back in 1976 this distinction was not made. Correct me if I'm wrong, but
I do not believe Algol 68 has pointers in the C sense -- things you can
point to arbitrary memory locations, perform arithmetic on, cast to and
from all sorts of types, and allowing dereferencing only on the
programmer's responsibility that it makes sense at all.

S.
Nov 17 '05 #19

P: n/a
John Bode wrote:
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.

Anything involving memory management involves pointers of some form,
and memory management comes into play all over the place, not just in
systems programming.

Regardless of whether they're exposed with special syntax/semantics as
in C and C++ or hidden behind reference types as in Java, pointers are
necessary for a wide variety of operations. A host of basic data
structures such as trees and lists rely on the ability to reference
arbitrary memory locations.

I feel a slight reformulation is necessary. Basic data structures rely
on the ability to reference *objects*, not arbitrary memory locations.
(Some advanced data structures may take advantage of various tricks to
save time and space by going low-level on the bits that make up objects,
but these are not general or portable.)

Looking at references as "hiding" pointers is a good way to understand
it coming from C, but it's unnecessary if you're working in the
languages themselves -- references are just references. They reference
objects and different references can reference the same object, so that
operations affect the object as seen from all references to it. That's
about all you need to know; you don't want to think in terms of memory
locations unless you're implementing the language.

S.
Nov 17 '05 #20

P: n/a
Skarmander <in*****@dontmailme.com> writes:
Walter Roberson wrote:
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
ca****@colorado.edu wrote:

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

Many imperative languages that do not have pointers have references --
that includes Algol 68.

In "A Practical Guide to Algol 68" (Frank G. Pagan, 1976),
the section that discusses 'ref' has the heading
6.2.1 Pointers and Casts
and the term "pointer" is used throughout that section.


But they *are* references, not pointers, in modern parlance, even if
back in 1976 this distinction was not made. Correct me if I'm wrong,
but I do not believe Algol 68 has pointers in the C sense -- things
you can point to arbitrary memory locations, perform arithmetic on,
cast to and from all sorts of types, and allowing dereferencing only
on the programmer's responsibility that it makes sense at all.


I think this is about to devolve into a debate over the meaning of the
term "pointer". That's sensible in the context of a single language,
but it's going to cause confusion if you try to define the term across
languages that have different semantics for similarly-named
constructs. Each language has its own terminology.

(Personally, I'd say that both Algol 68 and C have pointers; C's
pointers are just particularly undisciplined.)

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 17 '05 #21

P: n/a
Keith Thompson wrote:
[references and pointers]
I think this is about to devolve into a debate over the meaning of the
term "pointer". That's sensible in the context of a single language,
but it's going to cause confusion if you try to define the term across
languages that have different semantics for similarly-named
constructs. Each language has its own terminology.
Oh, poppycock. Languages agree on the general terms that allow effective
comparison, like function, procedure, subprogram, subroutine, routine... :-)
(Personally, I'd say that both Algol 68 and C have pointers; C's
pointers are just particularly undisciplined.)

I have no problem calling references "disciplined pointers", and in my
mind the distinction makes sense across languages, but it wasn't my
intent to start a definition war.

S.
Nov 17 '05 #22

P: n/a
Dear venktash,
Beside dynamic memory allocation, pointers increases the
execution time of a program. You can itself test it by writing programs
such as Fermat and Pythgoras in C and then in java or else.

Nov 17 '05 #23

P: n/a
"Amar Prakash Tripaithi" <tr************@gmail.com> writes:
Dear venktash,
Beside dynamic memory allocation, pointers increases the
execution time of a program. You can itself test it by writing programs
such as Fermat and Pythgoras in C and then in java or else.


That doesn't make any sense. A given algorithm implemented using
pointers *might* be slower than the same algorithm implemented without
pointers, but writing two different implementations is non-trivial,
and any performance difference caused by the use of pointers is likely
to be swamped by other differences. Comparing equivalent programs in
C and Java certainly isn't going to tell you anything about the
performance impact of pointers.

Use pointers when it makes sense to do so, and not when it doesn't.
Get the program working before you worry about micro-optimization.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 17 '05 #24

P: n/a
Ben Pfaff <bl*@cs.stanford.edu> writes:
I'm not sure what you mean by "the Algol descendants" here. C is
an Algol descendant. [...]


I would say C is more Algol-influenced than an Algol descendant.
Algol has nested functions, which C emphatically does not. Obviously
there are other differences, but this difference seems essential
rather than incidental.
Dec 3 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.