Is memory allocation necessary for integer pointer

U

Uday Joshi

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
 
R

Russell Hanneken

Uday said:
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
(e-mail address removed)
 
A

Artie Gold

Uday said:
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
 
P

Paul Hsieh

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

Christian Bau

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

Dave Thompson

(e-mail address removed) (Uday Joshi) wrote:
[...] 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
 

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

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top