c,c++基本功
来源:互联网 发布:机械设计手册软件2016 编辑:程序博客网 时间:2024/04/30 02:48
1,char p[3][10]
sizeof(p) = 30
void fun(char p[][10])
{
sizeof(p) = 4
}
char * p = new char[10]
sizeof(p) = 4
union A{
int a;
char b;
}
sizeof(A) = 4
struct B{
int a;
char b;
}
sizeof(B) = 5
2, 有关内存的思考
void getmemory(char *p)
{
p = (char *)malloc(100);
}
void test(void)
{
char *str = null;
getmemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行test 函数会有什么样的结果?
答:程序崩溃。
因为getmemory 并不能传递动态内存,
test 函数中的 str 一直都是 null。
strcpy(str, "hello world");将使程序崩溃。
char *getmemory(void)
{
char p[] = "hello world";
return p;
}
void test(void)
{
char *str = null;
str = getmemory();
printf(str);
}
请问运行test 函数会有什么样的结果?
答:可能是乱码。
因为getmemory 返回的是指向“栈内存”的指针,该指针的地址不是 null,但其原现的内容已经被清除,新内容不可知。
char *getmemory2(void)
{
char* p = "hello world";
return p;
}
void test(void)
{
char *str = null;
str = getmemory();
printf(str);
}
请问运行test函数会有什么结果?
答:正常输出"hello world"。
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)内存泄漏
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)语句不起作用。
指出以下定义的变量分别存储在什么内存区域
int b;栈
char s[]="abc"; //栈
char *p2; //栈
char *p3="123456"; //123456/0在常量区,p3在栈上。
static int c=0;//全局(静态)初始化区
p1 = (char*)malloc(10);
p2 = (char*)malloc(20); //分配得来得10和20字节的区域就在堆区。
strcpy(p1,"123456"); //123456/0放在常量区,编译器可能会将它与p3所向"123456"优化成一个地方。
3, 编写类string 的构造函数、析构函数和赋值函数(25 分)
已知类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 的上述4 个函数。
标准答案:
// string 的析构函数
string::~string(void) // 3 分
{
delete [] m_data;
// 由于m_data 是内部数据类型,也可以写成 delete m_data;
}
// string 的普通构造函数
string::string(const char *str) // 6 分
{
if(str==null)
{
m_data = new char[1]; // 若能加 null 判断则更好
*m_data = ‘/0’;
}
else
{
int length = strlen(str);
m_data = new char[length+1]; // 若能加 null 判断则更好
strcpy(m_data, str);
}
}
// 拷贝构造函数
string::string(const string &other) // 3 分
{
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 null 判断则更好
strcpy(m_data, other.m_data);
}
// 赋值函数
string & string::operate =(const string &other) // 13 分
{
// (1) 检查自赋值 // 4 分
if(this == &other)
return *this;
// (2) 释放原有的内存资源 // 3 分
delete [] m_data;
// (3)分配新的内存资源,并复制内容 // 3 分
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 null 判断则更好
strcpy(m_data, other.m_data);
// (4)返回本对象的引用 // 3 分
return *this;
}
4, 基于对象和面向对象的区别?
面向对象提供继承的机制,允许扩展。基于对象仅仅提供封装的特性,即在已有对象基础上调用其成员函数。
5,public,private,protected三种继承方式的作用?
public是使用最多的一种继承,“is-a"关系。从语义角度上来说,public继承是一种接口继承,根据面向对象中的关系而言就是,派生类可以代替基类完成基类接口所声明的行为,也就是必须符合“Liskov替换原则(LSP)”,此时派生类可以自动转换成为基类的接口,完成接口转换。
从语法角度上来说,public继承会保留基类中成员(包括函数和变量等)的可见性不变,也就是说,基类的public成员为派生类的public成员,基类的protected成员为派生类的protected成员
private继承,基类的public成员在子类中是私有成员,private提供了”has-a“的关系,最后用组合代替。
从语义角度上来说,private继承是一种实现继承,根据面向对象中的关系而言就是,派生类不能代替基类完成基类接口所声明的行为,也就是不符合“Liskov替换原则(LSP)”,此时派生类不能自动转换成为基类的接口,就算通过类型转换(static_cast和dynamic_cast)也会得到一个空指针。
从语法角度上来说,private继承会将基类中的public和protected可见性的成员修改成为private可见性,这样一来虽然派生类中同样还是可以调用基类的protected和public成员,但是在派生类的派生类就不可以再调用被private继承的基类的成员了。
protect继承,和private作用类似,只不过基类的public成员在子类中是保护成员,允许子类的派生类访问。从语义角度上来说,protected继承是一种实现继承,根据面向对象中的关系而言就是,派生类不能代替基类完成基类接口所声明的行为,也就是不符合“Liskov替换原则(LSP)”,此时派生类不能自动转换成为基类的接口,就算通过类型转换(static_cast和dynamic_cast)也会得到一个空指针。
从语法角度上来说,protected继承会将基类中的public可见性的成员修改成为protected可见性,相当于在派生类中引入了protected成员,这样一来在派生类中同样还是可以调用基类的protected和public成员,派生类的派生类就也可以调用被protected继承的基类的protected和public成员
- C/C++基本功1
- C/C++编程基本功
- c,c++基本功
- C语言基本功教程系列(1)
- C语言基本功教程系列 循环
- C语言基本功教程系列 if 语句
- C语言基本功教程系列锦集
- C语言基本功教程系列(1)
- 基本功练习_3_2_之(GPRS.c)
- c/c++基本功测试题一套
- C语言基本功教程系列(2) - if 语句
- C语言基本功教程系列 快速的函数调用
- C语言基本功教程系列 高效无错的内存访问
- C语言基本功教程系列 文件, Socket 和 其它
- c语言基本功不扎实,竟然这个不晓得
- C语言基本功教程系列(2) - if 语句
- C语言基本功教程系列(3) - 快速的函数调用
- Linux内核入门(三)—— C语言基本功
- MII接口详解【转】
- 发现RZCheckBox的BUG
- 对Gens的裁剪
- 精通STRUTS: Tiles 框架
- 用retrieve刷新时不清除原数据
- c,c++基本功
- 按钮的重复提交
- VS2008 and .NET Framework3.5
- windows任务栏图标编程步骤
- 上海 大型IT上市公司 招聘 高级上位机软件工程师
- PHP学习一(基础)
- 动态调用
- Spring Quartz定时详情
- gridview 打开新页 并且传递参数