-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Charles M. Reinke wrote:
OK, this may be a dumb question, but please bear with me. If I declare a
pointer:
int *p;
the memory space for a pointer is allocated, but is the memory space also
reserved for one integer,
No. Only the pointer is "allocated"
or not until I assign it some value such as:
*p = 7;
No. If you declare a pointer, you must first assign it a value before you can
assign the pointed-to object a value. To assign a value to the pointed-to
object before you've assigned a value to the pointer is to invoke undefined
behaviour.
That is to say
{
int *p;
*p = 7;
}
is wrong, and can (and likely will) cause problems, but
{
int q;
int *p;
p = &q;
*p = 7;
}
is right.
Also, how is this different (if at all) from the case where I create a null
pointer:
int *p;
p = NULL;
Very different.
In the first case, you didn't give the pointer /any/ value
In the 2nd case, you give the pointer a value of NULL
Is the memory space for one integer reserved in this case, or just the
memory space for a pointer with value 0?
In both cases, only the space for the pointer is allocated. You have to
allocate the space for the integer seperately.
This is actually a very simplified case of what I'm trying to do, but
hopefully my question is clearer this way.
Thanx!
Here's an analogy: a pointer is like a street address; it tells of a location
for a building, but doesn't actually create the building. You can have a
street address without having a building on the addressed property, but a
family can't move in to the address until a house is built on the property.
Similarly, a pointer names the location of an object, but you can't move a
value into the pointed to object until the pointed-to object is allocated.
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (
http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFClQo2agVFX4UWr64RApV7AKDMgKMgY+1SkywJVfjB1i pjLSsMTQCcDeSr
/xjEhYgnJQVbGL794UKdJcU=
=3MG/
-----END PGP SIGNATURE-----