473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Is memory allocation necessary for integer pointer

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
Nov 13 '05 #1
5 3667
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

Nov 13 '05 #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
rh*******@pobox.com

Nov 13 '05 #3
jo*******@rediffmail.com (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/
Nov 13 '05 #4
In article <a8**************************@posting.google.com >,
jo*******@rediffmail.com (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.
Nov 13 '05 #5
On 26 Aug 2003 23:55:50 -0700, qe*@pobox.com (Paul Hsieh) wrote:
jo*******@rediffmail.com (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
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hall | last post by:
I have a question regarding where memory is allocated when arrays are created. I'll illustrate this by example. I may be wrong on some details, do feel free to correct me. The code piece: int...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
5
by: Charles M. Reinke | last post by:
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,...
2
by: Jon Rea | last post by:
Following up on: > Any reason for creating iv dynamically? in "Re: Namespace and #Include best practises" What are the rules for when member data should be created as new memory with a member...
12
by: Varun Kacholia | last post by:
Apologies if this has been answered somewhere, but Google did not produce any concrete results. I would like to find out the memory footprint of a vector<T>. I tried to dig in the STL code and...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.