我的一个朋友从“理解和使用 C 指针 - Richard Reese,O'Reilly 出版物”中指出了第二个要点,我无法解释其中的第一句话。我错过了什么?
空指针
指向 void 的指针是一个通用指针,用于保存对任何数据类型的引用。指向 void 的指针的示例如下所示:
Run Code Online (Sandbox Code Playgroud)void *pv;它有两个有趣的特性:
- 指向 void 的指针将具有与指向 的指针相同的表示形式和内存对齐方式
char。- 指向 void 的指针永远不会等于另一个指针。但是,分配了一个
NULL值的两个空指针将是相等的。
这是我的代码,不是书中的代码,所有指针都具有相同的值并且相等。
#include <stdio.h>
int main()
{
int a = 10;
int *p = &a;
void *p1 = (void*)&a;
void *p2 = (void*)&a;
printf("%p %p\n",p1,p2);
printf("%p\n",p);
if(p == p1)
printf("Equal\n");
if(p1 == p2)
printf("Equal\n");
}
Run Code Online (Sandbox Code Playgroud)
输出:
0x7ffe1fbecfec 0x7ffe1fbecfec
0x7ffe1fbecfec
Equal
Equal
Run Code Online (Sandbox Code Playgroud) 在C++模板中完整指南的 第5.3节"成员模板":
请注意,模板赋值运算符不会替换默认赋值运算符.对于相同类型堆栈的分配,仍会调用默认赋值运算符.
这是正确的,因为当我在代码下面运行时:
#include<iostream>
using namespace std;
template<typename T>
class Pair
{
public:
T pair1,pair2;
Pair(T i,T j):pair1(i),pair2(j){}
template<typename T1>Pair<T>& operator=(Pair<T1>&);
};
template<typename T>
template<typename T1>
Pair<T>& Pair<T>::operator=(Pair<T1>& temp)
{
this->pair1 =temp.pair1*10;//At this point
this->pair2=temp.pair2;
return *this;
}
int main()
{
Pair<int>P1(10,20);
Pair<int>P2(1,2);
P2=P1;
cout<<P2.pair1<<' '<<P2.pair2<<endl;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我得到了答案100 20.
它没有给出默认的分配答案.
这是C++模板完整指南中的输入错误吗?
C++模板:完整指南作者:David Vandevoorde,Nicolai M. Josuttis
出版商:Addison Wesley
发布日期:2002年11月12日ISBN:0-201-73484-2页数:552
我有这样的代码:
int quotient = 100/*ptr;
Run Code Online (Sandbox Code Playgroud)
其中ptr是指向整数的指针.
但它正在/*作为评论.
如何通过指针取消引用值来划分除法的含义?我还有什么额外的特殊性才能具备这个含义?
#include <cinttypes>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
uint64_t descendingOrder(uint64_t a)
{
string str = to_string(a);
sort(str.begin(),str.end(),[](unsigned char a , unsigned char b) { return a>b;});
cout<<"sorted string:" <<str<<endl;
cout<<"value :"<<strtol(str.c_str(),nullptr,10)<<endl;
return strtol(str.c_str(),nullptr,10);
}
int main()
{
descendingOrder(9223372036854775807L);
}
Run Code Online (Sandbox Code Playgroud)
sorted string:9887777655433322200
value :9223372036854775807
Run Code Online (Sandbox Code Playgroud)
为什么sorted string:和value:不同?这好像是value:即使在排序之后也以某种方式获取了原始字符串。错误在哪里?是UB吗?
代码:在线代码
import sys
a = 10
b = a
print sys.getrefcount(a)
b=1
print sys.getrefcount(b)
Run Code Online (Sandbox Code Playgroud)
输出:
22
614
我的python解释器有问题吗?为什么这会给出像 614 这样巨大的值?
Python版本
/usr/lib/python2.7/
什么是使用value_type在STL容器?
来自MSDN:
// vector_value_type.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector<int>::value_type AnInt;
AnInt = 44;
cout << AnInt << endl;
}
Run Code Online (Sandbox Code Playgroud)
我不明白value_type这里具体实现了什么?变量也可以是一个int?是否使用是因为编码人员懒得检查向量中存在的对象类型是什么?
一旦我清楚这一点,我想我能理解allocator_type,size_type,difference_type,reference,key_type等.
std::array<int,0>大小为零的数组是什么意思?
在发布之前我已经在SO中经历了类似的问题,所有这些问题都是关于简单的数组类型和C语言,并且大多数人都认为这是非法的.但是在C++中array<int,0>是允许的.
根据cppreference.com
零长度数组(N == 0)有一种特殊情况.在那种情况下,
array.begin() == array.end()这是一些独特的价值.调用front()或back()对零大小的数组的影响是未定义的.
为什么不将它定义为非法?
我正在经历C++ FAQ 2nd Edition, FAQ 9.04- What is an exception specification?.
在那里,提到如果我们从其签名指定一组预定义异常类型的函数抛出一个意外异常,它应该调用unexpected()->terminate()->abort().但是我的程序捕获了意外的异常而不是abort()它,为什么?
#include<iostream>
using namespace std;
class Type1{};
class Type2{};
class Type3{};
void func() throw(Type1, Type2)
{
throw Type3();
}
int main()
{
try{
func();
}
catch (Type1 &obj1)
{
cout << "Type1 is caught" << endl;
}
catch (Type2 &obj2)
{
cout << "Type2 is caught" << endl;
}
catch (Type3 &obj3)
{
cout << "Type3 is caught" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我得到的输出 …
#include<iostream>
using namespace std;
class Test
{
public:
Test(){}
Test(int param):i(param){}
int i;
};
int main()
{
Test obj1(100);
//Test obj2[100](obj1) ; - This doesn't work I know
Test obj3[10] = obj1; //This works
cout<<obj3[9].i<<endl;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码Test obj2[100](obj1);不起作用Test obj3[10] = obj1;
为什么前者支持但后者支持.(两者都将调用复制构造函数.)
是不是因为编译器中的实现限制而不支持前者?
编辑:我没有使用c ++ 11.gcc版本4.8.2(i686-posix-dwarf-rev3,由MinGW-W64项目构建)Qt 5.3.1
有什么结论?
我有一个程序:
#include<iostream>
using namespace std;
class Test
{
public:
void func()
{
cout << "Inside func" << endl;
throw;
}
};
int myfunc()
{
Test T;
T.func();
return 1;
}
int main()
{
myfunc();
cout << "Main func" << endl;//should not print
getchar();
}
Run Code Online (Sandbox Code Playgroud)
我的期望是这个程序将终止main,但在VC++ 2015上,main cout它正在打印.这违背了我的理解,所以我编译了它gcc它在那里工作正常.
这是VC++ 2015中的错误还是程序终止的行为,这是未指定的/ UB行为?它应该执行cout << "Main func" << endl;吗?
IDE:VS2015 CTP Ultimate预览版(30天)
标志: /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc140.pdb" /fp:precise /D "_MBCS" /errorReport:prompt …