Pointers to global and stack variables

  • Follow


Hi
    I am trying to understand what might be a  few major and general
reasons why programmers might have pointers to global and stack data as
compared to pointers to heap which are essential to building data
structures. Based on that I want to find programs with such pointers.
Some reasons general reasons  I can think of are making pointers point
to a array based on runtime conditions,  writing obfuscated but compact
code , better code generation in DSP processors etc. Can any one
suggest me any other reasons ?

thanks
Shrey
[The short answer is "because that's what their programs do".  There might be
a linked list with dynamically allocated entries but a static head.  There
might be some sort of symbol table that has various sorts of subfields, some
of which could be in short lived stack variables.

If the question is whether you could write a language which decreed that pointers
can only point to heap storage, sure, it's been done, but at some loss in
expressive flexibility. -John]
0
Reply shreyas76 (29) 11/26/2005 5:21:36 AM

 shrey <shreyas76@gmail.com> wrote:
>    I am trying to understand what might be a  few major and general
>reasons why programmers might have pointers to global and stack data as
>compared to pointers to heap which are essential to building data
>structures...

The most fundamental reason, in some sense, is that many languages
make it awkward or impossible to return multiple values from a
procedure except by passing some variables by reference or
value-result, which is almost always done with pointers to the
variables.

Similarly, it is often desirable to pass large objects by reference to
avoid expensive copying operations, even in cases where there is no intent
to have the procedure alter the objects.  (If they're strictly read-only,
this is just an implementation detail, but if aliasing with an output
parameter is possible, then you have to make this explicit because it can
change how programs behave.)

Optimized traversals of arrays also typically involve using pointers
rather than just indexes.

Depending on the language, the pointers for these operations may or may
not be visible to the programmer.  You can invent ways to express these
things without introducing general pointers into the language, and treat
the use of pointers to implement them as the compiler's problem.  You do
lose some flexibility that way, though.
--
spsystems.net is temporarily off the air;               |   Henry Spencer
mail to henry at zoo.utoronto.ca instead.               | henry@spsystems.net
0
Reply henry 11/27/2005 5:37:13 AM


shrey wrote:
> Hi
>     I am trying to understand what might be a  few major and general
> reasons why programmers might have pointers to global and stack data as
> compared to pointers to heap which are essential to building data
> structures. Based on that I want to find programs with such pointers.
> Some reasons general reasons  I can think of are making pointers point
> to a array based on runtime conditions,  writing obfuscated but compact
> code , better code generation in DSP processors etc. Can any one
> suggest me any other reasons ?

When writing embedded code you frequently need tight control of your
heap usage, while the amount of memory available for the heap is
given.  Often you will have to write you own heap management, because
you need to be sure that garbage collection is done when it cannot
break timing constraints, etc.  An easy way of allocating the heap is
to declare it as a global array and let the heap allocation routines
return pointers into that array.

I was once writing a recursive routine, where I need a linked list of
the same length as the depth of the recursion.  The easiest way of
allocation memory for that list was to declare a variable in the
recursive routine.  Each variable was to hold an element in the list
and was implemented as a record with a field for linking the list.
Then the field used for linking the list pointed from the variable in
one activation record to the variable in an other activation record.
Normally having dynamically allocated data structures stored in the
activation records of routines is a no no, that would make a teacher
of good coding practices hit the roof.  However in this case the code
became easier to understand and test, because it was given from the
problem that the list would not be needed after the return of the
routine, and doing it this way I did not have to check for memory
leaks.

Why do you want to study such examples?  Both the examples I have given
could have been implemented without using such pointer, but at the price
of an increased overhead or code that is more difficult to maintain.  In
the case of embedded computing it is often important that the processor
needed is cheap, e.g., if you intend to sell a million printers, then we
are talking real money if the microprocessor in the printer becomes a
few cents more expensive.

Karsten Nyblad
148f3wg02 at sneakemail dot com
0
Reply Karsten 11/29/2005 9:06:21 PM

>>    I am trying to understand what might be a  few major and general
>>reasons why programmers might have pointers to global and stack data as
>>compared to pointers to heap which are essential to building data
>>structures...

There are a couple of uses that I can think of. The most important is
call-by-reference. You want to be able to pass the address of a local
or global variable for modification, or pass a pointer to large object
to avoid copying.

In languages like Pascal, this doesn't look like a pointer (which can
really only point to the heap), but in C it's a pointer just like a
heap pointer.

In Java this is necessarily a heap pointer - you can't call by
reference and you can't point to a non-object.

Another use, as was pointed out, is statically constructed linked
structures.
--
	mac the na�f
0
Reply Alex 11/30/2005 10:29:11 PM

Hi Sathich,

And there are the record pointers, the POINT option with SET. See docs.

Regards - Jim.

On Sat, 4 Nov 2006 09:05:33 -0500, Arthur Tabachneck <art297@NETSCAPE.NET>
wrote:

>Take a look at: http://xrl.us/sy8o
>
>Art
>--------
>On Sat, 4 Nov 2006 04:53:58 -0800, satishkingdom@GMAIL.COM wrote:
>
>>What is pointers in sas?
0
Reply jim2stat (833) 11/6/2006 9:50:24 AM

4 Replies
230 Views

(page loaded in 0.045 seconds)


Reply: