本人在面试的时候遇到了这几个道目,自己确定不了答案,请各位高手指点:
1.在Intel 32们平台下,试问如下定义的变量占有的内存空间为多少字节?
int **abc[3][4];2.编程规范中一般要求不要写出类似(++i)+(i++)或f(++i,i++)这样的代码,请说明原因?3.下面函数实现了将一堆栈内数据的清空功能,请试着找出程序中存在的问题:
class Stack
{
    int pop(void);
    void push(int);
    int count(void);
};
void clear(stack& stack)
{
    for(int i=0;i<stack.count();i++)
    {
       stack.pop();
    }
}4.#include <iostream.h>
void main()
{
   cout<<"Hello world"<<endl;
}
其输出结果为:
Hello world;
如何在不修改main()函数体的条件下使程序输出结果为:
Initialize
Hello world
clear up5.char *strcpy(_out_z char* _Dst,_in_z const char* _Src);
此函数返回值为什么是char*,有什么作用?

解决方案 »

  1.   

    回答如下(仅供参考):
         1.48
         2.前后i会发生改变,一旦多了就很容易搞混,可读性差,,给检测带来不便
         3.wait
         4.wait
         5.wait
      

  2.   

    3.    
         for(int   i=0;i <stack.count();i++) 
            { 
                  stack.pop(); 
            }     问题在这里,假如stack里有5个元素,那么stack.pop()后,stack.count()的数目将递减,那么当i大于stack.count()时将导致部分元素无法清楚。正确做法应是
       int nCount = stack.count();
               for(int   i=0;i <nCount;i++) 
            { 
                  stack.pop(); 
            } 
      

  3.   

    面试题有些难度。1、48,3*4*sizeof(int**);
    2、可读性差,而且不同的编译器结果不同;
    3、i<stack.count()每次循环都要计算,每次计算的结果不同(因为Count在变小);另外,类成员默认为私有成员,不能被外部非友元调用;
    4、构造一个类,定义一个该类的全局变量,利用构造和析构函数实现;
    5、为了编程使用方便。
      

  4.   

    4.
    .#include   <iostream.h> 
    class g_Info
    {
       g_Info()
    {
       cout < <"Initialize" < <endl; 
    }g_Info()
    {
       cout < <"clear   up " < <endl; 
    }};
    g_Info myInfo;
    void   main() 

          cout < <"Hello   world" < <endl; 

      

  5.   

      上面写错了一个地方:4. 
    .#include       <iostream.h>   
    class   g_Info 

          g_Info() 

          cout   <   <"Initialize"   <   <endl;   
    } ~g_Info()  // 析构函数

          cout   <   <"clear       up   "   <   <endl;   
    } }; 
    g_Info   myInfo; 
    void       main()   
    {   
                cout   <   <"Hello       world"   <   <endl;   
    }   
      

  6.   

    3.我只发现void   clear(stack&   stack) 
    里的stack&应改为Stack &,,也没做具体的验证
    4.给main加参数试试,,
    5.strcpy这个函数本身是这样子定义的,,没有为什么,,至于作用呢,
      应该是返回首地址,,
          比如:strcpy(ss,"hello");是把ss的首地址返回,,----------------
        以上为个人看法,,有误之处请后来者帮忙指正,,
      

  7.   

    1. 一个指向3*4个指针元素的数组,win32下一个指针4个字节,12*4=48
    2.首先语法隐晦,可读性不强,其次不同的编译器对对其进行不同的优化,以导致累加数不同
    3.public 关键字,count应该记录的是栈内元素数量 每pop一次 count就会减一,所以应为 while stack.count!=0 stack.pop();4.本人愚见:在main前声明一个全局变量 在其constructor中输出Initialize,distructor中输出clearn up
    5.方便了链市式操作 如 strcpy(a,strcpy(m,n))
      

  8.   

    stack&   和  Stack   & 是等效的。
      

  9.   

    to 2楼 clever101 :
         我似乎没发现什么不同,,不跟没改一样吗??
      

  10.   

    quote:to   2楼   clever101   : 
              我似乎没发现什么不同,,不跟没改一样吗??         for(int       i=0;i   <stack.count();i++)   // 这里stack.count()是变化的
                    {   
                                stack.pop();   
                    }        
               int   nCount   =   stack.count(); 
                          for(int       i=0;i   <nCount;i++)   // 这里nCount没有变化
                    {   
                                stack.pop();   
                    }        编两个程序对比一下你就知道区别了,编程要多实践的。    
      

  11.   

        道理:
    全局变量在进入main函数之前构造,在退出main函数后析构。
      

  12.   

    问题5链式操作:如strcmp(char *str Source,strcpy(_out_z   char*   _Dst,_in_z   const   char*   _Src)); 
      

  13.   

    谢谢clever101的指教,,与楼主共同学习ing,,,
      

  14.   

    不知道这样算不算?
    #include <iostream.h>#define cout cout<<"Initialize\n"
    #define endl endl<<"clear up\n"void main()
    {
    cout<<"Hello World!"<<endl;
    }
    实现方式不如4楼的正宗.
      

  15.   

    clever 101很强!
    3的确要很小心才能发现,偶开始想的是否因为每个指针都指向stack的最后一个元素的下一个位置去了,后来想stack.count()肯定应该是全部元素计数而不应该包含最后一个,还有stack最好模板化及加入出错处理
    别的题目都很简单了
      

  16.   

    17楼的好方法,
    5题在林锐的高质量C编程中说是
    为了实现链式表达式:
    例如:    int length = strlen(strcpy (strDest,"vmlinux"));不知道还有其他说法没.
      

  17.   

    4.using namespace std;inline ostream& operator << (ostream& io, const char* szBuff)
    {
    printf("Initialize\n%s\nclear up", szBuff);
    return io;
    }void   main()
    {
          cout < <"Hello   world" < <endl;
      

  18.   


    3.下面函数实现了将一堆栈内数据的清空功能,请试着找出程序中存在的问题: 
    .....
    void   clear(stack&   stack) 

      for(int   i=0;i <stack.count();i++) 
      { 
        stack.pop(); 
      } 

    我觉得这里倒着来应当好一点: 
    for(int i=stack.count() - 1; i >= 0; i--)
    {
      stack.pop();
    }