嵌入式软件工程师面试题1

1、what will happen after running this program?

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void getMemory(char *p, int num){
 5     p = (char*)malloc(sizeof(char)*num);
 6 }
 7 
 8 int main(void){
 9     char* str = NULL; 
10     getMemory(str,100);
11     strcpy(str,"hello");
12     cout<<str<<endl;
13     return 0;
14 }

  问题出在void getMemory(char *p, int num)里面,这里面的*p其实是main()里str的一个副本,编译器总要为函数的每个参数制作临时副本。在这里,p申请新内存,只是把p所指的内存地址改变了,但是str没有变。因为getMemory()没有返回值,所以str并不指向p申请的那个内存,所以getMemory()不能输出任何东西。而且,每次执行getMemory()都会申请一个内存,但是内存没有释放,会造成内存泄露。

  如果一定要用指针去申请内存,那么要采用指向指针的指针,传str的地址给函数getMemory(),代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void getMemory(char **p, int num){
 5     *p = (char*)malloc(sizeof(char)*num);
 6 }
 7 
 8 int main(void){
 9     char* str = NULL; 
10     getMemory(str,100);
11     strcpy(&str,"hello");
12     cout<<*str<<endl;
13     cout<<str<<endl;
14     cout<<&str<<endl;
15     return 0;
16 }

  这样的话,程序可以运行成功。打印出来发现,*str, str, &str的值分别是h, hello, 0x0012ff7c,str是字符串的值,*str是str首字符,&str是str的地址值。

  “指向指针的指针”不好理解,可以用函数返回至来传递动态内存,如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 char* getMemory(char *p, int num){
 5     p = (char*)malloc(sizeof(char)*num);
 6     return p;        
 7 }
 8 
 9 int main(void){
10     char* str = NULL; 
11     str = getMemory(str,100);
12     strcpy(str,"hello");
13     cout<<str<<endl;
14     return 0;
15 }

  注意这里面的内存还是没有释放的,使用的时候请注意。

  下面来推广一下,看看整形变量怎么样:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void getMemory(int *p){
 5     *p = 5;        
 6 }
 7 
 8 int main(void){
 9     int v;
10     getMemory(&v);
11     cout<<v<<endl;
12     return 0;
13 }

  getMemory()把V的地址传了进来,*p是地址里的值,V是副本。通过直接修改地址里的值,不需要返回值,也把V修改了,因为V所指地址的值发生了改变。

posted @ 2013-04-02 10:50  gjhqq  阅读(305)  评论(0)    收藏  举报