- 基本变量类型
注意,这里的大小表示的最小尺寸,不是常见的大小。现在int一般是4个字节,32位,short int 是16位, long double 是16字节 128位。 long 和 long long都是8个字节
float的精确到小数点后6-7位,double是到后15-16位
对于空指针是8
cout << sizeof(void *) << endl;
8
2.指针和引用的区别
添加链接描述
#include<iostream>
#include<mutex>
using namespace std;
class A{
int a, b,c;
}c;
int main()
{
A* a = new A();
auto &b = c;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
system("pause");
return 0;
}
/* 输出结果
8
12
*/
3.当需要传入指针的函数的参数,传入参数是引用和指针的时候分别有什么区别?
下面看一个例子:
#include<iostream>
#include<stdlib.h>
using namespace std;
void swap_int(int *a,int *b)
{
int *temp=a;
a=b;
b=temp;
}
void test(int *p)
{
int a=1;
p=&a;
cout<<p<<" "<<*p<<endl<<endl;;
}
int main(void)
{
int a=1,b=2;
int *p=NULL;
swap_int(&a,&b);
cout<<a<<" "<<b<<endl<<endl;
test(p);
if(p==NULL)
cout<<"指针p为NULL"<<endl<<endl;
system("pause");
}
/*
可以看到当传入的是引用的时候,可以直接改变对应内存中的值;但是传入指针的时候
*/