Velocity Reviews > Pointer memory allocation

# Pointer memory allocation

Charles M. Reinke
Guest
Posts: n/a

 05-25-2005
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, or not until I assign it some value such as:

*p = 7;

Also, how is this different (if at all) from the case where I create a null
pointer:

int *p;
p = NULL;

Is the memory space for one integer reserved in this case, or just the
memory space for a pointer with value 0?
This is actually a very simplified case of what I'm trying to do, but
hopefully my question is clearer this way.
Thanx!
--
Charles M. Reinke
Georgia Institute of Technology
School of Electrical and Computer Engineering
(404) 385-2579

August Karlstrom
Guest
Posts: n/a

 05-25-2005
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, or not until I assign it some value such as:
>
> *p = 7;

No, to allocate memory for one integer you have to do:

p = malloc(sizeof (int));

and then

*p = 7;

> Also, how is this different (if at all) from the case where I create a null
> pointer:
>
> int *p;
> p = NULL;
>
> Is the memory space for one integer reserved in this case, or just the
> memory space for a pointer with value 0?

No.

-- August

Lew Pitcher
Guest
Posts: n/a

 05-25-2005
-----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-----

Martin Ambuhl
Guest
Posts: n/a

 05-25-2005
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. You have declared only a pointer, not the space to which it points.

> or not until I assign it some value such as:
>
> *p = 7;

No, that is a severe error. p is a wild pointer, and you have just
stored 7 into your car's ignition system.

> Also, how is this different (if at all) from the case where I create a null
> pointer:
>
> int *p;
> p = NULL;

> Is the memory space for one integer reserved in this case, or just the
> memory space for a pointer with value 0?

The same situation: you have declared only one object, the pointer p.
At least this time you gave it an initial value, even if it is still a
severe error to attempt dereferencing it.

> This is actually a very simplified case of what I'm trying to do, but
> hopefully my question is clearer this way.

Please check the sections in the FAQ and in your textbook on pointer
usage. You seem not to have a clue.

Bhatnagar Achindra
Guest
Posts: n/a

 05-26-2005
When you declare a pointer, depending on the system processor word
size, a memory is allocated to pointer, say 32-bit in i386. It holds
pointer, i.e what ever pointer points to.

When you set pointer to some value, it stores the address of that
object in the memory where it was allocated.

To reserve space for some object u need to use malloc( ) function.

Charles M. Reinke
Guest
Posts: n/a

 05-26-2005
"Charles M. Reinke" <(E-Mail Removed)> wrote in message
news:d7302d\$e62\$(E-Mail Removed)...
> 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, or not until I assign it some value such as:
>
> *p = 7;
>
> Also, how is this different (if at all) from the case where I create a

null
> pointer:
>
> int *p;
> p = NULL;
>
> Is the memory space for one integer reserved in this case, or just the
> memory space for a pointer with value 0?
> This is actually a very simplified case of what I'm trying to do, but
> hopefully my question is clearer this way.
> Thanx!
> --
> Charles M. Reinke

Thanx everyone--you're answers were very helpful! In hindsight, my example
was rather stupid, but I appreciate your effort in figuring out what I was

-Charles