我只是用一个很好的答案来阅读这个问题:什么时候在null实例上调用成员函数会导致未定义的行为?
基本上,下面的代码是未定义的行为吗?
struct foo { static void bar() { } };
foo *p = nullptr;
p->bar();
Run Code Online (Sandbox Code Playgroud)
根据链接的帖子,这可以用不同的方式解释,一个是UB,一个不是.
在C++ 0x中,从n3126开始,模糊性仍然存在
这仍然适用于最终的C++ 11吗?
我知道NULL指针是(void*)0.But当我们使用如下语句时会发生什么:
if(ptr==NULL)
Run Code Online (Sandbox Code Playgroud)
其中ptr可以是一个char,float或int指针?是NULL保证被隐式转换为左侧的类型,正如,例如,在C,类型通过返回malloc()是void*但隐式转换为左值的类型?
我现在正在研究唯一的实例(在c ++中),我在.cpp文件中尝试了以下代码.
#include "OnlyInstance.h"
OnlyInstance* OnlyInstance::instance = NULL;
..........
Run Code Online (Sandbox Code Playgroud)
但是编译器告诉我"错误C2065:'NULL':未声明的标识符".这意味着,我应该这样做
#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)
在它面前?但我还在主文件中包含了stdio.h.因此编译时会多次包含stdio.h,对吗?如何正确使用NULL?
另一个问题是,我知道我可以使用0而不是NULL,但在c ++中更推荐哪种方式?
以下哪种方法是更正确的初始化变量的方法?
int x = 0;
int x = NULL;
Run Code Online (Sandbox Code Playgroud)
指针怎么样?我被告知不和谐的事情NULL,比如:" NULL最好初始化指针"或"不要使用NULL但是0来初始化变量"等等......现在,我在互联网上读到的NULL等于0和我测试了我自己.那有什么意义呢?为什么有些人说使用NULL不是一个好选择?有什么东西我不见了吗?
当我启动我的应用程序时它只是崩溃并给我这个错误代码:
尝试在空对象引用上调用虚方法'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String,int)'
这对我来说都很新鲜,请帮帮我.
这是我的主要活动
package com.awplicity.testappshared;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences = getSharedPreferences("", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void onClick(View v) {
switch(v.getId()) {
case R.id.nextAct:
editor.putString("mystring", "Hi");
editor.commit();
startActivity(new Intent("com.awplicity.testappshared.Main2Activity"));
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是第二项活动
package com.awplicity.testappshared;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class …Run Code Online (Sandbox Code Playgroud) 我在 firebase 中遇到空指针异常我知道没有任何像我提供的数据库引用那样的值,但我希望在生成值时它会自动显示......只是想获得那个单一的值每次在 firebase 上创建它时,它都会在 String val =... 行上给我空指针异常
请帮我 ...
final DatabaseReference dtt = database.getReference("/trial/trials/");
dtt.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String val= dataSnapshot.child("myvalue").getValue().toString();
if(val!=null)
{
Log.e("not null------","---------------");
}else
{
Log.e("null------","---------------");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Run Code Online (Sandbox Code Playgroud)
请帮帮我.....请...
我目前正在设计一个看起来大致如下的类层次结构:
struct Protocol
{
// Pass lower-layer protocol as a reference.
Protocol(Protocol & inLLProtocol) :
mLLProtocol(inLLProtocol)
{
}
// A protocol "always" has a LLProtocol.
Protocol & mLLProtocol;
};
struct Layer1Protocol : Protocol
{
// This is the "bottom" protocol, so I pass a fake reference.
Layer1Protocol() : Protocol(*static_cast<Protocol*>(nullptr)) {}
};
Run Code Online (Sandbox Code Playgroud)
*nullptr只要永远不会访问引用,IIRC绑定引用就是安全的.所以我现在有责任以这种方式设计我的Layer1Protocol类来防止这种情况发生.
我喜欢这种方法,因为我确保所有用户协议实例都会引用它们各自的低层协议(Layer1Protocol是例外,但它是核心库的一部分).我认为这比使用指针更可取,因为一旦引入指针,就可以传递空指针,然后可能需要在运行时检查,结果是很多指针检查代码和偶尔的错误.
你认为我的基于参考的方法是可以辩护的吗?或者使用空引用总是一种不好的做法?
我还在学习指针.我知道如何切换if语句等.在我正在使用的书中,我得到了这个例子:
FILE* from = fopen("in.txt", "r");
FILE* to = fopen("out.txt", "w");
if (from != NULL && to != NULL)
{
...
}
else
{
printf("failed to open files!\n");
}
} /* end of function */
Run Code Online (Sandbox Code Playgroud)
我知道可以改成这个:
FILE* from = fopen("in.txt", "r");
FILE* to = fopen("out.txt", "w");
if (from == NULL || to == NULL)
{
printf("failed to open files!\n");
return;
}
...
} /* end of function */
Run Code Online (Sandbox Code Playgroud)
现在,我的问题(如果它将编译)是,如果这是安全的或实现定义为NULL几乎每个编译器都不同.
FILE* from = fopen("in.txt", "r");
FILE* to …Run Code Online (Sandbox Code Playgroud) 空指针可以重复使用吗?如果是,在什么条件下?
更具体地说,谁能告诉这段代码会发生什么?
int *p = 0;
p = (int*)malloc(sizeof(int));
Run Code Online (Sandbox Code Playgroud)
如果有问题——为什么?正确的做法是什么?
编辑:我知道没有必要这样做。我问的是为了理解指针在 C 中的工作方式会发生什么。
type_t* x = NULL; type_t* y = NULL;鉴于我们总是x > y评估错误,C 标准是否能保证这一点?我问因为会话在初始或终端malloc缓冲区中可能吗?由此看来,人们应该小心指针比较,并且只有在处理指向同一数组元素的指针时才进行比较,松散地说。但是,保证评估为 false 很方便x > y,因为我想要一个结构,它是一个堆栈数组,具有第一个和最后一个元素的字段,如果该数组仍然有0元素,则设置这些字段很方便是NULL,并且仍然允许for循环遍历数组的元素是很方便的,而不需要显式检查数组是否有0元素或更多,所以这样的比较很方便......