Pointer memory allocation

Discussion in 'C Programming' started by Charles M. Reinke, May 26, 2005.

  1. 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
    Charles M. Reinke, May 26, 2005
    #1
    1. Advertising

  2. 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
    August Karlstrom, May 26, 2005
    #2
    1. Advertising

  3. Charles M. Reinke

    Lew Pitcher Guest

    -----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+1SkywJVfjB1ipjLSsMTQCcDeSr
    /xjEhYgnJQVbGL794UKdJcU=
    =3MG/
    -----END PGP SIGNATURE-----
    Lew Pitcher, May 26, 2005
    #3
  4. 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.
    Martin Ambuhl, May 26, 2005
    #4
  5. 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.
    Bhatnagar Achindra, May 26, 2005
    #5
  6. "Charles M. Reinke" <> wrote in message
    news:d7302d$e62$...
    > 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
    trying to ask.

    -Charles
    Charles M. Reinke, May 26, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maedowan
    Replies:
    9
    Views:
    10,105
    Gianni Mariani
    Sep 6, 2004
  2. Uday Joshi
    Replies:
    5
    Views:
    373
    Dave Thompson
    Sep 1, 2003
  3. Yossarian

    Pointer to Pointer & Mem allocation

    Yossarian, Jun 7, 2004, in forum: C Programming
    Replies:
    2
    Views:
    3,658
    Mike Wahler
    Jun 7, 2004
  4. Ken
    Replies:
    24
    Views:
    3,832
    Ben Bacarisse
    Nov 30, 2006
  5. chris
    Replies:
    6
    Views:
    971
    chris
    Oct 28, 2005
Loading...

Share This Page