zoukankan      html  css  js  c++  java
  • 嵌入式相关2

     

    两道嵌入式软件工程师面试题,答对者甚少啊!

    1、LINUX下的Socket套接字和Windows下的WinSock有什么共同点?请从C/C++语言开发的角度描述,至少说出两点共同点。(10分,说得好的每点加5分,没有上限。精通SOCK编程者破格录用。)

    本题的目的就是考考应试者的SOCK功底,以及应用能力。答案可谓是五花八门,但是答对的少得可怜。其实答案很多,也很好回答。可见面试者功底确实一般,对SOCK编程只是一知半解。更有甚者,把TCP/IP的原理搬出来讲了一大堆,却无一条符合要求。

    参考答案:

    第1题,答中一个得5分,答出其它正确答案的,也得5分。

    a)都基于TCP/IP协议,都提供了面向连接的TCP SOCK和无连接的UDP SOCK。

    b)都是一个sock结构体。

    c)都是使用sock文件句柄进行访问。

    d)都具有缓冲机制。

     

     

    2、请编写一个标准Shell脚本testd,实现如下功能:

    A、在Linux操作系统启动的时候,自动加载/mnt/test/test程序。

    B、当test异常退出之后,自动重新启动。

    C、当test程序重启次数超过100次,自动复位操作系统。

    假设你所拥有的资源:

    A、目标机器是一台具有标准shell的嵌入式计算机,CPU为ARM7 56MB,内存16MB,软件环境基于Linux2.6.11和BusyBox1.2构建。

    B、当前已有11个用户进程在运行,占用了大部分的CPU时间和内存,你可使用的内存只有2MB左右,CPU时间由系统分派。

    (本题满分20分,程序15分,注释5分。程序不能运行的0分,能够运行但有BUG的10分,能够正确无误的运行的15分。清楚编写注释的5分。)

    本题是考查LINUX和嵌入式编程功底的,写出程序来的不少,但是95%以上的人竟无视我假设的资源,不知道在重启test程序的时候需要加上一个适当的掩饰时间,以便资源紧张的操作系统有时间回收资源。85%的人不知道写完testd之后,要在init里边加载这个脚本,才能实现启动时自动加载的功能。有人甚至在脚本开头使用bash作为脚本解析器,我已经清清楚楚说明了用"标准shell"!用sh不就完了吗?是习惯作祟吗?

    参考答案:

    ########################################

    #testd is a daemon script to start and watch the program test

    ########################################

    #!/bin/sh

    #load *.so that may need

    if [ -r /sbin/ldconfig ]; then

    ldconfig

    fi

    #add the libs PATH that may need

    export LD_LIBRARY_PATH="/lib"

    #count is the counter of test started times

    count=0

    #main loop

    while [ 1 ] ;do

    #add execute attribute for /mnt/test/test

    chmod +x /mnt/test/test

    #run test

    /mnt/test/test

    #a running times counter

    let count=count+1

    echo "test running times is $count"

    #Is test runs too many times?

    if [ "$count" -gt 100 ]; then

    echo "Will reboot when test runs too many times"

    reboot

    fi

    #waiting for test stop...

    sleep 3

    done

    #########################################

     

     

     

     

     

     

     

    c语言如何对寄存器进行位操作

    1 寄存器地址的定义:

      #define UART_BASE_ADRS (0x10000000) /* 串口的基地址 */

      #define UART_RHR *(volatile unsigned char *)(UART_BASE_ADRS + 0) /* 数据接受寄存器 */

      #define UART_THR *(volatile unsigned char *)(UART_BASE_ADRS + 0) /* 数据发送寄存器 */

      2 寄存器读写操作:

    UART_THR = ch; /* 发送数据 */

    ch = UART_RHR; /* 接收数据 */

    也可采用定义带参数宏实现

    #define WRITE_REG(addr, ch) *(volatile unsigned char *)(addr) = ch

    #define READ_REG(addr, ch) ch = *(volatile unsigned char *)(addr)

      3 对寄存器相应位的操作方法:

      定义寄存器

    #define UART_LCR *(volatile unsigned char *)(UART_BASE_ADRS + 3) /* 线控制寄存器 */

    定义寄存器相应位的值

    #define CHAR_LEN_5 0x00

    #define CHAR_LEN_6 0x01

    #define CHAR_LEN_7 0x02

    #define CHAR_LEN_8 0x03 /* 8 data bit */

    #define LCR_STB 0x04 /* Stop bit control */

    #define ONE_STOP 0x00 /* One stop bit! */

    #define LCR_PEN 0x08 /* Parity Enable */

    #define PARITY_NONE 0x00

    #define LCR_EPS 0x10 /* Even Parity Select */

    #define LCR_SP 0x20 /* Force Parity */

    #define LCR_SBRK 0x40 /* Start Break */

    #define LCR_DLAB 0x80 /* Divisor Latch Access Bit */

     

    定义寄存器相应位的值另一种方法

    #define CHAR_LEN_5 0<<0

    #define CHAR_LEN_6 1<<0

    #define CHAR_LEN_7 1<<1

    #define CHAR_LEN_8 (1<<0)|(1<<1) /* 8 data bit */

    #define LCR_STB 1<<2 /* Stop bit control */

    #define ONE_STOP 0<<2 /* One stop bit! */

    #define LCR_PEN 1<<3 /* Parity Enable */

    #define PARITY_NONE 0<<3

    #define LCR_EPS 1<<4 /* Even Parity Select */

    #define LCR_SP 1<<5 /* Force Parity */

    #define LCR_SBRK 1<<6 /* Start Break */

    #define LCR_DLAB 1<<7 /* Divisor Latch Access Bit */

     

    对寄存器操作只需对相应位或赋值

    UART_LCR = CHAR_LEN_8 | ONE_STOP | PARITY_NONE; /* 设置 8位数据位,1位停止位,无校验位 */

      4 对寄存器某一位置位与清零

       对某一寄存器第7位置位

    XX_CRTL |= 1<<7;

    XX_CRTL &= ~(1<<7);

     

    UART_LCR |= LCR_DLAB; /* 时钟分频器锁存使能 */

    UART_LCR &= ~(LCR_DLAB); /* 禁止时钟分频器锁存 */

     

      5 判断寄存器某一位是否置位或为0的方法

    #define UART_LSR *(volatile unsigned char *)(UART_BASE_ADRS + 5) /* 线状态寄存器 */

    #define LSR_DR 1<<0 /* Data Ready */

      当UART_LSR的第0位为1时结束循环

     while (!(UART_LSR & LSR_DR)) /* 等待数据接收完 */

     

    补充:我自己做的试验

    rNFCONF = 0;

    rNFCONF |= (1 << 1)|(1 << 0); //rNFCONF = 0x3

     

    rNFCONF |= (1 << 2); //rNFCONF = 0x7

     

    rNFCONF |= ~(1 << 3); //rNFCONF = 0xFFFFFFF7

    rNFCONF =0xFFFFFFFF; //rNFCONF = 0xFFFFFFFF

    rNFCONF &= ~(1<<0); //rNFCONF = 0xFFFFFFFE

     

    rNFCONF = 0xFFFFFFFF; //rNFCONF = 0xFFFFFFFF

    rNFCONF &= ~(0<<1); //rNFCONF = 0xFFFFFFFF

     

    我这个天生愚钝的人,一直对这个搞的不是太明白,今天狠心把它搞个透彻。共有两种操作符,"&" 、"|"和"<<"。在程序中的操作形式有两种形式,一种是赋值、另一种就是对寄存器的位操作了。

    赋值就是使用的"=",而对寄存器的单独位操作就是使用"&="和"|="两种。

    unsigned int i;

    首先说赋值操作,i = ( 1 << 0) | ( 1 << 1); 执行完成后就是 i = 3;和i以前的值没有任何的关系。但是在表达式中 使用|(0 << 2),对结果是没有任何影响的。查看汇编程序前,一直以为是使用的另一个寄存器和当前的寄存器进行的与或操作,实际上编译器已经将处理成了 movl 0x03,exb 大概这样的形式。编译器已经将右值提前计算出来了。

    另一种是i = (~(1 << 1))&(~(1 << 0)); i=0xfffffffc; 可以看出这两种操作的区别是一个是置位,另一个是清位,而两者的操作数是有区别的,分别对0x0和0xffffffff操作,不知道为什么。简单的记忆好了。

     

    改变位的操作,分为清位和置位。在一行表达式中不要同时出现两种操作,它们是互斥的。

    置位:i |= (1 << 0)| (1 << 1); 将第0位和第1位分别进行了置1的操作。

    清位: i &= (~(1 << 1))&(~(1 << 0)); 将第0位和第1位分别进行了置零

     

    对于那种一个功能包含两个数据位的寄存器,我们可以使用这种方式同时更改两位:

    i |= (3 << (0*2))| (3 << (1*2)); 或者 i |= (3 << 0)| (3 << 2); i = 0x0f

    i &= (~(3 << (0*2)))&(~(3 << (1*2));

     

    也可以使用

    i = 0xAA;

    j = i;

    j &= (~(0x03 << 0)) & (~(0x03 << 2));

    j |= (0x01 << 0) | (0x01 << 2);

    i = j;

    这种形式可以保证只改变自己需要改变的寄存器。

    简化写法为i = ( i & (~(0x03 << 0))) | (0x01 << 0);

    或者直接写为 i = (i & 0xFFFFFFFC)| ();

    下面来个更直接的 (_raw_readl(s3c2410_GPFCON) & (~(3 << 8)) | (1 << 8))

     

     

     

     

     

     

    C++和操作系统面试问题分类

        inline的使用是有所限制的,inline只适合函数体内代码简单的函数使用,不能包含复杂的结构控制语句例如while switch,并且不能内联函数本身不能是直接递归函数(自己内部还调用自己的函数)

        C++多态实现机制:C++中,对于有virtual的类,其sizeof会比正常情况多处4个字节。既在类的最开始四个字节,放的是VTABLE表的地址(void *类型)。而在VTABLE中,所有虚函数是以指针数组的形式存放。 对于派生的类,即时没有重载基类的虚函数,也会在其VTABLE占用一格。造成空间上的浪费。非虚基类没有VTABLE,VTABLE是在构造的时候编译器生成的。

        线程和进程:进程是操作系统资源分配的最小单位,线程是CPU运行的最小单位。linux中,使用的是用户线程(对应核心线程:线程管理由内核实现),而且是1:1形式,既每一个线程,都对应内核中的一个轻量级进程,调度由内核实现,但是线程的管理(比如产生和结束),均有一个管理线程实现。管理线程在第一次调用pthread_create的时候生成。

        软件开发流程:

        需求分析和项目计划:可行性计划,项目计划,需求分析,测试计划

        软件设计说明书:功能设计说明书,实现设计说明书

        使用手册

        测试报告

        项目总结

        C++继承机制:

        n类成员的访问控制方式

        public:类本身、派生类和其它类均可访问;

        protected:类本身和派生类均可访问,其它类不能访问;

        private(默认):类本身可访问,派生类和其它类不能访问。

        继承成员的访问控制规则

        ——由父类成员的访问控制方式和继承访问控制方式共同决定

        private+public(protectd,private)=>不可访问

        pubic(protected)+public=>public(protected)

        public(protected)+protected=>protected

        public(protected)+private(默认)=>private

        C++中的模板和virtual异同? ==>?

        private继承和public继承区别? ==>?

        6. static有什么用途?(请至少说明两种)

        1.限制变量的作用域

        2.设置变量的存储

        7. 引用与指针有什么区别?

        1) 引用必须被初始化,指针不必。

        2) 引用初始化以后不能被改变,指针可以改变所指的对象。

        3) 不存在指向空值的引用,但是存在指向空值的指针。

        8. 描述实时系统的基本特性

        在特定时间内完成特定的任务,实时性与可靠性

        9. 全局变量和局部变量在内存中是否有区别?如果有,是什么区别?

        全局变量储存在静态数据区,局部变量在堆栈

        10. 什么是平衡二叉树?

        左右子树都是平衡二叉树 且左右子树的深度差值的绝对值不大于1

        11. 堆栈溢出一般是由什么原因导致的?

        没有回收垃圾资源

        12. 什么函数不能声明为虚函数?

        constructor  ==>C++中的类的构造函数声明

        13. 冒泡排序算法的时间复杂度是什么?

        O(n^2)

        14. 写出float x 与"零值"比较的if语句。

        if(x>0.000001&&x<-0.000001)

        16. Internet采用哪种网络协议?该协议的主要层次结构?

        tcp/ip 应用层/传输层/网络层/数据链路层/物理层

        17. Internet物理地址和IP地址转换采用什么协议

        ARP (Address Resolution Protocol)(地址解析协议)

        18.IP地址的编码分为哪俩部分?

        IP地址由两部分组成,网络号和主机号。不过是要和"子网掩码"按位与上之后才能区分哪些是网络位哪些是主机位。

        19.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。

        循环链表,用取余操作做  ——>??

        20.不能做switch()的参数类型是:

        SWITH(表达式),表达式可以是整型、字符型以及枚举类型等表达式。

        switch的参数不能为实型。

     

     

     

     

     

     

     

    What will print out?

    main()

    {

    char *p1="name";

    char *p2;

    p2=(char*)malloc(20);

    memset (p2, 0, 20);

    while(*p2++ = *p1++);

    printf("%sn",p2);

    }

    Answer:empty string.

     

     

    What will be printed as the result of the operation below:

    main()

    {

    int x=20,y=35;

    x=y++ + x++;

    y= ++y + ++x;

    printf("%d%dn",x,y);

    }

    Answer : 5794

     

     

    What will be printed as the result of the operation below:

    main()

    {

    int x=5;

    printf("%d,%d,%dn",x,x< <2,x>>2);

    }

    Answer: 5,20,1

     

     

    What will be printed as the result of the operation below:

    #define swap(a,b) a=a+b;b=a-b;a=a-b;

    void main()

    {

    int x=5, y=10;

    swap (x,y);

    printf("%d %dn",x,y);

    swap2(x,y);

    printf("%d %dn",x,y);

    }

    int swap2(int a, int b)

    {

    int temp;

    temp=a;

    b=a;

    a=temp;

    return 0;

    }

    Answer: 10, 5

    10, 5

     

     

    What will be printed as the result of the operation below:

    main()

    {

    char *ptr = " Cisco Systems";

    *ptr++; printf("%sn",ptr);

    ptr++;

    printf("%sn",ptr);

    }

    Answer:Cisco Systems

    isco systems

     

     

    What will be printed as the result of the operation below:

    main()

    {

    char s1[]="Cisco";

    char s2[]= "systems";

    printf("%s",s1);

    }

    Answer: Cisco

     

     

    What will be printed as the result of the operation below:

    main()

    {

    char *p1;

    char *p2;

    p1=(char *)malloc(25);

    p2=(char *)malloc(25);

    strcpy(p1,"Cisco");

    strcpy(p2,"systems");

    strcat(p1,p2);

    printf("%s",p1);

    }

    Answer: Ciscosystems

    The following variable is available in file1.c, who can access it?:

    static int average;

    Answer: all the functions in the file1.c can access the variable.

     

     

    WHat will be the result of the following code?

    #define TRUE 0 // some code

    while(TRUE)

    {

    // some code

    }

    Answer: This will not go into the loop as TRUE is defined as 0.

     

     

    What will be printed as the result of the operation below:

    int x;

    int modifyvalue()

    {

    return(x+=10);

    }

    int changevalue(int x)

    {

    return(x+=1);

    }

    void main()

    {

    int x=10;

    x++;

    changevalue(x);

    x++;

    modifyvalue();

    printf("First output:%dn",x);

    x++;

    changevalue(x);

    printf("Second output:%dn",x);

    modifyvalue();

    printf("Third output:%dn",x);

    }

    Answer: 12 , 13 , 13

     

     

    What will be printed as the result of the operation below:

    main()

    {

    int x=10, y=15;

    x = x++;

    y = ++y;

    printf("%d %dn",x,y);

    }

    Answer: 11, 16

     

     

    What will be printed as the result of the operation below:

    main()

    {

    int a=0;

    if(a==0)

    printf("Cisco Systemsn");

    printf("Cisco Systemsn");

    }

    Answer: Two lines with "Cisco Systems" will be printed.

     

     

     

     

     

     

    1. 以下三条输出语句分别输出什么?[C易]

    char str1[] = "abc";

    char str2[] = "abc";

    const char str3[] = "abc";

    const char str4[] = "abc";

    const char* str5 = "abc";

    const char* str6 = "abc";

    cout << boolalpha << ( str1==str2 ) << endl; // 输出什么?

    cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?

    cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?

    文章出自http://www.ad0.cn/

     

    2. 非C++内建型别 A 和 B,在哪几种情况下B能隐式转化为A?[C++中等]

    答:

    a. class B : public A { ……} // B公有继承自A,可以是间接继承的

    b. class B { operator A( ); } // B实现了隐式转化为A的转化

    c. class A { A( const B& ); } // A实现了non-explicit的参数为B(可以有其他带默认值的参数)构造函数

    d. A& operator= ( const A& ); // 赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个

     

    3. 以下代码中的两个sizeof用法有问题吗?[C易]

    void UpperCase( char str[] ) // 将 str 中的小写字母转换成大写字母

    {

    for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i )

    if( 'a'<=str[i] && str[i]<='z' )

    str[i] -= ('a'-'A' );

    }

    char str[] = "aBcDe";

    cout << "str字符长度为: " << sizeof(str)/sizeof(str[0]) << endl;

    UpperCase( str );

    cout << str << endl;

     

    4. 以下代码有什么问题?[C难]

    void char2Hex( char c ) // 将字符以16进制表示

    {

    char ch = c/0x10 + '0'; if( ch > '9' ) ch += ('A'-'9'-1);

    char cl = c%0x10 + '0'; if( cl > '9' ) cl += ('A'-'9'-1);

    cout << ch << cl << ' ';

    }

    char str[] = "I love 中国";

    for( size_t i=0; i<strlen(str); ++i )

    char2Hex( str[i] );

    cout << endl;

     

    5. 以下代码有什么问题?[C++易]

    struct Test

    {

    Test( int ) {}

    Test() {}

    void fun() {}

    };

    void main( void )

    {

    Test a(1);

    a.fun();

    Test b();

    b.fun();

    }

     

    6. 以下代码有什么问题?[C++易]

    cout << (true?1:"1") << endl;

     

    7. 以下代码能够编译通过吗,为什么?[C++易]

    unsigned int const size1 = 2;

    char str1[ size1 ];

    unsigned int temp = 0;

    cin >> temp;

    unsigned int const size2 = temp;

    char str2[ size2 ];

     

    8. 以下代码中的输出语句输出0吗,为什么?[C++易]

    struct CLS

    {

    int m_i;

    CLS( int i ) : m_i(i) {}

    CLS()

    {

    CLS(0);

    }

    };

    CLS obj;

    cout << obj.m_i << endl;

     

    9. C++中的空类,默认产生哪些类成员函数?[C++易]

    答:

    class Empty

    {

    public:

    Empty(); // 缺省构造函数

    Empty( const Empty& ); // 拷贝构造函数

    ~Empty(); // 析构函数

    Empty& operator=( const Empty& ); // 赋值运算符

    Empty* operator&(); // 取址运算符

    const Empty* operator&() const; // 取址运算符 const

    };

     

    10. 以下两条输出语句分别输出什么?[C++难]

    float a = 1.0f;

    cout << (int)a << endl;

    cout << (int&)a << endl;

    cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?

    float b = 0.0f;

    cout << (int)b << endl;

    cout << (int&)b << endl;

    cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?

     

    11. 以下反向遍历array数组的方法有什么错误?[STL易]

    vector array;

    array.push_back( 1 );

    array.push_back( 2 );

    array.push_back( 3 );

    for( vector::size_type i=array.size()-1; i>=0; --i ) // 反向遍历array数组

    {

    cout << array[i] << endl;

    }

     

    12. 以下代码有什么问题?[STL易]

    typedef vector IntArray;

    IntArray array;

    array.push_back( 1 );

    array.push_back( 2 );

    array.push_back( 2 );

    array.push_back( 3 );

    // 删除array数组中所有的2

    for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )

    {

    if( 2 == *itor ) array.erase( itor );

    }

     

    13. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]

    答:

    void* mymemcpy( void *dest, const void *src, size_t count )

    {

    char* pdest = static_cast<char*>( dest );

    const char* psrc = static_cast<const char*>( src );

    if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了

    {

    for( size_t i=count-1; i!=-1; --i )

    pdest[i] = psrc[i];

    }

    else

    {

    for( size_t i=0; i<count; ++i )

    pdest[i] = psrc[i];

    }

    return dest;

    }

    int main( void )

    {

    char str[] = "0123456789";

    mymemcpy( str+1, str+0, 9 );

    cout << str << endl;

    system( "Pause" );

    return 0;

    }

     

    本试题仅用于考查C++/C程序员的基本编程技能。内容限于C++/C常用语法,不涉及数据结构、算法以及深奥的语法。考试成绩能反映出考生的编程质量以及对C++/C的理解程度,但不能反映考生的智力和软件开发能力。

    笔试时间90分钟。请考生认真答题,切勿轻视。

     

     

     

     

     

     

     

     

     

     

    C++/C试题

    一、请填写BOOL , float, 指针变量 与"零值"比较的 if 语句。(10分)

    请写出 BOOL flag 与"零值"比较的 if 语句。(3分)

    标准答案:

    if ( flag )

    if ( !flag )

    如下写法均属不良风格,不得分。

    if (flag == TRUE)

    if (flag == 1 )

    if (flag == FALSE)

    if (flag == 0)

    请写出 float x 与"零值"比较的 if 语句。(4分)

    标准答案示例:

    const float EPSINON = 0.00001;

    if ((x >= - EPSINON) && (x <= EPSINON)

    不可将浮点变量用"=="或"!="与数字比较,应该设法转化成">="或"<="此类形式。

    如下是错误的写法,不得分。

    if (x == 0.0)

    if (x != 0.0)

     

    请写出 char *p 与"零值"比较的 if 语句。(3分)

    标准答案:

    if (p == NULL)

    if (p != NULL)

    如下写法均属不良风格,不得分。

    if (p == 0)

    if (p != 0)

    if (p)

    if (!)

    二、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)

    char str[] = "Hello" ;

    char *p = str ;

    int n = 10;

    请计算

    sizeof (str ) = 6 (2分)

    sizeof ( p ) = 4 (2分)

    sizeof ( n ) = 4 (2分)

    void Func ( char str[100])

    {

    请计算

    sizeof( str ) = 4 (2分)

    }

    void *p = malloc( 100 );

    请计算

    sizeof ( p ) = 4 (2分)

    三、简答题(25分)

    1、头文件中的 ifndef/define/endif 干什么用?(5分)

    答:防止该头文件被重复引用。

    2、#include 和 #include "filename.h" 有什么区别?(5分)

    3、const 有什么用途?(请至少说明两种)

    试题1:请写一个C函数,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1

    解答:

    int checkCPU( )

    {

        {

            union w

            {

                int a;

                char b;

            } c;

            c.a = 1;

               return(c.b ==1);

        }

    }

    剖析:

    嵌入式系统开发者应该对Little-endian和Big-endian模式非常了解。采用Little-endian模式的CPU对操作数的存放方式是从低字节到高字节,而Big-endian模式对操作数的存放方式是从高字节到低字节。例如,16bit宽的数0x1234在Little-endian模式CPU内存中的存放方式(假设从地址0x4000开始存放)为:

    内存地址

    0x4000

    0x4001

    存放内容

    0x34

    0x12

    而在Big-endian模式CPU内存中的存放方式则为:

    内存地址

    0x4000

    0x4001

    存放内容

    0x12

    0x34

    32bit宽的数0x12345678在Little-endian模式CPU内存中的存放方式(假设从地址0x4000开始存放)为:

    内存地址

    0x4000

    0x4001

    0x4002

    0x4003

    存放内容

    0x78

    0x56

    0x34

    0x12

    而在Big-endian模式CPU内存中的存放方式则为:

    内存地址

    0x4000

    0x4001

    0x4002

    0x4003

    存放内容

    0x12

    0x34

    0x56

    0x78

    联合体union的存放顺序是所有成员都从低地址开始存放,面试者的解答利用该特性,轻松地获得了CPU对内存采用Little-endian还是Big-endian模式读写。如果谁能当场给出这个解答,那简直就是一个天才的程序员。

     

     

    2.

    char str[] = "Hello" ;

    char *p = str ;

    int n = 10;

    请计算

    sizeof (str ) = 6 (2分)

    sizeof ( p ) = 4 (2分)

    sizeof ( n ) = 4 (2分)

    void Func ( char str[100])

    {

    请计算

    sizeof( str ) = 4 (2分)

    }

    void *p = malloc( 100 );

    请计算

    sizeof ( p ) = 4 (2分)

     

    3、在C++程序中调用被 C编译器编译后的函数,为什么要加 extern "C"? (5分)

    答:C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言的不同。假设某个函数的原型为: void foo(int x, int y);

    该函数被C编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。

    C++提供了C连接交换指定符号extern"C"来解决名字匹配问题。

    4.有关内存的思考题

    void GetMemory(char *p)

    {

    p = (char *)malloc(100);

    }

    void Test(void)

    {

    char *str = NULL;

    GetMemory(str);

    strcpy(str, "hello world");

    printf(str);

    }

    请问运行Test函数会有什么样的结果?

    答:程序崩溃。

    因为GetMemory并不能传递动态内存,

    Test函数中的 str一直都是 NULL。

    strcpy(str, "hello world");将使程序崩溃。

    char *GetMemory(void)

    {

    char p[] = "hello world";

    return p;

    }

    void Test(void)

    {

    char *str = NULL;

    str = GetMemory();

    printf(str);

    }

    请问运行Test函数会有什么样的结果?

    答:可能是乱码。

    因为GetMemory返回的是指向"栈内存"的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。

     

     

    void GetMemory2(char **p, int num)

    {

    *p = (char *)malloc(num);

    }

    void Test(void)

    {

    char *str = NULL;

    GetMemory(&str, 100);

    strcpy(str, "hello");

    printf(str);

    }

    请问运行Test函数会有什么样的结果?

    答:

    (1)能够输出hello

    (2)内存泄漏

      

     

     

    void Test(void)

    {

    char *str = (char *) malloc(100);

    strcpy(str, "hello");

    free(str);

    if(str != NULL)

    {

    strcpy(str, "world");

    printf(str);

    }

    }

    请问运行Test函数会有什么样的结果?

    答:篡改动态内存区的内容,后果难以预料,非常危险。

    因为free(str);之后,str成为野指针,

    if(str != NULL)语句不起作用。

     

     

     

     

     

     

     

     

     

     

     

     

    五、编写strcpy函数(10分)

    已知strcpy函数的原型是

    char *strcpy(char *strDest, const char *strSrc);

    其中strDest是目的字符串,strSrc是源字符串。

    (1)不调用C++/C的字符串库函数,请编写函数 strcpy

    (2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

     

    六、编写类String的构造函数、析构函数和赋值函数(25分)

    已知类String的原型为:

    class String

    {

    public:

    String(const char *str = NULL); // 普通构造函数

    String(const String &other); // 拷贝构造函数

    ~ String(void); // 析构函数

    String & operate =(const String &other); // 赋值函数

    private:

    char *m_data; // 用于保存字符串

    };

    请编写String的上述4个函数。

    //普通构造函数
    
    String::String(const
    							char *str)
    
    {
    
    
    					if(str==NULL)
    
            {
    
                    m_data = new
    							char[1]; //得分点:对空字符串自动申请存放结束标志''的
    
                    //加分点:对m_data加NULL 判断
    
                    *m_data = '';
    
            }
    
    
    					else
    						
            {
    
    
    					int length = strlen(str);
    
             m_data = new
    							char[length+1]; // 若能加 NULL 判断则更好
    
             strcpy(m_data, str);
    
            }
    
    }
    
    // String的析构函数
    
    String::~String(void)
    
    {
    
            delete [] m_data; // 或delete m_data;
    
    }
    
    //拷贝构造函数
    
    String::String(const String &other)    // 得分点:输入参数为const型
    
    {    
    
    
    					int length = strlen(other.m_data);
    
            m_data = new
    							char[length+1];     //加分点:对m_data加NULL 判断
    
            strcpy(m_data, other.m_data);   
    
    }
    
    //赋值函数
    
    String & String::operate =(const String &other) // 得分点:输入参数为const 型
    
    {    
    
    
    					if(this == &other)                    //得分点:检查自赋值
    
    
    					return *this;  
    
            delete [] m_data;                //得分点:释放原有的内存资源
    
    
    					int length = strlen( other.m_data );     
    
            m_data = new
    							char[length+1];  //加分点:对m_data加NULL 判断
    
            strcpy( m_data, other.m_data );  
    
    
    					return *this;             //得分点:返回本对象的引用 
    
    }
    

     

     

     

     

     

     

     

     

     

     

    华为的CC++面试题

    Q1:请你分别划划OSI的七层网络结构图,和TCP/IP的五层结构图?

    : Q2:请你详细的解释一下IP协议的定义,在哪个层上面,主要有什么作用?

    : TCP与UDP呢?

    : 总得来说前面两道题目还是比较简单的!

    : Q3:请问交换机和路由器分别的实现原理是什么?分别在哪个层次上面实

    : 现的?

    : Q4:请问C++的类和C里面的struct有什么区别?

    : Q5:请讲一讲析构函数和虚函数的用法和作用?

    : Q6:全局变量和局部变量有什么区别?实怎么实现的?操作系统和编译器

    : 是怎么知道的?

    : Q7:一些寄存器的题目,我忘记了具体实什么题目,主要好像是寻址和内

    : 存管理等一些知识,不记得了。

    : Q8:8086是多少尉的系统?在数据总线上是怎么实现的?还有一些硬件方

    : 面的知识我既不清楚了。

    : 一般建议参加华为的研发面试的同学先要准备一下相关的知识,软件的主要

    : 是看看C和数据结构方面的,硬件模电,数电和微机原理

     

    两道c面试题

    1、一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息。

    程序代码

    #i nclude "stdio.h"

    #i nclude "conio.h"

    struct stu{

    char name[20];

    char sex;

    int no;

    int age;

    struct stu * next;

    }*linklist;

    struct stu *creatlist(int n)

    {

    int i;

    //h为头结点,p为前一结点,s为当前结点

    struct stu *h,*p,*s;

    h = (struct stu *)malloc(sizeof(struct stu));

    h->next = NULL;

    p=h;

    for(i=0;i<n;i++)

    {

    s = (struct stu *)malloc(sizeof(struct stu));

    p->next = s;

    printf("Please input the information of the student: name sex no age ");

    scanf("%s %c %d %d",s->name,&s->sex,&s->no,&s->age);

    s->next = NULL;

    p = s;

    }

    printf("Create successful!");

    return(h);

    }

    void deletelist(struct stu *s,int a)

    {

    struct stu *p;

    while(s->age!=a)

    {

    p = s;

    s = s->next;

    }

    if(s==NULL)

    printf("The record is not exist.");

    else

    {

    p->next = s->next;

    printf("Delete successful!");

    }

    }

    void display(struct stu *s)

    {

    s = s->next;

    while(s!=NULL)

    {

    printf("%s %c %d %d ",s->name,s->sex,s->no,s->age);

    s = s->next;

    }

    }

    int main()

    {

    struct stu *s;

    int n,age;

    printf("Please input the length of seqlist: ");

    scanf("%d",&n);

    s = creatlist(n);

    display(s);

    printf("Please input the age: ");

    scanf("%d",&age);

    deletelist(s,age);

    display(s);

    return 0;

    }

     

     

    2、实现一个函数,把一个字符串中的字符从小写转为大写。

    程序代码

    #i nclude "stdio.h"

    #i nclude "conio.h"

    void uppers(char *s,char *us)

    {

    for(;*s!='';s++,us++)

    {

    if(*s>='a'&&*s<='z')

    *us = *s-32;

    else

    *us = *s;

    }

    *us = '';

    }

    int main()

    {

    char *s,*us;

    char ss[20];

    printf("Please input a string: ");

    scanf("%s",ss);

    s = ss;

    uppers(s,us);

    printf("The result is: %s ",us);

    getch();

    }

  • 相关阅读:
    全卷积神经网络FCN
    面试题 —— 面向对象
    【一题多解】平方根的计算及完全平方数的判断
    【一题多解】Python 字符串逆序
    JVM 自带性能监测调优工具 (jstack、jstat)及 JVM GC 调优
    n 中选 m —— 随机采样的艺术
    位图(bitmap)—— C语言实现
    C++ STL 数据结构与算法 —— 排序
    斐波那契 —— 矩阵形式推导
    常用文本编辑器 editor 的常用插件 —— CopyEdit
  • 原文地址:https://www.cnblogs.com/timssd/p/4107292.html
Copyright © 2011-2022 走看看