Quick Question: Exclamation Symbol (!)

Mar 30, 2010 at 11:05am
Code for inserting an element at the front of a list:

1
2
3
4
5
6
7
bool insertInFront( IntElement **head, int data ){
IntElement *newElem = new IntElement;
if( !newElem ) return false;
newElen->data = data;
*head = newElem; // Correctly updates head
return true;
}


What does the exclamation symbol (!) mean here? I can't find the answer. Many thanks.
Mar 30, 2010 at 11:08am
Hmmm,

I didn't see something like that before... C-Style?

He checks if new fails.
You can also write:
1
2
if ( newElem != 0 /* OR NULL*/ )
    //... 


bye and have fun dude (:
Mar 30, 2010 at 11:10am
C++ operators and precedence list:
http://msdn.microsoft.com/en-us/library/126fe14k.aspx
Mar 30, 2010 at 11:14am
I still don't get it. What does it mean in front of a pointer?
Mar 30, 2010 at 11:18am
Oh dude you have to read a article about pointers. They are complex but not difficult (:

http://www.cplusplus.com/doc/tutorial/pointers/

Have fun (:
Mar 30, 2010 at 11:22am
No. I mean what does !newElem mean?
Mar 30, 2010 at 11:30am
Did you read my post before?

I wrote:
Hmmm,

I didn't see something like that before... C-Style?

He checks if new fails.
You can also write:

1
2
if ( newElem != 0 /* OR NULL*/ )
    //...  




bye and have fun dude (:
Mar 30, 2010 at 11:32am
I did see your post. But I got the code off a textbook so I assume it's syntactically valid. I would like to know what it means.
Mar 30, 2010 at 3:45pm
He checks if new fails. <- it means this.

if new fails, the pointer would be 0 or NULL or false (thats the same, why i mention false, look at the bottom of my post)
if new successes, the pointer is an address, that means something that is NOT 0.

1
2
3
4
5
6
if (!newElem) ... means
if (!newElemn == true) thats the same as
if (newElem == !true) thats the same as
if (newElem == false) thats the same as
if (newElem == NULL) thats the same
if (newElem == 0)

so it checks if newElem is a NULL-pointer, that means "new" failed the line before.
remember: false = 0 and true is any other value

you could change the above code to
1
2
3
4
5
6
7
if( newElem ) {
  newElen->data = data;
  *head = newElem; // Correctly updates head
  return true;
} else {
  return false;
}

it would have the same effect, but this time it would check if newElem has another value than 0

Last edited on Mar 30, 2010 at 3:47pm
Mar 30, 2010 at 4:22pm
Hey Mathes,

I don't think so, that new returns a NULL-pointer if he couldn't allocate memory. He would raise an exception named 'bad_alloc'. You can catch the exception.

In C you have to check if malloc fails, but not in C++. So this code is not valid C++.

If I'm wrong pls correct me!

bye and have fun (:
Mar 30, 2010 at 7:32pm
Mathes:
How can new ever fail here?

MorningStar:
Thanks for your response! If this is indeed in C, what does it mean to have an exclamation symbol before a pointer type:

!newElem
Mar 30, 2010 at 8:47pm
What if there's no allocatable memory available? If the heap is entirely consumed? After all, the compiler doesn't know, since the allocation will occur at runtime. Where's new going to allocate from then?
The new keyword is designed to try and allocate memory. Although you kind of have to trust it to be able to, it's still not a significant cost to make SURE that it succeeded.
Mar 30, 2010 at 9:30pm
Mar 31, 2010 at 6:15pm
@MorningStar
im not very familiar with C++. needed a lot of C lately and now i just moved on to C++, so excuse me here if its not valid C++ code ;-)
i just checked my book im learning with and it uses try-catch with bad_alloc.
Good, now i know better. Thanks :-)

@WilliamY
look at what tummychow said.
and you should never rely on "it cant be possible here". murphys law: if it can go wrong, it will!
so be sure, that if it happens (even if its a chance of 1 in a million) your program can react and does not just close down.
but it still means the same as we said before: checking if the pointer is a NULL-pointer. if you dont understand my, kinda, confusing post before, sorry, but i dont know how to explain it in another way. (english is not my mother tongue and my english skills arent good enough to explain certain things)
Mar 31, 2010 at 6:42pm
!val evaluates to 1 if val is 0. It's equivalent to val==0.
Apr 1, 2010 at 2:04am
helios:

Thank you. I just ran your code and I think your explanation makes sense and is correct.
Apr 1, 2010 at 3:36am
I should add that my previous statement is true for val being of any basic type, including bool, integers, floating point types, and pointers. It doesn't apply for complex types (classes).
Topic archived. No new replies allowed.