zoukankan      html  css  js  c++  java
  • 某公司2014校园招聘笔试题C++

    算是复习一下C++的东西。

    1. What is the output of the program below in a Win32 platform? (10 points)

    void size_func(char a[])
    {
        printf("%d\n", sizeof(a));
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char str[] = "WelcomeToBJ";
        char *p = str;
        void *m = malloc(100);
        int n = 10;
        
        printf("%d\n", sizeof(str));
        printf("%d\n", sizeof(p));
        printf("%d\n", sizeof(n));
        printf("%d\n", sizeof(m));
        size_func(str);
        
        return 0;
    }

    The output is:

    12

    4

    4

    4

    4

    2. What are the outputs of Test() functions? Explain the reason for your answer. (10 points)

    1.

    void GetMemory(char *p)
    {
        p = (char *)malloc(100);
    }
    
    void Test(void)
    {
        char *str = NULL;
        GetMemory(str);
        strcpy(str, "Hello Beijing");
        printf(str);
    }

    2.

    char *GetMemory(void)
    {
        char p[] = "Hello Beijing";
        return p;
    }
    
    void Test(void)
    {
        char *str = NULL;
        str = GetMemory();
        printf(str);
    }

    3.

    void GetMemory3(char **p, int num)
    {
        *p = (char *) malloc(num);
    }
    
    void Test3()
    {
    
        char *str = NULL;
        GetMemory3(&str, 100);
        strcpy(str, "Hello Beijing");
        printf(str);
    }

    4.

    void Test4()
    {
        char *str = (char *)malloc(100);
        strcpy(str, "Hello");
        free(str);
        if(str != NULL)
        {
            strcpy(str, "Beijing");
            printf(str);
        }
    }

    Answer:1.显然不对,GetMemory函数传参的时候只是将str的值(也就是一个地址)赋给了p,在函数里给p赋值不影响外面的str值,所以在strcpy()的时候会出错。

    2. 函数中的p指向的字符串应为local variable,随着函数调用结束而释放。

    (如果不是定义的字符串“char p[] = "Hello Beijing";”, 而是定义的字符指针 char *p,那么可以正常执行。

    因为初始化的字符串数组存储在栈中,可以改变其值。初始化的字符串常量指针存储在静态常量区,不能修改其值。)

    3. 可以正常输出"Hello Beijing"

    4. 输出为:"Beijing",在内存被free之后str并不等于NULL,但是str也是野指针,并不安全,可能会覆盖其他数据,所以一般会将指针free后直接将指针赋为空。

    3. What is the output of the program below? (10 points)

    class IGT
    {
    public:
        void x()    {cout << "class IGT, function x"<< endl;}
        virtual void y()    {cout << "class IGT, function y" << endl;}
    };
    
    class ADV : public IGT
    {
    public:
        virtual void x()    { cout << "class ADV, function x" << endl;}
        void y()    { cout << "class ADV, function y" << endl;}
    };
    
    class CTA : public ADV
    {
    public:
        void x()    {cout << "class CTA, function x" << endl;}
        void y()    {cout << "class CTA, function y" << endl;}
    };
    
    int tmain2()
    {
        IGT* igt1 = new ADV;
        IGT* igt2 = new CTA;
        ADV* adv = new CTA;
    
        igt1->x();
        igt1->y();
        igt2->x();
        igt2->y();
        adv->x();
        adv->y();
        
        delete igt1;
        igt1 = NULL;
    
        delete igt2;
        igt2 = NULL;
    
        delete adv;
        adv = NULL;
    
        return 0;
    }

    The output :

    class IGT, function x
    class ADV, function y
    class IGT, function x
    class CTA, function y
    class CTA, function x
    class CTA, function y

  • 相关阅读:
    Java JDK在Mac下的配置方法
    Kubernetes 在知乎上的应用
    kubernetes 简介:kube-dns 和服务发现
    CNI:容器网络接口
    SkyDNS试用
    docker 跨主机网络:overlay 简介
    为什么Kubernetes不使用libnetwork
    libnetwork 源码浅析
    NVMe到底是什么?用它的SSD有啥优势?
    TUNING FOR ALL FLASH DEPLOYMENTS
  • 原文地址:https://www.cnblogs.com/zijin/p/3376471.html
Copyright © 2011-2022 走看看