深信服笔试题

来源:互联网 发布:linux管理web日志 编辑:程序博客网 时间:2024/06/03 00:20

#include<iostream>using namespace std;#include<String>  int * fun1(int a){        return &a; //参数 存放在栈里。}  int * fun2(int a){        int b=a; //局部变量存放在栈里        return &b;}  int * fun3(int a){        static int b=a;//静态局部变量 函数结束时值不释放        return &b;}  int *fun4(int a){        int * p=(int *)malloc(sizeof(int)); //局部变量 指向的是堆里面。        *p=a;        return p;}int *fun5(int a){        static int * p=(int*)malloc(sizeof(int)); //静态局部变量  指向的是堆里面。        *p=a;        return p;}  int main(){        cout<<"fun1(2)-fun1(1):"<<*fun1(2)-*fun1(1)<<"  地址:fun1(2)-fun1(1): "<<fun1(2)<<":"<<fun1(1)<<endl;        cout<<"fun2(2)-fun2(1):"<<*fun2(2)-*fun2(1)<<"  地址:fun2(2)-fun2(1): "<<fun2(2)<<":"<<fun2(1)<<endl;        cout<<"fun3(2)-fun3(1):"<<*fun3(2)-*fun3(1)<<"  地址:fun3(2)-fun3(1): "<<fun3(2)<<":"<<fun3(1)<<endl;        cout<<"fun4(2)-fun4(1):"<<*fun4(2)-*fun4(1)<<"  地址:fun4(2)-fun4(1): "<<fun4(2)<<":"<<fun4(1)<<endl;        cout<<"fun5(2)-fun5(1):"<<*fun5(2)-*fun5(1)<<"  地址:fun5(2)-fun5(1): "<<fun5(2)<<":"<<fun5(1)<<endl;        int *p2=fun5(6);        cout<<"p2地址为:"<<p2<<endl;        cout<<*fun5(6)<<"地址fun5(6):     "<<fun5(6)<<"fun5(6) "<<*p2<<endl;        //p2=fun5(7);        cout<<*fun5(7)<<"地址fun5(7):  "<<*p2<<"  "<<fun5(7)<<" fun5(7)"<<*p2<<endl;//前面一个*p2 输出7,后面一个*p2输出6 为什么是因为cout从左到右输出的跟printf。            char * p = "Hello World1"; //数据"Hello World1"为字符串常量,所以存储在静态存储区。通过p[2]可以访问到静态存储区中的第三个数据单元,即字符'l'所在的存储的单元。但是因为数据"Hello World1"为字符串常量,不可以改变,所以在程序运行时,会报告内存错误。    char a[] = "Hello World2"; //因为数据"Hello World2"存在于数组中,所以,此数据存储于栈区.对它修改是没有任何问题的    //p[2] ='A';//    a[2] ='A';    char* p1 = "Hello World1";        cout<<&(*p)<<" "<<&(*p1)<<endl;        //cout<<"地址p-p1: "<<p<<" -"<<p1<<endl;        printf("%p  %p\n",p, p1);//查看 p p1 内存地址是不是一样的。不能用cout<<"地址p-p1:"<<p<<" - "<<p1<<endl;会输出Hello World1来。        cout<<"地址a:"<<&a<<" -"<<a<<endl;        printf("a数组a[1]地址为:%p\n",&a[1]);        return 0;}


输出结果为:

fun1(2)-fun1(1):0  地址:fun1(2)-fun1(1): 0013FF08:0013FF10

fun2(2)-fun2(1):0  地址:fun2(2)-fun2(1): 0013FEFC:0013FF04

fun3(2)-fun3(1):0  地址:fun3(2)-fun3(1): 00478764:00478764

fun4(2)-fun4(1):1  地址:fun4(2)-fun4(1):  00481A80:00481AB0

fun5(2)-fun5(1):0  地址:fun5(2)-fun5(1): 00481870:00481870

p2地址为:00481870

6地址fun5(6):     00481870 fun5(6) 6

7地址fun5(7):  7 00481870 fun5(7) 6

Hello World1 HelloWorld1

0046D064  0046D064

地址a:0013FF68 - HeAloWorld2

a数组a[1]地址为:0013FF69

Press any key tocontinue



分析一下。为什么 fun1,fun2 fun3 都为0 。因为fun1 fun2 返回的是栈地址,在函数调用完就恢复了,所以不是返回2-1=1 而是0。
fun3 是静态局部变量所以fun3(2) fun3(1)都是指向一个地方。所以a-a=0 情况。
fun4 =1是因为 返回的内存为堆里。当程序调用完了。变量值还在。 所以 2-1=1;
fun5=1 是因为 staticint * p=(int *)malloc(sizeof(int));  指针为静态。所以指针都分配同一个堆里空间从打印 fun5(6) 和 fun5(7) 里地址可以看出都在 00481870。
从这里可以看出那些是栈地址 那些变量存放在栈里。 如 0013FE....都是栈里的。
char a[] = "Hello World2"; 也在栈里。 因为 a地址 为0013FF68(就是a[0]地址)  a[1] 0013FF69。
注意:cout<<&p<<endl; 是输出变量本身所在的地址。

在main 函数里加上如下语句:

        int * q1=fun1(8);

        cout<<*q1<<endl; //注意cout<<p<<endl 只对于字符数组类型就输出值,其他类型还是输出指针所指地址。

        cout<<*fun1(2)<<endl; //这里输出2,注意与cout<<q1区别。

        printf("fun7  %d\n",*q1);

        q1=fun1(9);

        printf("fun7%p\n",q1);

        q1=fun7(9);

        printf("fun7%p\n",q1);

生成结果为:

4198615 //那个栈里存放的值
2
fun1  4198615
fun10013FF10
fun70013FF10 //看出调用fun1 fun7返回变量位置是一样的。有时间好好分析一下。
Press any key to continue