Re: Debug my program please.



Richard E Maine <nospam@xxxxxxxxxxxxx> wrote:
> Jugoslav Dujic <jdujic@xxxxxxxxx> wrote:

>> [ functions returning pointers ]

> 3) Not so much a gotcha as a "what is the benefit?" To me, the whole
> point of a function is that you can use it as part of an expression.

Good point, but pointer expressions do occur in practice. In
addition to Jugoslav's example where a returned pointer is passed
directly to another subprogram, there is

x= lookup(hashtable, key)%somefield

where lookup() returns a pointer to some type stored in the hashtable.
For the most part this would be a pointer to already existing data.
However I can see cases where it is convenient for lookup() to
allocate new data (with default values) when the key isn't found.
There is no memory leak because the new data is properly referenced
by the hashtable. A real-life example of mine went like

lookup(sparse_matrix, iglobal, jglobal)+= element_matrix(i,j)

but that was in C, where there is a "+=" operator :-)


--
pa at panix dot com
.



Relevant Pages