Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > regarding dynamic allocation for pointers

Reply
Thread Tools

regarding dynamic allocation for pointers

 
 
sam_cit@yahoo.co.in
Guest
Posts: n/a
 
      01-06-2007
Hi Everyone,

I just heard from a friend of mine that there are few c compilers that
give an error when pointers are not initialised to NULL. Is it correct?
and if so, is there any standard for that?

Thanks in advance...

 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      01-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi Everyone,
>
> I just heard from a friend of mine that there are few c compilers that
> give an error when pointers are not initialised to NULL. Is it correct?
> and if so, is there any standard for that?
>
> Thanks in advance...
>

Incorrect. There is no requirement in the Standard that any pointer be
initialized to NULL by the programmer. If the Standard wants a new
pointer to be NULL, the compiler does it. Static pointers at file scope
for example.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-06-2007
(E-Mail Removed) wrote:
>
> I just heard from a friend of mine that there are few c compilers
> that give an error when pointers are not initialised to NULL. Is
> it correct? and if so, is there any standard for that?


See below for standards (C99). Another option is N1124, but that
is not available in text form. There is no reason to initialize
pointers to NULL. There are many reasons to not derefernce
unitialized and invalid pointers, or NULL pointers.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net> (C-info)


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      01-06-2007
On 6 Jan 2007 05:08:24 -0800, (E-Mail Removed) wrote:

>Hi Everyone,
>
> I just heard from a friend of mine that there are few c compilers that
>give an error when pointers are not initialised to NULL. Is it correct?
>and if so, is there any standard for that?
>


Did you really mean error or is it actually just a warning.

Unfortunately, the standard does not prohibit diagnostics for
non-erroneous code. The standard also does not distinguish between
informational messages, warning messages, error messages, etc.
Consequently, in addition to the required diagnostics (e.g.,
constraint violations), the compiler writer is allowed to add any and
as many additional ones as he wants. It becomes a quality of
implementation issue.

The standard does require the compiler to accept a correct program
even if it issued optional diagnostics.

Some compilers use this flexibility intelligently, such as checking
the types of printf arguments against the conversion specifiers and
reporting mismatches. Others, in my opinion, go overboard (as in the
original post) and apparently do so inconsistently (why not flag other
uninitialized objects besides pointers).


Remove del for email
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-06-2007
(E-Mail Removed) wrote:
> Hi Everyone,
>
> I just heard from a friend of mine that there are few c compilers that
> give an error when pointers are not initialised to NULL. Is it correct?
> and if so, is there any standard for that?
>
> Thanks in advance...


Any self-respecting compiler *shouldn't* emit an error for an
uinitialised pointer. I don't think the standard prohibits issuing a
diagnostic, (which would be more useful than an error), though. However
a compiler is bound to compile a correct translation unit.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-06-2007
(E-Mail Removed) writes:
> I just heard from a friend of mine that there are few c compilers that
> give an error when pointers are not initialised to NULL. Is it correct?
> and if so, is there any standard for that?


In what context, and what exactly do you mean by "give an error"?

Pointer objects can be initialized to NULL, initialized to some other
value, or uninitialized. I don't think there's any context in which a
compiler is required to issue a diagnostic for an uninitialized or
null pointer. A decent compiler might issue a non-fatal warning on an
attempt to *use* a null or uninitialized pointer.

Show us an example, and we can tell you (a) what a compiler is
required to do, and (b) what a compiler is allowed to do.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-06-2007
CBFalconer said:

> (E-Mail Removed) wrote:
>>
>> I just heard from a friend of mine that there are few c compilers
>> that give an error when pointers are not initialised to NULL. Is
>> it correct? and if so, is there any standard for that?

>
> See below for standards (C99). Another option is N1124, but that
> is not available in text form. There is no reason to initialize
> pointers to NULL.


<cough> I beg to differ. I can see why you might find reasons not to
initialise, but to say there is *no* reason to initialise is a bit strong,
isn't it?

> There are many reasons to not derefernce
> unitialized and invalid pointers, or NULL pointers.


And that's a great reason for initialising pointers to NULL if you have no
better value for them - so that you can find out whether you can legally
dereference them by inspecting their value.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      01-06-2007
Richard Heathfield wrote:
> CBFalconer said:
>
> > (E-Mail Removed) wrote:
> >>
> >> I just heard from a friend of mine that there are few c compilers
> >> that give an error when pointers are not initialised to NULL. Is
> >> it correct? and if so, is there any standard for that?

> >
> > See below for standards (C99). Another option is N1124, but that
> > is not available in text form. There is no reason to initialize
> > pointers to NULL.

>
> <cough> I beg to differ. I can see why you might find reasons not to
> initialise, but to say there is *no* reason to initialise is a bit strong,
> isn't it?


Agreed (although I assume this is merely poor choice of wording on
CBFalconer's part), but...

> > There are many reasons to not derefernce
> > unitialized and invalid pointers, or NULL pointers.

>
> And that's a great reason for initialising pointers to NULL if you have no
> better value for them - so that you can find out whether you can legally
> dereference them by inspecting their value.


....it's only in rare situations that you'll really need this.
Statically allocated variables are implicitly initialised to zero, for
auto variables it is almost always possible to not use them until
you've set them to a valid (and non-null) value, and it is impossible
to initialise dynamically allocated memory except by use of the
calloc() function, which is not guaranteed to work as one might expect
for pointers in the first place.

Initialising pointers to NULL can occasionally be good style, though.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-07-2007
Harald van D?k said:

> Richard Heathfield wrote:
>> CBFalconer said:
>>

<snip>
>
>> > There are many reasons to not derefernce
>> > unitialized and invalid pointers, or NULL pointers.

>>
>> And that's a great reason for initialising pointers to NULL if you have
>> no better value for them - so that you can find out whether you can
>> legally dereference them by inspecting their value.

>
> ...it's only in rare situations that you'll really need this.
> Statically allocated variables are implicitly initialised to zero,


Yes, but for me they are the exception rather than the rule.

> for
> auto variables it is almost always possible to not use them until
> you've set them to a valid (and non-null) value,


Sure, but what's to stop Joe Maintainer from slipping in a deref by mistake,
halfway between declaration and first assignment? I'd rather make his
debugging job a bit easier by giving him a null pointer to detect.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-07-2007
Richard Heathfield wrote:
> Harald van D?k said:
>
>>for
>>auto variables it is almost always possible to not use them until
>>you've set them to a valid (and non-null) value,

>
>
> Sure, but what's to stop Joe Maintainer from slipping in a deref by mistake,
> halfway between declaration and first assignment? I'd rather make his
> debugging job a bit easier by giving him a null pointer to detect.
>

And his compiler or lint's job a bit harder?

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding Dynamic Memory Allocation saurabh29789 C Programming 1 02-10-2009 03:58 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Dynamic mem allocation for pointers srini C Programming 4 12-18-2005 02:55 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
strings, arrays, pointers and dynamic memory allocation swarsa@msn.com C Programming 5 12-24-2004 06:59 PM



Advertisments