On Monday 17 September 2007 20:10:36 Alex Esplin wrote:
> On 9/17/07, Derek Davis <[EMAIL PROTECTED]> wrote:
> > The memory that temp points to is indeed on the heap. However, the
> > value of array is passed by value, so it will be the same value when
> > it returns. If it were passed by reference, as preceding the
> > parameter with an & would do, the new value would be returned. What
> > this means is that within the function, you delete what array points
> > to, then tell array to point to temp. When the function returns,
> > array is not changed due to pass-by-value, so it points back to where
> > it did before the function, which is memory that you have now deleted.
> > The memory you allocated in the function is still there, but noone is
> > pointing to it.
>
> Arrays (or char**) are pointers and not passed by value. When you
> pass an array as an argument to a function, you are passing the
> pointer to the 0th (first) location in that array.
You're passing the pointer to the beginning of the array by value _not_ the
array itself. (Derek was referring to the value of array, the variable, a
char **, not the array of char * that it is pointing to)
Since array is a copy of the original in the calling function, changing it is
pointless -- array (in the caller) remains the same (which is bad, since
you've deleted that data and you've failed to keep track of the
replacement--double whammy, segfault and memleak).
---Mike Larsen
--
Politicians are like diapers...
they should be changed often, and for the same reason
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
--------------------
BYU Unix Users Group
http://uug.byu.edu/
The opinions expressed in this message are the responsibility of their
author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info: http://uug.byu.edu/mailman/listinfo/uug-list