Is memory allocation necessary for integer pointer

Discussion in 'C Programming' started by Uday Joshi, Aug 27, 2003.

  1. Uday Joshi

    Uday Joshi Guest

    Hi

    look at the code below
    /* test.c */
    int main(int argc, char *argv[])
    {
    int *x;
    *x = 10;
    printf("%d\n", *x);
    return 0;
    }

    The program when compiled using gcc on Redhat Linux prints 10.
    Compiled as "gcc test.c"

    the question is, Is it legal not to allocate memory for the integer
    pointers ? If yes, then which data types need memory allocation and
    which doesn't.

    Uday Joshi
     
    Uday Joshi, Aug 27, 2003
    #1
    1. Advertising

  2. Uday Joshi wrote:
    >
    > look at the code below
    > /* test.c */
    > int main(int argc, char *argv[])
    > {
    > int *x;
    > *x = 10;
    > printf("%d\n", *x);
    > return 0;
    > }
    >
    > The program when compiled using gcc on Redhat Linux prints 10.


    That just means you lucked out this time. Since you didn't initialize
    x, its value is indeterminate; there's no telling where it points. That
    being the case, the effect of dereferencing x is undefined by the C
    standard.

    Regards,

    Russell Hanneken
     
    Russell Hanneken, Aug 27, 2003
    #2
    1. Advertising

  3. Uday Joshi

    Artie Gold Guest

    Uday Joshi wrote:
    > Hi
    >
    > look at the code below
    > /* test.c */
    > int main(int argc, char *argv[])
    > {
    > int *x;


    `x' has some indeterminate value

    > *x = 10;
    > printf("%d\n", *x);


    No declaration of `printf()' is in scope.

    > return 0;
    > }
    >
    > The program when compiled using gcc on Redhat Linux prints 10.
    > Compiled as "gcc test.c"
    >
    > the question is, Is it legal not to allocate memory for the integer
    > pointers ? If yes, then which data types need memory allocation and
    > which doesn't.
    >

    It is not legal.

    You invoked undefined behavior -- which in this case just happened to
    "seem" to work. You got unlucky.

    BTW it is a good idea to always turn up your warnings as high as
    possible.

    <OT>
    In this case,

    gcc -Wall -O -Wunitialized -ansi -pedantic test.c

    would have caught the error.
    </OT>

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Aug 27, 2003
    #3
  4. Uday Joshi

    Paul Hsieh Guest

    (Uday Joshi) wrote:
    > look at the code below
    > /* test.c */
    > int main(int argc, char *argv[])
    > {
    > int *x;
    > *x = 10;
    > printf("%d\n", *x);
    > return 0;
    > }
    >
    > The program when compiled using gcc on Redhat Linux prints 10.
    > Compiled as "gcc test.c"


    The funny thing about C programs is even if they are completely
    blatantly wrong, they may appear to work correct from a testing point
    of view. The reason this works is because the uninitialized value of
    x happened to be pointing to a location in memory that your program
    has read/write access to, and does not affect the successfully exiting
    of you program. But in other platforms, in other circumstances, this
    could easily not be true.

    > the question is, Is it legal not to allocate memory for the integer
    > pointers?


    Not if you are going to read or write to them.

    > [...] If yes, then which data types need memory allocation and
    > which doesn't.


    Any non-zero lengthed data type in C requires either allocation or
    direct declaration if you access them via a pointer. In the pervasive
    ANSI C standard that is out there now, the idea of a 0-lengthed data
    type doesn't make sense (if I recall correctly -- the ANSI C standard
    is a landmine of arbitrary pedanticness.) I believe C99 allows
    0-lengthed arrays (though that's kind of irrelevant since C99 does not
    have any serious support) in which case you *can't* allocate space for
    such a data type anyway.

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
     
    Paul Hsieh, Aug 27, 2003
    #4
  5. In article <>,
    (Uday Joshi) wrote:

    > Hi
    >
    > look at the code below
    > /* test.c */
    > int main(int argc, char *argv[])
    > {
    > int *x;
    > *x = 10;
    > printf("%d\n", *x);
    > return 0;
    > }
    >
    > The program when compiled using gcc on Redhat Linux prints 10.
    > Compiled as "gcc test.c"
    >
    > the question is, Is it legal not to allocate memory for the integer
    > pointers ? If yes, then which data types need memory allocation and
    > which doesn't.


    It is legal not to allocate memory. It is highly illegal to use a
    pointer with an undefined value and a very unfortunate coincidence that
    your program didn't crash.

    That's true for all types.
     
    Christian Bau, Aug 27, 2003
    #5
  6. On 26 Aug 2003 23:55:50 -0700, (Paul Hsieh) wrote:

    > (Uday Joshi) wrote:

    <snip>
    > > [...] If yes, then which data types need memory allocation and
    > > which doesn't.

    >
    > Any non-zero lengthed data type in C requires either allocation or
    > direct declaration if you access them via a pointer. In the pervasive
    > ANSI C standard that is out there now, the idea of a 0-lengthed data
    > type doesn't make sense (if I recall correctly -- the ANSI C standard
    > is a landmine of arbitrary pedanticness.) I believe C99 allows
    > 0-lengthed arrays (though that's kind of irrelevant since C99 does not
    > have any serious support) in which case you *can't* allocate space for
    > such a data type anyway.
    >

    C99 doesn't "allow" 0-length arrays; 6.2.5p20, unchanged from C90
    6.1.2.5 except for footnote numbering:
    An array type describes a contiguously allocated nonempty set of
    objects with a
    particular member object type, called the element type.36)

    It does however add VLAs whose bound is computed at runtime, and for
    those the constraint that requires a diagnostic if the bound is not
    greater than zero does not apply. I.e. a static array bound must be
    greater than zero else it is a required diagnostic; if a VLA bound is
    not greater than zero it is Undefined Behavior.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Sep 1, 2003
    #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,215
    Gianni Mariani
    Sep 6, 2004
  2. R.kumaran
    Replies:
    6
    Views:
    413
    Kevin Easton
    Sep 3, 2003
  3. Yossarian

    Pointer to Pointer & Mem allocation

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

Share This Page