HELP:Pointers in function arguement
Author Message
HELP:Pointers in function arguement

(initialization deleted)
: void crossover(...., ...., unsigned long **c1, unsigned long **c2)
: {
:       unsigned long x,y;
:
:       *c1 = &x;
:       *c2 = &y;

You should know that x and y are allocated with a scope that is only
within crossover. When you take the address of x or y (&x &y) it is only
valid within the crossover function. When crossover terminates, your
assignments are not meaningful and will not produce correct results. I'm
suspecting that you should call crossover with (..., ..., c1, c2) and
then within crossover assign *c1 = x and *c2 = y. That will copy the
value of x to the bytes allocated by your mem(sizeof(unsigned long)) area.
--

Sun, 06 Jul 1997 00:46:38 GMT
HELP:Pointers in function arguement
|> (initialization deleted)
|> : void crossover(...., ...., unsigned long **c1, unsigned long **c2)
|> : {
|> :         unsigned long x,y;
|> :
|> :         *c1 = &x;
|> :         *c2 = &y;
|>
|> You should know that x and y are allocated with a scope that is only
|> within crossover. When you take the address of x or y (&x &y) it is only

Let us keep terminology straight here. What you are talking about is lifetime or
duration not scope. `static unsigned long x, y; would also have the same _scope_,
but then you could correctly return c1 and c2.

|> valid within the crossover function. When crossover terminates, your
|> assignments are not meaningful and will not produce correct results. I'm
|> suspecting that you should call crossover with (..., ..., c1, c2) and
|> then within crossover assign *c1 = x and *c2 = y. That will copy the
|> value of x to the bytes allocated by your mem(sizeof(unsigned long)) area.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]

Sun, 06 Jul 1997 02:59:44 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages