about pointer

V

venkatesh

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
 
C

carlos

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

carlos

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

Daniel Fischer

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
 
N

Nick Keighley

venkatesh said:
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.
 
P

pemo

venkatesh said:
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.
 
J

John Bode

venkatesh said:
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.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.
 
C

carlos

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

Skarmander

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

Ben Pfaff

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

osmium

:

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

Chris Torek

Are pointers necessary beyond system programming?

Are arrays necessary? Instead of op(a) 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);

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

carlos

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

Skarmander

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

carlos

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

Simon Biber

venkatesh said:
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!
 
J

John Bode

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

Walter Roberson

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

Skarmander

Walter said:
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.
 
S

Skarmander

John said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top