我正在移植现有代码以在gcc 4.7.2下编译并且遇到了与nullptr的奇怪问题.我已经设法将其归结为一个简单的测试用例:
#include <stdio.h>
const char* g_marker = "Original value";
void SetMarker( const char* s )
{
g_marker = s;
}
char* Test1()
{
return SetMarker( "I was here 1" ), nullptr;
}
char* Test2()
{
SetMarker( "I was here 2" );
return nullptr;
}
char* Test3()
{
return SetMarker( "I was here 3"), (char*)NULL;
}
int main()
{
char* returnValue = Test1();
printf( "%s\n", g_marker );
}
Run Code Online (Sandbox Code Playgroud)
用g ++ test.cpp -o test -std = c ++ 0x编译它.
我期望的输出是"我在这里1",但我得到"原始值",表明从未调用过SetMarker.
调用Test2或Test3会得到预期的输出. …
我正在编写一个小型 C++11 库,我相信std::optional它会是一些可以返回nullptr. 然而,std::optional这是 C++17 的功能。由于 C++11 是一项要求,因此我正在寻找在std::optional保持兼容性的同时使用的方法。
我发现功能宏可以测试。我想我可以用它来检测是否std::optional可用......但是当它不可用时最好的方法是什么?
我应该提供自己的std::optional实现吗?
不可用nullptr时返回?std::optional(可能会弄乱我的代码。)
还是放弃这个想法,nullptr只继续返回?
你好?我正在研究双向链表
无论如何,当我过去将数据放在节点上时,出现了 C6011 错误 [:Dereferencing Null Pointer 'NewNode']
所以我参考了https://learn.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019和
尝试检查空值。但它没有用..
我检查了错误窗口的一些详细信息,例如我添加的图片
他们说“NewNode 将为 NULL”,“即使取消引用,NewNode 仍将为 NULL”
代码如下
1.头文件(DoubleLinkedList.h)
#pragma once
#ifndef DOUBLY_LINKEDLIST_H
#define DOUBLY_LINKEDLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElementType;
typedef struct MyNode
{
ElementType Data;
struct MyNode* Prev;
struct MyNode* Next;
}Node;
Node* DLL_CreateNode(ElementType NewData); //1.create node
void DLL_DestroyNode(Node* Node);//2.destroy node
void DLL_AppendNode(Node** Head, Node* NewNode);//3.append node
/*
4.insert node
connect
prev-insert-next
*/
void DLL_InsertAfter(Node* Current, Node* NewNode);
void DLL_RemoveNode(Node** Head, Node* Remove);//5.remove node …Run Code Online (Sandbox Code Playgroud) 在 ISO/IEC 14882:2017 (C++17) 中,第 5.13.7 节“指针文字”规定:
5.13.7 指针文字 [lex.nullptr]
指针字面量:nullptr
1 指针字面量是关键字 nullptr。它是 std::nullptr_t 类型的纯右值。[注意:std::nullptr_t 是一个独特的类型,它既不是指针类型也不是指向成员类型的指针;相反,这种类型的纯右值是空指针常量,可以转换为空指针值或空成员指针值。见 7.11 和 7.12。——尾注]
下面nullptr是一个类型的纯右值std::nullptr_t。类型的纯右值std::nullptr_t是空指针常量;因此nullptr是一个空指针常量。空指针常量(so is nullptr)可以转换为空指针值或空成员指针值。
现在我在 ISO/IEC 14882:2017 的第 7.11 节“指针转换”中提到:
空指针常量是具有零值的整数文字 (5.13.2) 或类型为 std::nullptr_t 的纯右值。空指针常量可以转换为指针类型;结果是该类型的空指针值,并且与对象指针或函数指针类型的所有其他值区分开来。
我知道空指针常量是一个值为 0 或类型为纯右值的整数文字std::nullptr_t,但我不明白与空指针值和空成员指针值的区别。我不明白从空指针常量到指针类型的转换结果如何是“该类型的空指针值”以及该上下文中的空指针值是什么。
我的问题是:
nullptr)、空指针值和空成员指针值之间有什么区别?就目前而言,我的项目正确使用 libavcodec 解码视频,其中每一帧都被操纵(无论如何)并输出到新视频。我从网上找到的例子拼凑起来,它的工作原理。结果是处理过的帧的完美 .mp4,减去音频。
我的问题是,当我尝试将音频流添加到输出容器时,我在 mux.c 中遇到了无法解释的崩溃。它在static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt). 当st->internal->priv_pts->val = pkt->dts;尝试,priv_pts是nullptr。
我不记得版本号,但这是 2020 年 11 月 4 日 ffmpeg 从 git 构建的。
我的MediaContentMgr比我在这里的要大得多。我正在剥离与帧操作有关的所有内容,因此如果我遗漏了任何内容,请告诉我,我会进行编辑。
添加时触发 nullptr 异常的代码被内联调用
.h:
#ifndef _API_EXAMPLE_H
#define _API_EXAMPLE_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "glm/glm.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
}
#include "shader_s.h"
class MediaContainerMgr {
public:
MediaContainerMgr(const std::string& infile, const std::string& vert, const std::string& frag, …Run Code Online (Sandbox Code Playgroud) 更新到 Ventura 13.3,安装最新的 Xcode 和命令行工具后,我在编译任何 cpp 文件时收到此错误
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cstddef:50:9: error: no member named 'nullptr_t' in the global namespace
Run Code Online (Sandbox Code Playgroud)
它发生在#include <iostream>
例如:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
In file included from Untitled.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:41:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:221:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:18:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:841:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/copy.h:12:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/unwrap_iter.h:13:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__iterator/iterator_traits.h:14:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__iterator/incrementable_traits.h:15:
In …Run Code Online (Sandbox Code Playgroud) 我厌倦了等待编译器的支持nullptr(gcc 4.6确实如此,但它是如此新的少数发行版支持它).
因此,作为一个止损间隙直到nullptr完全支持我决定模仿它.有两个仿真示例:一个来自这里,一个来自wikibooks.
值得注意的是,两个实现都没有提及operator ==.但是,没有一个,以下代码将无法编译.
int* ptr = nullptr;
assert( ptr == nullptr ); // error here: missing operator ==
Run Code Online (Sandbox Code Playgroud)
这个operator ==错误是编译器错误吗?
是operator ==(和!=,<,<=,等)需要更完美的模拟nullptr?
模拟nullptr和真实交易还有什么不同?
我正在开发一个有两个不同最终用户的库,其中一个使用gcc 4.5.3而另一个只是移动到gcc 4.6.3.该库使用新的C++ 11智能指针(特别是unique_ptr)并在gcc 4.5.3上编译良好.但是,在这两个版本之间,gcc开始支持nullptr,因此unique_ptr的API更改为更接近标准.现在这样做,以下代码从精细到模糊
unique_ptr up( new int( 30 ) );
...
if( up == 0 ) // ambiguous call now to unique_ptr(int) for 0
Run Code Online (Sandbox Code Playgroud)
是否有一个干净的(即,下一句)方式来改变上面的if语句,以便它可以使用和不使用nullptr?如果可能的话,我想避免配置检查,然后像下面的宏(我认为可行)
#if defined NULLPOINTER_AVAILABLE
#define NULLPTR (nullptr)
#else
#define NULLPTR (0)
#endif
Run Code Online (Sandbox Code Playgroud)
或者这是获得我正在寻找的行为的唯一方法吗?
我有一个模板,如:
template <class A, class B>
void func(A* a, B* b){
...
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,B* b不需要参数,因此,我尝试使用nullptr:
MyA a;
func(&a, nullptr);
Run Code Online (Sandbox Code Playgroud)
编译器不喜欢它,因为nullptr某种方式不是一种类型.
我该如何处理这种情况?唯一的想法是在这种情况下使用虚拟类型.
我正在尝试在Ubuntu 16.04上安装OpenDetection.我已经根据安装了所有的依赖关系在这里,除了OpenCV的.这是我用于OpenCV的camke命令:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_VTK=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -DOPENCV_EXTRA_MODULES_PATH=/home/tiestu/Documents/GitRepository/OpenCV3/opencv_contrib/modules /home/tiestu/Documents/GitRepository/OpenCV3/opencv
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用makeOpenCV时,我收到了这个错误,我认为这与VTK有关!
根据安装说明,我使用默认设置使用最新的预制二进制版本的cmake配置了VTK.但是这个错误说:
vtkGenericDataArrayLookupHelper.h:72:23: error: ‘nullptr’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
我使用的是什么版本的编译器(11或0x)是否重要?如果是这样,我应该使用什么版本?