【很简单的面试题】为什么这个样会错?

WYD2608 2010-07-28 12:33:03
#include <stdio.h>
#include <malloc.h>
#include <string.h>

void GetMemory(char *p)
{
p=(char *)malloc(100);
}

void main()
{
char *str=NULL;
GetMemory(str);
strcpy(str,"hello world!");
printf(str);
}


为什么崩溃
...全文
82 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
werben 2010-07-28
  • 打赏
  • 举报
回复
我来学习!
chong_boy 2010-07-28
  • 打赏
  • 举报
回复
调试一下就知道了。如无意外,str值在strcpy()前一直为NULL。GetMemory()是值传递,不是引用~~
MFC_ipsu 2010-07-28
  • 打赏
  • 举报
回复
str没有分配空间。
GetMemory传递的只是str的副本

LZ 这样改
#include <stdio.h>
#include <malloc.h>
#include <string.h>

void GetMemory(char* &p)
{
p=(char*)malloc(100);
}

void main()
{
char *str = NULL;
GetMemory(str);
strcpy(str,"hello world!");
printf(str);
}

这不是鸭头 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yxqyrh 的回复:]

str没有分配空间。
GetMemory传递的只是str的副本
[/Quote]
void GetMemory(char& *p)
{
p=(char *)malloc(100);
}
yxqyrh 2010-07-28
  • 打赏
  • 举报
回复
p和str指向同一地址,但他们本身的值并没关系
你在GetMemory中位P重新分配空间之后,它和str已经没有任何关系,也没有返回给str
yxqyrh 2010-07-28
  • 打赏
  • 举报
回复
str没有分配空间。
GetMemory传递的只是str的副本
blackmailer 2010-07-28
  • 打赏
  • 举报
回复
其实大家都已经说清楚了,根源在于函数调用的值传递。
如果支持地址传递,函数调用就用引用。
反之,采用返回已申请空间地址的方法
elegant87 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wyd2608 的回复:]

引用 8 楼 elegant87 的回复:
C/C++ code

#include <stdio.h>
#include <malloc.h>
#include <string.h>

char* GetMemory(char *p) //用返回值可以实现拷贝
{
p=(char *)malloc(100);
return p;
}

void main()
{
……
[/Quote]
void GetMemory(char* &p)
{
p=(char*)malloc(100);
}
这是在简单值拷贝,只在函数中起作用的,调用完str仍然不会改变的,是原来的NULL
而函数的返回值相当于引用,调用后就改变str的值了
太乙 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wyd2608 的回复:]
引用 8 楼 elegant87 的回复:
C/C++ code

#include <stdio.h>
#include <malloc.h>
#include <string.h>

char* GetMemory(char *p) //用返回值可以实现拷贝
{
p=(char *)malloc(100);
return p;
}

void main()
{
c……
[/Quote]

在main中,str指向NULL
你传递给GetMemory函数的实际上是str指针的副本,暂且叫str_tmp吧。
实际上相当于:
char* str_tmp = str;
void GetMemory()
{
str_tmp = malloc(100);
}
现在,你在GetMemory函数中给str_tmp赋值了,但是没有对str赋值。。。

相当于两个指针(str、str_tmp),str指向了NULL,但是str_tmp指向了分配内存。。。
太乙 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wyd2608 的回复:]
引用 4 楼 mfc_ipsu 的回复:
str没有分配空间。
GetMemory传递的只是str的副本

LZ 这样改
#include <stdio.h>
#include <malloc.h>
#include <string.h>

void GetMemory(char* &amp;p)
{
p=(char*)malloc(100);
}

void ……
[/Quote]传递引用是c++里的语法,c里不支持,lz可以做如下修改:


#include <stdio.h>
#include <malloc.h>
#include <string.h>

char* GetMemory()
{
return (char *)malloc(100);
}

void main()
{
char *str=NULL;
str = GetMemory();
strcpy(str,"hello world!");
printf(str);
free(str);
}



WYD2608 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 elegant87 的回复:]
C/C++ code

#include <stdio.h>
#include <malloc.h>
#include <string.h>

char* GetMemory(char *p) //用返回值可以实现拷贝
{
p=(char *)malloc(100);
return p;
}

void main()
{
char *s……
[/Quote]能细说下 为什么原来是错的吗?
elegant87 2010-07-28
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>
#include <string.h>

char* GetMemory(char *p) //用返回值可以实现拷贝
{
p=(char *)malloc(100);
return p;
}

void main()
{
char *str=NULL;
str=GetMemory(str);
strcpy(str,"hello world!");
printf(str);
}
WYD2608 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mfc_ipsu 的回复:]
str没有分配空间。
GetMemory传递的只是str的副本

LZ 这样改
#include <stdio.h>
#include <malloc.h>
#include <string.h>

void GetMemory(char* &p)
{
p=(char*)malloc(100);
}

void main()
{
char *str = NUL……
[/Quote]c.c
C:\Program Files\Microsoft Visual Studio\MyProjects\niubaniub\c.c(5) : error C2143: syntax error : missing ')' before '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\niubaniub\c.c(5) : error C2143: syntax error : missing '{' before '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\niubaniub\c.c(5) : error C2059: syntax error : '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\niubaniub\c.c(5) : error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\niubaniub\c.c(13) : warning C4013: 'GetMemory' undefined; assuming extern returning int
Error executing cl.exe.

c.obj - 4 error(s), 1 warning(s)

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧