473,748 Members | 7,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3682
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*******@redif fmail.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*******@redif fmail.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*******@redif fmail.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.ne t
Nov 13 '05 #6

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

Similar topics

2
5342
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 p; Creates a 2dimensional array. p can be thought of as a pointer, containing the adres of the first element in the array. The memory is
2
2650
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
8139
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 alignment) { unsigned long ptr, buf; ASSERT( alignment > 0 );
7
1857
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 code that basically acts as a server and that will be running for weeks or months and probably even longer, memory management is a topic that is quite crucial.
5
4136
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, or not until I assign it some value such as: *p = 7;
2
1929
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 pointer vs as just a member? Cheers, Jon Rea
12
5052
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 if I understand correctly, its 3 * void(*) + sizeof(T) * size_of_vector. (3 ptrs for __M_start, __M_finish and __M_end_of_storage). If any STL guru could confirm my findings, that would be much appreciated.
62
17840
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
1
7973
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.