n gram:175-200

来源:百度文库 编辑:偶看新闻 时间:2024/05/02 13:21:14

176

 char *GetMemory(void)

char p[] = "hello world";

return p;

}

void Test(void)

{

char *str = NULL;

 

str = GetMemory();  

printf(str);

}

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

答:可能是乱码。

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

 

177

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)内存泄漏

 

178

 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)语句不起作用。

 

179、请阅读以下一段程序,并给出答案。

class A

{

public:

    A(){ doSth(); }

    virtual void doSth(){printf("I am A");}

};

class B:public A

{

public:

    virtual void doSth(){ printf("I am B");}

};

B b;

执行结果是什么?为什么?

答:执行结果是I am A

因为b对象构造时调用基类A的构造函数A(),得此结果。

 

 

 

180  实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数;

答:双向链表删除一个节点P

template void list::delnode(int p)

{

 int k=1;

 listnode *ptr,*t;

 ptr=first;

 

 while(ptr->next!=NULL&&k!=p)

 {

  ptr=ptr->next;

     k++;

 }

    t=ptr->next;

 cout<<"你已经将数据项 "<data<<"删除"<

 

 ptr->next=ptr->next->next;

 length--;

 delete t;

}

 

在节点P后插入一个节点:

template bool list::insert(type t,int p)

{

 listnode *ptr;

 ptr=first;

 

 int k=1;

 while(ptr!=NULL&&k

 {

  ptr=ptr->next;

  k++;

 }

 if(ptr==NULL&&k!=p)

  return false;

 else

 {

   listnode *tp;

   tp=new listnode;

   tp->data=t;

   tp->next=ptr->next;

   ptr->next=tp;

   length++;

  

   return true;

 }

}

 

181.完成下列程序

 

*

 

*.*.

 

*..*..*..

 

*...*...*...*...

 

*....*....*....*....*....

 

*.....*.....*.....*.....*.....*.....

 

*......*......*......*......*......*......*......

 

*.......*.......*.......*.......*.......*.......*.......*.......

 

#i nclude

using namespace std;

 

const int n = 8;

 

main()

{

   int i;

   int j;

   int k;

 

   for(i = n; i >= 1; i--)

   {

      for(j = 0; j < n-i+1; j++)

      {

         cout<<"*";

         for(k=1; k < n-i+1; k++)

        {

            cout<<".";

         }

      }

      cout<

   }

   system("pause");

}

 

 

182完成程序,实现对数组的降序排序

 

#include

using namespace std;

 

void sort(int* arr, int n);

 

int main()

 

{

   int array[]={45,56,76,234,1,34,23,2,3};

   sort(array, 9);

   for(int i = 0; i <= 8; i++)//曾经在这儿出界

      cout<

   cout<

   system("pause");

}

 

void sort(int* arr, int n)

{

   int temp;

   for(int i = 1; i < 9; i++)

   {

      for(int k = 0; k < 9 - i; k++)//曾经在这儿出界

      {

         if(arr[k] < arr[k + 1])

            {

               temp = arr[k];

               arr[k] = arr[k + 1];

               arr[k + 1] = temp;

            } 

      }

   }

}

 

183. 以下两条输出语句分别输出什么?[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; // 输出什么?

1

1065353216

boolalpha0

0

0

boolalpha1

 

51. 以下反向遍历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;

}

 

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

答:

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

{

    char* pdest = static_cast( dest );

    const char* psrc = static_cast( src );

    if( pdest>psrc && pdest

    {

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

                pdest[i] = psrc[i];

    }

    else

    {

        for( size_t i=0; 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;

}

 

185 对于C++中类(class) 与结构(struct)的描述正确的为:

  A,类中的成员默认是private的,当是可以声明为public,private 和protected,

       结构中定义的成员默认的都是public;

  B,结构中不允许定义成员函数,当是类中可以定义成员函数;

  C,结构实例使用malloc() 动态创建,类对象使用new 操作符动态分配内存;

  D,结构和类对象都必须使用new 创建;

  E,结构中不可以定义虚函数,当是类中可以定义虚函数.

  F,结构不可以存在继承关系,当是类可以存在继承关系.

答:A,D,F

 

186.两个互相独立的类:ClassA 和 ClassB,都各自定义了非静态的公有成员函数 PublicFunc() 和非静态的私有成员函数 PrivateFunc();

   现在要在ClassA 中增加定义一个成员函数ClassA::AdditionalPunction(ClassA a,ClassB b);则可以在AdditionalPunction(ClassA x,ClassB y)的实现部分(函数功能体内部)

    出现的合法的表达是最全的是:

    A,x.PrivateFunc();x.PublicFunc();y.PrivateFunc();y.PublicFunc();

    B,x.PrivateFunc();x.PublicFunc();y.PublicFunc();

    C,x.PrivateFunc();y.PrivateFunc();y.PublicFunc();

    D,x.PublicFunc();y.PublicFunc();

答:B

 

186.C++程序下列说法正确的有:

  A,对调用的虚函数和模板类都进行迟后编译.

  B,基类与子类中函数如果要构成虚函数,除了要求在基类中用virtual 声名,而且必须名字相同且参数类型相同返回类型相同

  C,重载的类成员函数都必须要:或者返回类型不同,或者参数数目不同,或者参数序列的类型不同.

  D,静态成员函数和内联函数不能是虚函数,友员函数和构造函数也不能是虚函数,但是析构函数可以是虚函数.

答:A

***************************************************************************

 

187  头文件的作用是什么?

答:一、通过头文件来调用库功能。在很多场合,源代码不便(或不准)向用户公布,只要向用户提供头文件和二进制的库即可。用户只需要按照头文件中的接口声明来调用库功能,而不必关心接口怎么实现的。编译器会从库中提取相应的代码。

二、头文件能加强类型安全检查。如果某个接口被实现或被使用时,其方式与头文件中的声明不一致,编译器就会指出错误,这一简单的规则能大大减轻程序员调试、改错的负担。

 

188、以下为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分)

 

 

3写出下列程序的运行结果。

 

unsigned int i=3;

cout<

 

189.写出下列程序所有可能的运行结果。

 

int a;

int b;

int c;

 

void F1()

{

b=a*2;

a=b;

}

 

void F2()

{

c=a+1;

a=c;

}

 

main()

{

a=5;

//Start F1,F2 in parallel

F1(); F2();

printf("a=%d\n",a);

}a=11

 

190一个链表的操作,注意代码的健壮和安全性。要求:

(1)增加一个元素;

(2)获得头元素;

(3)弹出头元素(获得值并删除)。

 

 

191.unsigned short array[]={1,2,3,4,5,6,7};

int i = 3;

*(array + i) = ?

答:

4

 

192

class A

{

  virtual void func1();

  void func2();

}

Class B: class A

{

  void func1(){cout << "fun1 in class B" << endl;}

  virtual void func2(){cout << "fun2 in class B" << endl;}

}

A, A中的func1和B中的func2都是虚函数.

B, A中的func1和B中的func2都不是虚函数.

C, A中的func2是虚函数.,B中的func1不是虚函数.

D, A中的func2不是虚函数,B中的func1是虚函数.

 

答:

A

 

193输出下面程序结果。

#include

class A

{

public:

 virtual void print(void)

 {

    cout<<"A::print()"<

 }

};

class B:public A

{

public:

 virtual void print(void)

 {

   cout<<"B::print()"<

 };

};

class C:public B

{

public:

 virtual void print(void)

 {

  cout<<"C::print()"<

 }

};

void print(A a)

{

   a.print();

}

void main(void)

{

   A a, *pa,*pb,*pc;

   B b;

   C c;

  

   pa=&a;

   pb=&b;

   pc=&c;

  

   a.print();

   b.print();

   c.print();

  

   pa->print();

   pb->print();

   pc->print();

  

   print(a);

   print(b);

   print(c);

}

 

A::print()

A::print()

B::print()

C::print()

A::print()

B::print()

C::print()

A::print()

A::print()

A::print()

 

--------------------------------------------------------------------------

194.程序改错

class mml

{

  private:

    static unsigned int x;

  public:

    mml(){ x++; }

    mml(static unsigned int &) {x++;}

    ~mml{x--;}

  pulic:

    virtual mon() {} = 0;

    static unsigned int mmc(){return x;}

    ......                     

 

};

class nnl:public mml

{

  private:

    static unsigned int y;

  public:

    nnl(){ x++; }

    nnl(static unsigned int &) {x++;}

    ~nnl{x--;}

  public:

    virtual mon() {};

     static unsigned int nnc(){return y;}

    ......                  

};

 

代码片断:

mml* pp = new nnl;

..........

delete pp;

 

 

A:

基类的析构函数应该为虚函数

virtual ~mml{x--;}

 

--------------------------------------------------------------------------

195.101个硬币100真、1假,真假区别在于重量。请用无砝码天平称两次给出真币重还是假币重的结论。

答:

101个先取出2堆,

33,33

第一次称,如果不相等,说明有一堆重或轻

那么把重的那堆拿下来,再放另外35个中的33

如果相等,说明假的重,如果不相等,新放上去的还是重的话,说明假的轻(不可能新放上去的轻)

 

第一次称,如果相等的话,这66个肯定都是真的,从这66个中取出35个来,与剩下的没称过的35个比

下面就不用说了

 

方法二:

第3题也可以拿A(50),B(50)比一下,一样的话拿剩下的一个和真的比一下。

如果不一样,就拿其中的一堆。比如A(50)再分成两堆25比一下,一样的话就在

B(50)中,不一样就在A(50)中,结合第一次的结果就知道了。

 

196.写出程序结果:

void Func(char str[100])

{

  printf("%d\n", sizeof(str));

}

 

答:

4

分析:

指针长度

 

197.int id[sizeof(unsigned long)];

    这个对吗?为什么??

答:

这个 sizeof是编译时运算符,编译时就确定了

可以看成和机器有关的常量。

 

198、 sizeof应用在结构上的情况

请看下面的结构:

struct MyStruct

{

double dda1;

char dda;

int type

};

对结构MyStruct采用sizeof会出现什么结果呢?sizeof(MyStruct)为多少呢?也许你会这样求:

sizeof(MyStruct)=sizeof(double)+sizeof(char)+sizeof(int)=13

但是当在VC中测试上面结构的大小时,你会发现sizeof(MyStruct)为16。你知道为什么在VC中会得出这样一个结果吗?

其实,这是VC对变量存储的一个特殊处理。为了提高CPU的存储速度,VC对一些变量的起始地址做了"对齐"处理。在默认情况下,VC规定各成员变量存放的起始地址相对于结构的起始地址的偏移量必须为该变量的类型所占用的字节数的倍数。下面列出常用类型的对齐方式(vc6.0,32位系统)。

类型

对齐方式(变量存放的起始地址相对于结构的起始地址的偏移量)

Char

偏移量必须为sizeof(char)即1的倍数

int

偏移量必须为sizeof(int)即4的倍数

float

偏移量必须为sizeof(float)即4的倍数

double

偏移量必须为sizeof(double)即8的倍数

Short

偏移量必须为sizeof(short)即2的倍数

各成员变量在存放的时候根据在结构中出现的顺序依次申请空间,同时按照上面的对齐方式调整位置,空缺的字节VC会自动填充。同时VC为了确保结构的大小为结构的字节边界数(即该结构中占用最大空间的类型所占用的字节数)的倍?

 

199

#include "stdafx.h"

 Y n P }2{&k O v H `,o0

#define SQR(X) X*X

int main(int argc, char* argv[])

{

int a = 10;

int k = 2;

int m = 1;

a /= SQR(k+m)/SQR(k+m);

printf("%d\n",a);

return 0;

}

这道题目的结果是什么啊?

define 只是定义而已,在编择时只是简单代换X*X而已,并不经过算术法则的

a /= k+m*k+m/k+m*k+m;

=>a /= (k+m)*1*(k+m);

=>a = a/9;

=>a = 1;

 

200.下面的代码有什么问题?

 

void DoSomeThing(...)

{

char* p;

p = malloc(1024); // 分配1K的空间

2y x

if (NULL == p)

return;

p = realloc(p, 2048); // 空间不够,重新分配到2K

if (NULL == p)

return;

}

A:

p = malloc(1024);     应该写成: p = (char *) malloc(1024);

        没有释放p的空间,造成内存泄漏。