null-pointer

Which of these will create a null pointer?

冷暖自知 提交于 2019-12-17 19:15:59
问题 The standard says that dereferencing the null pointer leads to undefined behaviour. But what is "the null pointer"? In the following code, what we call "the null pointer": struct X { static X* get() { return reinterpret_cast<X*>(1); } void f() { } }; int main() { X* x = 0; (*x).f(); // the null pointer? (1) x = X::get(); (*x).f(); // the null pointer? (2) x = reinterpret_cast<X*>( X::get() - X::get() ); (*x).f(); // the null pointer? (3) (*(X*)0).f(); // I think that this the only null

Assigning a reference by dereferencing a NULL pointer

坚强是说给别人听的谎言 提交于 2019-12-17 18:54:48
问题 int& fun() { int * temp = NULL; return *temp; } In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give exception if it is by value then it does. Even when dereferencing of NULL pointer is assinged to reference (like the below line) then also it does not give. int* temp = NULL: int& temp1 = *temp; Here my question is that does not compiler do the dereferencing in

Uninitialized pointers in code

三世轮回 提交于 2019-12-17 09:23:19
问题 I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some other program. Now if that is the case we should never have this line in any part of our code: int* ptr; Instead we should have something like int* ptr = NULL; //Is this going to avoid the problem Please suggest because I have seen the first line( int* ptr; ) in many books so I am getting this doubt. If possible give some

Uninitialized pointers in code

百般思念 提交于 2019-12-17 09:23:09
问题 I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some other program. Now if that is the case we should never have this line in any part of our code: int* ptr; Instead we should have something like int* ptr = NULL; //Is this going to avoid the problem Please suggest because I have seen the first line( int* ptr; ) in many books so I am getting this doubt. If possible give some

Calling a method on an uninitialized object (null pointer)

 ̄綄美尐妖づ 提交于 2019-12-17 06:30:12
问题 What is the normal behavior in Objective-C if you call a method on an object (pointer) that is nil (maybe because someone forgot to initialize it)? Shouldn't it generate some kind of an error (segmentation fault, null pointer exception...)? If this is normal behavior, is there a way of changing this behavior (by configuring the compiler) so that the program raises some kind of error / exception at runtime? To make it more clear what I am talking about, here's an example. Having this class:

Calling a method on an uninitialized object (null pointer)

六眼飞鱼酱① 提交于 2019-12-17 06:30:00
问题 What is the normal behavior in Objective-C if you call a method on an object (pointer) that is nil (maybe because someone forgot to initialize it)? Shouldn't it generate some kind of an error (segmentation fault, null pointer exception...)? If this is normal behavior, is there a way of changing this behavior (by configuring the compiler) so that the program raises some kind of error / exception at runtime? To make it more clear what I am talking about, here's an example. Having this class:

What is a void pointer and what is a null pointer?

懵懂的女人 提交于 2019-12-17 06:29:58
问题 So I was going through some interview questions and I came across one about void and null pointers, which claims: a pointer with no return type is called a null pointer. It may be any kind of datatype. This confused me thoroughly! It seems void and null could be used interchangeably according to this question, and I don't believe that to be correct. I assumed void to be a return type and null to be a value. But I am just a code-rookie and am not sure I am right. Please express your views as

Can I use if (pointer) instead of if (pointer != NULL)?

谁说我不能喝 提交于 2019-12-17 04:14:57
问题 Is it safe to check a pointer to not being NULL by writing simply if(pointer) or do I have to use if(pointer != NULL) ? 回答1: You can; the null pointer is implicitly converted into boolean false while non-null pointers are converted into true. From the C++11 standard, section on Boolean Conversions: A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool . A zero value, null pointer value, or null member pointer value is

Nullpointer exception on Inflater in Custom Drawer Adapter

一曲冷凌霜 提交于 2019-12-12 02:14:35
问题 I'm trying to make a custom adapter for my navigation drawer, and keep getting a nullpointer exception on the layout inflater. I've tried declaring the LayoutInflater variable inside and outside the getView method as suggested in other posts, but to no avail. Below is my Custom Adapter Class Context mContext; private ArrayList<String> mValues = new ArrayList<String>(); LayoutInflater inflater; public DrawerListAdapter(Context context, ArrayList<String> mValues) { super(context, R.layout

When does invoking a member function on a null instance result in undefined behavior?

和自甴很熟 提交于 2019-12-11 14:34:08
问题 Consider the following code: #include <iostream> struct foo { // (a): void bar() { std::cout << "gman was here" << std::endl; } // (b): void baz() { x = 5; } int x; }; int main() { foo* f = 0; f->bar(); // (a) f->baz(); // (b) } We expect (b) to crash, because there is no corresponding member x for the null pointer. In practice, (a) doesn't crash because the this pointer is never used. Because (b) dereferences the this pointer ( (*this).x = 5; ), and this is null, the program enters undefined