/*
#include<iostream>
using namespace std;
static int a=1;
void fun1(){a=2;}
void fun2(){int a=3;}
void fun3(){
static int a=4;
}
int main(){
cout<<a;
fun1();
cout<<a;
fun2();
cout<<a;
fun3();
cout<<a;
}
//1 2 2 2
*/
/*
#include<iostream>
using namespace std;
int main(){
int i;
for(i=0;i<3;i++){
switch(i){
case 0:cout<<i;
case 2:cout<<i;
default:cout<<i;
}
}
}*/
//0 0 0 1 2 2
/*
#include<iostream>
using namespace std;
int function(const int src[16]){
int val1=0;
int dst[16]={0};
int val2=0;
const int* psrc=src;
fprintf(stdout,"size of src=%lu\n",sizeof(src));
for(int i=0;i<=16;i++){
dst[i]=*psrc+i;
psrc++;
}
cout<<val1<<" "<<val2;
return 0;
}
int main(){
int s[16]={0};
function(s);
}
//4
//0 随机一个数
*/
/*
#include<iostream>
using namespace std;
struct A{
unsigned int a;
char b[2];
double c;
short d;
};
int main(){
cout<<sizeof(A);
}
//24
*/
/*
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char a[]="ABCDEFG";
char b[]={'A','B','C','D','E','F','G'};
cout<<sizeof(a)<<" "<<sizeof(b)<<" "<<strlen(a)<<" "<<strlen(b);
}
//8 7 7 7
*/
/*
#include<iostream>
#include<cstring>
using namespace std;
struct A{
uint16_t id;
uint32_t value;
uint64_t timestamp;
};
void fool(){
A A1={};
uint64_t a=0x00010001;
memcpy(&A1,&a,sizeof(uint64_t));
cout<<A1.id<<" "<<A1.value<<" "<<A1.timestamp;
}
int main(){
fool();
}
//1 0 0
*/
/*
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char dog[]={'\0'};
char dog2[]={"wang\0wang"};
cout<<sizeof(dog)<<" "<<strlen(dog)<<endl;
cout<<sizeof(dog2)<<" "<<strlen(dog2)<<endl;
}
//1 0
//10 4
*/
/*
#include<stdio.h>
class A
{
public:
void foo()
{
printf("1");
}
virtual void fun()
{
printf("2");
}
};
class B: public A
{
public:
void foo()
{
printf("3");
}
void fun()
{
printf("4");
}
};
int main(void)
{
A a;
B b;
A *p = &a;
p->foo();
p->fun();
p = &b;
p->foo();
p->fun();
A *ptr = (A *)&b;
ptr->foo();
ptr->fun();
return 0;
}
//121414
*/
/*
#include<iostream>
using namespace std;
class A
{
int i; //4
union U //16
{
char buff[13];
int i;
}u;
void foo() { }
typedef char* (*f)(void*);
enum {red, green, blue} color ;//4
}a;
int main(){
cout<<sizeof(a);//24
}
*/
/*
#include<iostream>
using namespace std;
class A
{
public:
A() { }
~A() { cout<<"~A"<<endl; }
};
class B:public A
{
public:
B(A &a):_a(a)
{
}
~B()
{
cout<<"~B"<<endl;
}
private:
A _a;
};
int main(void)
{
A a; //很简单,定义a的时候调用了一次构造函数
B b(a);
}
// 析构B A A A
*/
/*
#include<stdio.h>
int main(){
int i=-2147483648;
return printf("%d,%d,%d,%d",~i,-i,1-i,-1-i);
}
//2147483647
*/
/*
#include<iostream>
using namespace std;
class foo {
public:
foo(){cout<<"foo()"<<endl;};
};
class boo : public foo {
public:
boo() : foo() {cout<<"boo()"<<endl;};
};
int main(){
foo f;
boo b;
}*/
/*
#include<iostream>
using namespace std;
class cls{
public:
cls(int &n,int *m):n(n),m(m){}
cls():cls(a,&a){}
void f(){cout<<a<<endl<<*b<<endl<<n<<endl<<*m<<endl;}
private:
int a=3;
int *b=&a;
int n;
int *m;
};
int main()
{
cls m;
m.f();
return 0;
}*/
/*
#include<iostream>
using namespace std;
struct sampleStruct
{
char flag[3];
short value;
};
union sampleUnion
{
char flag[3];
short value;
};
int main(){
cout<<sizeof(sampleStruct)<<" "<<sizeof(sampleUnion);
}
//输出结果是:6,4
//字符类型占1字节, 可以从任何地址开始
//short类型占2字节, 必须从2字节倍数地址开始
//int类型占4字节,必须从4字节倍数地址开始
//0000 a[0]
//0001 a[1]
//0002 a[2]
//0003 空一个字节
//0004 b 这个是2倍数的地址
//0005
//结构体在内存组织上是顺序式的,联合体则是重叠式,各成员共享一段内存,所以整个联合体的sizeof也就是每个成员sizeof的最大值。
*/
C++小题(一)
最新推荐文章于 2022-02-24 00:44:03 发布