1. 写出以下程序的输出结果
#include <iostream>
using namespace std;
class A
{
public: virtual void print(void)
{
cout << "A::print()" << endl;
};
};
class B : public A
{
public: virtual void print(void)
{
cout << "B::print()" << endl;
};
};
class C : public A
{
public: void print(void)
{
cout << "C::print()" << endl;
};
};
void print(A a)
{
a.print();
}
int main()
{
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);
return 0;
}
using namespace std;
class A
{
public: virtual void print(void)
{
cout << "A::print()" << endl;
};
};
class B : public A
{
public: virtual void print(void)
{
cout << "B::print()" << endl;
};
};
class C : public A
{
public: void print(void)
{
cout << "C::print()" << endl;
};
};
void print(A a)
{
a.print();
}
int main()
{
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);
return 0;
}
答案:
A::print()
B::print()
C::print()
A::print()
B::print()
C::print()
A::print()
A::print()
A::print()
2. What will happen after running the "Test"?
#include <iostream>
void GetMemory(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
}
int main()
{
char *str = NULL;
GetMemory(str, 100);
strcpy(str, "hello");
return 0;
}
void GetMemory(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
}
int main()
{
char *str = NULL;
GetMemory(str, 100);
strcpy(str, "hello");
return 0;
}
分析:由于void GetMemory(char *p, int num)中的*p实际上是主函数中的一个str的一个副本,编译器总是要为函数的每个参数制作临时副本。在本倒中,p申请了新的内存,只是把p把指的内存地址改变了,但str丝毫未变。因为GetMemory没有返回值,因此str并不指向p所申请的那段内存,所以函数GetMemory并不能输出任何东西。事实上,每执行一次GetMemory就会申请一块内存,但申请的内存却不能有效释放,结果是内存一直被独占,最终造成内存泄露。
答案:程序崩溃。因为GetMemory并不能传递动态内存,Test函数中的str一直都是NULL。
以下是正常的申请方式:
方法一:用指针参数去申请内存时,应用采用指向指针的指针,把str的地址传给函数GetMemory
#include <iostream>
void GetMemory(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
int main()
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
std::cout << *str << std::endl;
std::cout << str << std::endl;
std::cout << &str << std::endl;
return 0;
}
void GetMemory(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
int main()
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
std::cout << *str << std::endl;
std::cout << str << std::endl;
std::cout << &str << std::endl;
return 0;
}
输出:
h
hello
0012FF7C
方法二:用函数返回值来传递动态内存
#include <iostream>
char *GetMemory(char *p, int num)
{
return p = (char *)malloc(sizeof(char) * num);
}
int main()
{
char *str = NULL;
str = GetMemory(str, 100);
strcpy(str, "hello");
std::cout << *str << std::endl;
std::cout << str << std::endl;
std::cout << &str << std::endl;
return 0;
}
char *GetMemory(char *p, int num)
{
return p = (char *)malloc(sizeof(char) * num);
}
int main()
{
char *str = NULL;
str = GetMemory(str, 100);
strcpy(str, "hello");
std::cout << *str << std::endl;
std::cout << str << std::endl;
std::cout << &str << std::endl;
return 0;
}
3. What will happen after running the "Test"?
#include <iostream>
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
int main()
{
char *str = NULL;
str = GetMemory();
std::cout << str << std::endl;
return 0;
}
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
int main()
{
char *str = NULL;
str = GetMemory();
std::cout << str << std::endl;
return 0;
}
答案:可能是乱码,也有可能是正常输出。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是NULL,但其原来的内容已经被清除,新内容不可知。