loading ...
loading...

2008-07-24 | c++/c测试题

分享

1.char s[]="";

sizeof(s)=1;

三、简答题

1、(1)头文件中的ifndef/define/endif的作用

答:防止头文件重复引用

(2)#include<filename.h>和#include“filename.h”的区别

答:)#include<filename.h>编译器从开发环境设置的路径开始搜索filename.h;#include“filename.h”

编译器从用户工作目录开始搜索。

2、const的用途(至少说出两种)

答:(1)定义const常量

(2)修饰函数的参数、返回值、函数的定义体,被const修饰的东西受到强制保护,防止被意外修改,提高程序的健壮性。

3、在c++程序中调用被c编译器编译后的函数,为是么要加extern “c”

答:c++支持函数重载,c不支持函数重载。函数被c++编译在库中的名字和c语言不同,因此,c++提供了连接交换指定符号extern来解决名字匹配问题。

4、简述以下两个for循环的优缺点

//第一个                                            //第二个

for(i=0;i<N;i++)                             if(condition)

{                                               {

 if(condition)                                         for(i=0;i<N;i++)

   DoSomething();                                      DoDoSomething(); 

else                                             }

  DoOtherthing();                                else

}                                                 { for(i=0;i<N;i++)

                                                     DoOtherthing();

                                                   }

优点:程序简洁                             优点:循环效率高。

缺点:多执行了N-1次逻辑判断,并且打断了      缺点:程序不简洁。

循环“流水线”作业,使得编译器不能对循环

进行优化处理,降低了效率。

四、有关内存的思考题

运行Test函数会有事么结果,为是么?

1、void GetMemory1(char *p,int num)
{
 *p=(char *)malloc(sizeof(char)*num);
}

void Test(void)

{  char *str=NULL;

   GetMemory1(str);

   strcpy(str,"hello world");

   printf(str);

}

答:程序崩溃。GetMemory不能返回动态内存,test里面的str一直为NULL,strcpy将使程序崩溃。

2、char *GetMemory2(void)

{
 char p[]="hello world!";
 return p;
}

void Test(void)

{ char *str=NULL;

   str=GetMemory2();

   printf(str);

}

答:可能是乱码。GetMemory返回的是指向“栈内存”指针,地址不为NULL,但返回后原有的内容已被清空,新内容不可知

3、void GetMemory3(char **p,int num)
{
 *p=(char *)malloc(sizeof(char)*num);
}

void Test(void)

{ char *str=NULL;

 GetMemory3(&str,100);

strcpy(str,"hello");

printf(str);

}

答:输出hello。但存在内存泄漏。

4、void Test(void)

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

strcpy(str,"hello");

free(str);

if(str!=NULL) {
  strcpy(str,"world");
  printf("%s\n",str);
 }

}

答:输出hello,但篡改动态内存的内容,后果难以预料,非常危险。因free之后,str指针成为野指针,if(str!=NULL)语句不再起作用。

五、类型转换

double d=100.25;

int x=d;

int *pInt=(int *)&d;

请问以下两个输出语句的结果是否相同,为是么?

cout<<x<<endl;

cout<<*pInt<<endl;

答:(1)输出100,x取d的整数部分。(2)输出的不是100,*pInt指向的的前4个字节的数值,而不是d的整数部分。

六、编写strrcpy函数。已知原型为char *strcpy(char *strDest,const char *strSrc);

(1)不调用字符串的库函数,实现strcpy

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

答:

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

{  assert((strDest!=NULL)&&(strSrc!=NULL));//judge wether the two pointers are NULL

   char *address=strDest;

   while((*strDest++=*strSrc++)!='\0') NULL;

   return address;

}

(2)为了实现链式表达

七、编写string类的构造函数、析构函数和赋值函数

类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::string(const char *str))//普通构造函数

{   if(str==NULL) 

    {   m_data=new char[1];

       m_data='\0';

     }

    else

    {   m_data=new char[strlen(str)+1];

       strcpy(m_data,str);

    }

}

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

{   m_data=new char[strlen(other.m_data)+1];

    strcpy(m_data,other.m_data);

}

string &string::operate=(const string&other)//赋值函数

{   if(this==&other)//(1)检查自赋值

        return *this;

    delete []m_data //(2)释放原有空间

    m_data=new char[strlen(other.m_data)+1];//(3)重新分配空间并赋值

    strcpy(m_data,other.m_data);

   return *this;//(4)返回本对象的引用

}

~string::string(void)

{   delete []m_data;

}

分享 分享 |  评论 (0) |  阅读 (?)  |  固定链接 |  类别 (技术学习) |  发表于 11:16  | 最后修改于 2008-07-24 11:41
搜狐博客温馨提示:搜狐博客官方不会要求参加活动的各位博友缴纳任何的手续费用。请勿轻信留言、评论中的中奖信息,更不要拨打陌生电话及向陌生帐户汇款,谨防受骗!识别更多网络骗术,请 点击查看详情
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  *中国人爱国心,搜狗输入法爱国主题皮肤下载>>
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复