homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam

The Geek Culture Forums


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» The Geek Culture Forums   » Other Geeky Stuff   » Ask a Geek!   » linked list/pointers-- default values?

 - UBBFriend: Email this page to someone!    
Author Topic: linked list/pointers-- default values?
californiarockr
Assimilated
Member # 1986

Member Rated:
3
Icon 1 posted April 30, 2005 18:40      Profile for californiarockr     Send New Private Message       Edit/Delete Post   Reply With Quote 
What default values do you use for a linked list in C++ using pointers? I used to use char "\0", but then I started getting NULL Pointer exceptions one day. My new professor used 0 in his examples, but now I get segmentation faults. What should I use? I use the value to check for end of linked lists. I could throw a bool in to determine the tail, but thats more coding... Can I use -1 or something?

This is OS X, but I have had issues on winders too

--------------------
lol

Posts: 361 | From: guess... :/ | Registered: Feb 2003  |  IP: Logged
quantumfluff
BlabberMouth, a Blabber Odyssey
Member # 450

Member Rated:
5
Icon 1 posted April 30, 2005 20:04      Profile for quantumfluff     Send New Private Message       Edit/Delete Post   Reply With Quote 
First of all 0 is not NULL. 0 is a number - always. Your compiler may be forgiving, but that's IMO a bug in the compiler. NULL is required to be something that is always a legal pointer (of any type) value. It happens to have a bit representation of all zeros, but that's beside the point.

That said, a singly linked list always has NULL as the last element. A doubly linked list generally has a head element, which would link to itself if the list were empty. No pointers in that kind of list would be null.

Posts: 2902 | From: 5 to 15 meters above sea level | Registered: Jun 2000  |  IP: Logged
csk

Member # 1941

Member Rated:
5
Icon 1 posted April 30, 2005 22:05      Profile for csk     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by californiarockr:
What default values do you use for a linked list in C++ using pointers? I used to use char "\0", but then I started getting NULL Pointer exceptions one day.

I'd be using NULL, as you are doing. However, null pointer exceptions happen when you try and dereference a pointer that has a null value.

Of course, you should never be doing anything with a pointer that could be NULL other than checking for NULLarity. So I'd postulate that the right thing to do is work out why you are trying to dereference NULL pointers, and fix your code accordingly.

--------------------
6 weeks to go!

Posts: 4455 | From: Sydney, Australia | Registered: Jan 2003  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted May 01, 2005 21:46      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by quantumfluff:
First of all 0 is not NULL. 0 is a number - always. Your compiler may be forgiving, but that's IMO a bug in the compiler. NULL is required to be something that is always a legal pointer (of any type) value. It happens to have a bit representation of all zeros, but that's beside the point.

That said, a singly linked list always has NULL as the last element. A doubly linked list generally has a head element, which would link to itself if the list were empty. No pointers in that kind of list would be null.

NULL is 0 by defintion, as is a plain #define statment. In C++, things aren't very strongly typed. There are lots of instances where this becomes useful, like using chars to hold binary bytes. Some people think this is an abuse of the language, but others think that it is an feature of the language. If your looking for a stronger typing, use another language.

0's or NULL work... But you need to make sure that as elements are added, and removed properly, and that a NULL is never derefferenced.

--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
quantumfluff
BlabberMouth, a Blabber Odyssey
Member # 450

Member Rated:
5
Icon 1 posted May 02, 2005 06:10      Profile for quantumfluff     Send New Private Message       Edit/Delete Post   Reply With Quote 
Sorry GameMaster, but you're wrong on that one. NULL is very rarely 0. It is usually ((void *)0) or ((char *)0), depending on the compiler. The statement
SomeStruct *x = 0;
will be a compile time error on many compilers, whereas
SomeStruct *x = NULL;
is always legitimate. Code that relies on lax type enforcement should be taken out and shot.

Posts: 2902 | From: 5 to 15 meters above sea level | Registered: Jun 2000  |  IP: Logged
spungo
BlabberMouth, a Blabber Odyssey
Member # 1089

Member Rated:
4
Icon 1 posted May 02, 2005 06:56      Profile for spungo     Send New Private Message       Edit/Delete Post   Reply With Quote 
IMHO, this is a bit of a design fault with C: i.e., the null ascii character having the same label as the null pointer. Some folk like to separate the labels, for example with a -

#define NUL '\0'

(and only using 'NULL' for pointers).

- which kinda makes sense, but is a bit confusing, really, isn't it? [Smile]

--------------------
Shameless plug. (Please forgive me.)

Posts: 6529 | From: Noba Scoba | Registered: Jan 2002  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted May 02, 2005 12:33      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
It's still true that the language isn't very strongly typed, and it is true that some believe
char* ptr = 0;
is fine and dandy (as some (most?) compilers won't complain).

Granted, that using what always works is better (especially when planning on porting), but the standards don't weigh in on this one. So, I do bow to you on this one that NULL should be used.

In anycase, if his prof.'s example uses 0, then the compiler they are using doesn't mind it; and, that means that his problem is elsewhere (probably in an insert or remove).

--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
spungo
BlabberMouth, a Blabber Odyssey
Member # 1089

Member Rated:
4
Icon 1 posted May 02, 2005 12:40      Profile for spungo     Send New Private Message       Edit/Delete Post   Reply With Quote 
Myeh - the trouble with C is that what you read in books and such ain't always the best guidance - 'gets' is still part of ANSI C, for example.

You may get away with

char *ptr = 0;

but it (may be) harmful in that it obscures how you should treat pointers. That you can get away with it is not enough.

--------------------
Shameless plug. (Please forgive me.)

Posts: 6529 | From: Noba Scoba | Registered: Jan 2002  |  IP: Logged
californiarockr
Assimilated
Member # 1986

Member Rated:
3
Icon 1 posted May 03, 2005 09:15      Profile for californiarockr     Send New Private Message       Edit/Delete Post   Reply With Quote 
In Comp Sci 1 I was one of the few to understand null. Its like slopes, you can have 0 slope, or a undefined. Null is a lack of data, 0 is data

--------------------
lol

Posts: 361 | From: guess... :/ | Registered: Feb 2003  |  IP: Logged
spungo
BlabberMouth, a Blabber Odyssey
Member # 1089

Member Rated:
4
Icon 1 posted May 03, 2005 10:05      Profile for spungo     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by californiarockr:
Null is a lack of data, 0 is data

Huh? If NULL was a lack of data, how would you test against it, as in -

if (ptr == NULL) ??

It isn't a #ifdef, is it?

(edit: sorry - I don't mean to come across as a 'semantics nazi', but it clearly is data, Jim - just not as we know it. )

--------------------
Shameless plug. (Please forgive me.)

Posts: 6529 | From: Noba Scoba | Registered: Jan 2002  |  IP: Logged
jfw
Solid Nitrozanium SuperFan!
Member # 1923

Member Rated:
5
Icon 1 posted May 03, 2005 12:09      Profile for jfw     Send New Private Message       Edit/Delete Post   Reply With Quote 
As often happens when the C null pointer is discussed, there has been some confusion. I don't happen to have my draft copy of the ANSI C standard handy (it was a draft copy because I was helping to review it), but I do have the ISO C++ standard handy, and hereby lay down The Law for C++ null pointers.

quote:

4.10. Pointer Conversions.

A null pointer constant is an integral constant expression ... rvalue of integral type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is a null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type.

quote:

18.1 Types

Common definitions.

The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10).

In C++, legal definitions of the NULL macro in <cstddef> include

#define NULL 0
#define NULL (3-3)
#define NULL (4/5 - 7/8)

Legal definitions of the NULL macro do not include

#define (void*)0
#define (char*)0
extern int __guaranteed_to_be_zero;
#define __guaranteed_to_be_zero

Note that ANSI C is slightly more flexible than C++ on the definition of the NULL macro; it is permitted to be either a null pointer constant (see above) or a null pointer constant cast to (void*). Those are the only two permissible choices in ANSI C. Any compiler which offers, for example, (char *)0 as a definition of NULL is not a conforming C implementation nor a conforming C++ implementation.

It is not a bug that you are allowed to spell the null pointer as
0, it is a requirement.

A null pointer value (as distinct from a null pointer constant) does not need to be all-zero-bits; the C++ standard does not sully itself with irritating low-level details like that, but the C standard makes it clear enough. There are conforming C compilation systems where null pointer values of various types do not have all-zero-bits representations; these conforming C compilation systems still spell the null pointer constant as 0. Note that this means that
code:
int zero = 0;
int *pNull = 0;
if (zero == (int)pNull) puts("yes");

does not necessary output the string "yes", but that's not necessarily going to happen on any compiler, of course. The conversion from pointer type to integral type is implementation-defined behavior, not standard-defined behavior, and the implementation is at liberty to define the behavior as "all pointers convert to the integer 42", just as the implementation is at liberty to define the results of casting an integer to a pointer as "the result is a pointer to a block of memory containing the EBCDIC characters "Please Don't Do That."

As to the original question: "what default values do you use for linked-list pointers in C++", the answers already given are pretty good (null pointer is usual for a single-linked list, either null or a sentinel for a doubly-linked list); sometimes a doubly-linked list package will initialize an object's link pointers to itself, i.e.

code:
struct link {
link *pNext, *pPrev;
link() pNext(this), pPrev(this) {}
...
}

Sometimes that can make the insert operation easier to write; sometimes that can complicate things, and then you might favor NULL.

If you are getting segmentation faults in your code, maybe it would help if you could post a code snippet.

Posts: 20 | From: Boxboro Massachusetts | Registered: Jan 2003  |  IP: Logged
Whyren
Maximum Newbie
Member # 3787

Member Rated:
5
Icon 1 posted May 03, 2005 19:38      Profile for Whyren     Send New Private Message       Edit/Delete Post   Reply With Quote 
Correct me if I'm wrong, but 0 should be acceptable as null AS LONG as you're working with chars; if you're using int (or other number data types), using 0 will assign the ASCII value of zero, which is not null.
Posts: 10 | From: Midlin USA | Registered: Apr 2005  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted May 03, 2005 20:52      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
No. If you read the post by jfw, I think the answer is quite clear.

"It is not a bug that you are allowed to spell the null pointer as 0, it is a requirement."

--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
Groggle
Mini Geek
Member # 2360

Member Rated:
3
Icon 1 posted May 04, 2005 20:21      Profile for Groggle     Send New Private Message       Edit/Delete Post   Reply With Quote 
For _most_ platforms, 0 is equivalent to NULL.

Having said that, never use '\0' as a NULL - that will assign a byte-wise zero value to the pointer, but does not guarantee that all bytes of the pointer are zeroed.

C/C++ are rather rude languages, and do not guarantee that memory pages are in fact zero-filled unless you specifically ask for it (e.g. using calloc()) As a result, there is a distinct possibility that you get random crap off the system heap with any given variable.

So - going back to californiarockr's starting example, you get this kind of possibility:

int *ptr1 = '\0'

will often be (in memory) 00 00 00 00
but could be: 00 53 27 81 FE

where only the first byte of the ptr1 is set to '\0' by the assignment. (Most compilers will complain about coercing a char value to an int*, but not all. If your compiler has K&R rules turned on instead of ANSI, then it will quite happily let you do the assignment, and you get to figure out the mistakes the hard way.

My rule of thumb - if you want NULL, use the constant NULL. Don't use '\0', don't use integer 0 especially on 64 bit platforms, where you get another casting problem in that ints are 32 bit, and longs are 64 bit. A constant like 0 will be assumed to be 32 bit by a lot of compilers.

(How do I know this? I've done product ports from 16 -> 32 bit platforms, and 32 -> 64 bit platforms far too many times in my career)

In C/C++ - always CAST explicitly.

e.g.

long i = (long)(55);

Don't assume the compiler will get it right - most of the time it will, and the one time you need it to, will be when you discover the latent bug in the compiler!

Posts: 77 | From: Calgary | Registered: Aug 2003  |  IP: Logged


All times are Eastern Time  
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

Contact Us | Geek Culture Home Page

2015 Geek Culture

Powered by Infopop Corporation
UBB.classicTM 6.4.0



homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam