Pointer memory allocation

C

Charles M. Reinke

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!
 
A

August Karlstrom

Charles said:
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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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+1SkywJVfjB1ipjLSsMTQCcDeSr
/xjEhYgnJQVbGL794UKdJcU=
=3MG/
-----END PGP SIGNATURE-----
 
M

Martin Ambuhl

Charles said:
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.
 
B

Bhatnagar Achindra

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.
 
C

Charles M. Reinke

Charles M. Reinke said:
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!

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
trying to ask.

-Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top