c++绝版面试题--9.7(8)

2020-02-22 13:13

}

优点:程序简洁

缺点:多执行了N-1次逻辑判断,并且打断了循环“流水线”作业,使得编译器不能对循环进行优化处理,降低了效率。

优点:循环的效率高 缺点:程序不简洁 175

void GetMemory(char *p) {

p = (char *)malloc(100); }

void Test(void) {

char *str = NULL; GetMemory(str);

strcpy(str, \printf(str); }

请问运行Test函数会有什么样的结果? 答:程序崩溃。

因为GetMemory并不能传递动态内存, Test函数中的 str一直都是 NULL。

strcpy(str, \将使程序崩溃。

176

char *GetMemory(void) {

char p[] = \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, \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(\};

class B:public A {

public:

virtual void doSth(){ printf(\}; 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<<\你已经将数据项 \删除\

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(\}

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<

system(\}

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];


c++绝版面试题--9.7(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2016中考王中考命题研究(河北)语文:专题二十六2016年说明文分

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: