Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > NULL in C

Reply
Thread Tools

NULL in C

 
 
Micah Cowan
Guest
Posts: n/a
 
      10-19-2003
"Serve La" <(E-Mail Removed)> writes:

> C programmers get accused of writing platform dependant code when they write
> int *x = 0;
> They have to write
> int *x = NULL;
> because some platforms don't represent NULL as all bits zero.
>
> C++ programmers don't seem to have this problem, they can write 0 whenever
> they want. Why is that?


Neither do C programmers. The fact that NULL is not represented
as all bits zero is:

1) Not limited to C. This is also true of C++.
2) Not relevant to the initializations you have above.

Where it becomes a problem is more in code such as:

void **foo = calloc(NUM_ELEMS * sizeof *foo);
/* ... Code that assumes that foo[0] through foo[NUM_ELEMS-1]
are NULL pointers due to the all-bits-zero initialization from
calloc()... */

HTH,
Micah
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      10-19-2003
Serve La wrote:

> C programmers get accused of writing
> platform dependant code when they write
> int *x = 0;


The accusers are ignorant.

> They have to write
> int *x = NULL;
> because some platforms don't represent NULL as all bits zero.
>
> C++ programmers don't seem to have this problem,
> they can write 0 whenever they want.


> Why is that?


Your question depends on an invalid hypothesis.

--
pete
 
Reply With Quote
 
 
 
 
Serve La
Guest
Posts: n/a
 
      10-19-2003
"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:_Jhkb.2609$(E-Mail Removed) ink.net...
> > Why is that?

>
> I don't know why your collection of at best half-truths exists. Your post
> is one of the worst attempts at a troll I've ever seen.


Troll!?!? More misinformed than trolling and it was somebody here who said
it BTW.

Here's the worst troll. "Pascal is better than C"


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-19-2003
Irrwahn Grausewitz wrote:
>
> "Scott Fluhrer" <(E-Mail Removed)> wrote:
>
> >
> >"Mathew Hendry" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed).. .
> >> On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <(E-Mail Removed)> wrote:
> >>
> >> >C programmers get accused of writing platform dependant code when they

> >write
> >> >int *x = 0;
> >> >They have to write
> >> >int *x = NULL;
> >> >because some platforms don't represent NULL as all bits zero.
> >>
> >> The accusers are misinformed. This is a common source of confusion. NULL

> >and
> >> 0 are interchangable with very few exceptions.

> >int i = 0; /* Groovy */
> >int j = NULL; /* An error on some compilers */
> >
> >

>
> Er, seems you're just joking, but anyway: the discussion was about
> 0 vs. NULL in pointer contexts.
>
> int *i = 0; /* valid */
> int *j = NULL; /* more idiomatic variant */
> printf("%p",0); /* undefined behaviour */
>

The variadic printf() requires (void*)0 because the compiler doesn't
parse the format string and doesn't know the context of 0. If the
implementation has defined 'NULL 0' then (void*)NULL will be required.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
"Serve La" <(E-Mail Removed)> wrote:

>"Martin Ambuhl" <(E-Mail Removed)> wrote in message
>news:_Jhkb.2609$(E-Mail Removed) link.net...
>> > Why is that?

>>
>> I don't know why your collection of at best half-truths exists. Your post
>> is one of the worst attempts at a troll I've ever seen.

>
>Troll!?!? More misinformed than trolling and it was somebody here who said
>it BTW.


If somebody here said so, he has most certainly been corrected.
Or maybe you just misunderstood something.

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
Joe Wright <(E-Mail Removed)> wrote:

>Irrwahn Grausewitz wrote:

<snip>
>> 0 vs. NULL in pointer contexts.
>>
>> int *i = 0; /* valid */
>> int *j = NULL; /* more idiomatic variant */
>> printf("%p",0); /* undefined behaviour */
>>

>The variadic printf() requires (void*)0 because the compiler doesn't
>parse the format string and doesn't know the context of 0. If the
>implementation has defined 'NULL 0' then (void*)NULL will be required.


That's the point.
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      10-19-2003
In article <bmrjb2$nfv$(E-Mail Removed)1.nb.home.nl>,
"Serve La" <(E-Mail Removed)> wrote:

> C programmers get accused of writing platform dependant code when they write
> int *x = 0;
> They have to write
> int *x = NULL;
> because some platforms don't represent NULL as all bits zero.
>
> C++ programmers don't seem to have this problem, they can write 0 whenever
> they want. Why is that?


That's not a problem of the C language, that is a problem with people
who don't understand it.

Come back when you understand the difference between an integer zero, a
value with a representation of all bits zero, a null pointer, a null
pointer constant and the NULL macro.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-19-2003
Micah Cowan <(E-Mail Removed)> writes:
[...]
> Neither do C programmers. The fact that NULL is not represented
> as all bits zero is:
>
> 1) Not limited to C. This is also true of C++.
> 2) Not relevant to the initializations you have above.


To be precise, NULL is not *necessarily* represented as all bits zero.
On many implementations, NULL is represented as all bits zero (which
can make it more difficult to detect bugs caused by code that makes
this assumption).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-20-2003
In <(E-Mail Removed)> Micah Cowan <(E-Mail Removed)> writes:

>Neither do C programmers. The fact that NULL is not represented
>as all bits zero is:


NULL is a macro. Its usual representations are 0 or ((void *)0).
If you mean null pointer, NULL is *not* a valid abbreviation.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
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
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
Is there a null ostream (like /dev/null) in cpp? Bo Peng C++ 13 07-18-2004 07:17 PM



Advertisments