由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - [合集] 问个C的基本问题
相关主题
请教一个入门级的C的指针问题find i < j < k 使得 A[i] < A[j] < A[k]
想成为嵌入式程序员应知道的0x10个基本问题 zz发面经攒RP
[合集] 想成为嵌入式程序员应知道的0x10个基本问题 zz请教狗狗题:复制带随机指针的链表
一道老题面试问了个“master c++”的这个问题,答不出来,直接给枪毙了 (转载)
电面又挂了问个java的问题
问一个链表的问题问个c++的问题
微软onsite有behaviral 问题吗借人气问个C递归函数的问题
链表中每三个数逆转的题?[合集] 说说老印的面试
相关话题的讨论汇总
话题: 问个话题: 基本
进入JobHunting版参与讨论
1 (共1页)
S**I
发帖数: 15689
1
☆─────────────────────────────────────☆
honeydream (pretty) 于 (Fri May 6 00:30:21 2011, 美东) 提到:
下面这个程序为啥会segmentation fault呢?
int main(void)
{
char *p1, *p2;
*p1 = 'a';
*p2 = 'b';
printf("%c %c\n", *p1, *p2);
return 0;
}
如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。
☆─────────────────────────────────────☆
chenpp (chenpp) 于 (Fri May 6 00:31:43 2011, 美东) 提到:
p1和p2浮空,指向未定义的地址空间。
对未定义的地址空间进行读写操作会导致未定义的行为,包括段错。

☆─────────────────────────────────────☆
mercuriusl (Mercurius) 于 (Fri May 6 00:40:06 2011, 美东) 提到:
这个看printf()怎么定义
显然, 他们不要你放星星
☆─────────────────────────────────────☆
honeydream (pretty) 于 (Fri May 6 00:41:22 2011, 美东) 提到:
不太明白,*p1, *p2我不是赋值了么?
如果不能这么直接print,那我该怎么输出*p1和*p2的值呢?分别赋给p3,p4?
我试了一下,下面这个程序还是segmentation fault呀?
int main(void)
{
char *p1, *p2, p3, p4;
*p1 = 'c';
*p2 = 'd';
p3 = *p1;
p4 = *p2;
printf("%c %c\n", p3, p4);
return 0;
}
☆─────────────────────────────────────☆
leong (piggypiggy) 于 (Fri May 6 00:44:31 2011, 美东) 提到:
p1 p2 are pointers char*, they themselves do not have values but nulls. you
need to have p1, p2 malloc'ed to fix the problem.
or p3 = 'c'; p1 = &p3; so that p1 takes the address of p3
☆─────────────────────────────────────☆
bethoven (千金散尽还复来) 于 (Fri May 6 00:45:27 2011, 美东) 提到:
char *p1 这种定义,编译器会默认给 p1 赋值为 null,
然后你 *null 当然会出现 segment fault
☆─────────────────────────────────────☆
fiddler (Motion and Emotion) 于 (Fri May 6 00:45:31 2011, 美东) 提到:
加一句
p1 = new char;
☆─────────────────────────────────────☆
leong (piggypiggy) 于 (Fri May 6 00:48:41 2011, 美东) 提到:
that's cpp not c leh.
☆─────────────────────────────────────☆
ihasleetcode (1337coder) 于 (Fri May 6 00:55:32 2011, 美东) 提到:
不会。
C 或者 C++ 定义 pointer 是没有默认值的,pointer 的值就是 garbage
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
☆─────────────────────────────────────☆
ihasleetcode (1337coder) 于 (Fri May 6 00:58:13 2011, 美东) 提到:
p1 一直没有被赋值。
你赋的值是 *p1,也就是 p1 指向的地方。
但由于 p1 里的值没有被初始化,所以 p1 可以指向任意地方,这当然会造成
segmentation fault。
没有 segmentation fault 会更加恐怖,这意味着你程序里的 data 可能暗地里被修改
了!
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
☆─────────────────────────────────────☆
romancity (山顶一枝草) 于 (Fri May 6 00:59:21 2011, 美东) 提到:
should initialize p1, p2 firstly
☆─────────────────────────────────────☆
bethoven (千金散尽还复来) 于 (Fri May 6 01:04:30 2011, 美东) 提到:
c c++ 中的全局指针应该是默认 null 的,
c++ 种类成员应该也是默认 null 的,
至于临时变量应该是和编译器相关吧
☆─────────────────────────────────────☆
ch222 (ch) 于 (Fri May 6 01:08:44 2011, 美东) 提到:
this is the basic pointer issue.
another way to understand:
char *p1, *p2;
only declares two addresses of char.
read "Pointers and Memory" at
http://cslibrary.stanford.edu/102/
☆─────────────────────────────────────☆
honeydream (pretty) 于 (Fri May 6 01:08:56 2011, 美东) 提到:
你说的对,我试了一下 if(p1== NULL)也不对,所以p1应该不是NULL,而是garbage。
最前面malloc一下就好了。
☆─────────────────────────────────────☆
pinealex (无名) 于 (Fri May 6 01:33:03 2011, 美东) 提到:
p1 and p2是指针 , 就是表示的是地址
地址在没有赋值前, 利用该地址访问内存空间 会发生随机错误 因为你不知道该地址
到底指向哪个内存单元
所以在使用某个地址 即指针之前 必须对它赋值(赋予有效地址)
然后才能利用*p1 去访问p1指向的内存单元进行读写操作
☆─────────────────────────────────────☆
pinealex (无名) 于 (Fri May 6 01:38:49 2011, 美东) 提到:
这个是因为全局变量放在 程序地址空间的initdata区
在程序被运行, 进行初始化的时候 设置0 利于减小开销
临时变量(local variables )如果在stack frame里面, 一般不进行初始化 如gcc
就不对它进行初始化
如果进行初始化 会增加开销
☆─────────────────────────────────────☆
ihasleetcode (1337coder) 于 (Fri May 6 02:05:21 2011, 美东) 提到:
Yes, you are right.
This include static variables.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
gcc
☆─────────────────────────────────────☆
mercuriusl (Mercurius) 于 (Fri May 6 02:08:56 2011, 美东) 提到:
牛人。
so, where do static variables reside? which segment in the process?
☆─────────────────────────────────────☆
ihasleetcode (1337coder) 于 (Fri May 6 02:18:43 2011, 美东) 提到:
same as global variable, static variables reside in the data segment for the
entire lifetime of the process.
this is true for C-style literal string too, e.g. char *s = "hello world";
Even though it seemed that it's declared locally, it is stored in the data
segment and thus is available throughout the lifetime of the process. Also
note that it is stored as read-only... That's why most people don't
understand why something like s[2] = 'c' and get segmentation fault.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
☆─────────────────────────────────────☆
chenpp (chenpp) 于 (Fri May 6 02:32:15 2011, 美东) 提到:
现在data segment还分read only和writeable么。。。
我学汇编的时候data segment里的东西全是可读写的。。
我上学期给学生带32位MIPS汇编的时候,见过把只读变量放在text segment的样例。
the
data
☆─────────────────────────────────────☆
mercuriusl (Mercurius) 于 (Fri May 6 02:32:43 2011, 美东) 提到:
cool. i was confused because i thought you said global variables are on
the stack.
Okay, got a side queestion on cpp:
when an new object is created out of a class, do the class's code get
copied?
I know it's a No, but how do they share the code while retaining
different data?
for the
world";
data
Also
☆─────────────────────────────────────☆
ihasleetcode (1337coder) 于 (Fri May 6 02:36:23 2011, 美东) 提到:
Each object has its own data,but all member functions share the same code,
which has the function pointer points to the same location in the memory.
Right?
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
☆─────────────────────────────────────☆
chenpp (chenpp) 于 (Fri May 6 02:38:59 2011, 美东) 提到:
我猜cpp的object的底层实现类似于一个C的结构体。
struct{
DTYPE1 ...
DTYPE2 ...
...
FTYPE1 (*f)(...);
FTYPE2 (*f)(...);
...
} ...;
这样运行ob的成员函数的时候,只是调用了成员函数的入口地址,
而函数的参数还是该ob的成员数据。
这样就不用拷code了。
☆─────────────────────────────────────☆
pinealex (无名) 于 (Fri May 6 02:43:11 2011, 美东) 提到:
Right.
☆─────────────────────────────────────☆
mercuriusl (Mercurius) 于 (Fri May 6 02:47:47 2011, 美东) 提到:
啊。。。谢谢
我不懂的就是这个。
我还以为运行成员函数时要把ob的指针传进去, 不然函数就无法operate on the data.
☆─────────────────────────────────────☆
LackofOffer1 (刘川枫) 于 (Fri May 6 09:24:02 2011, 美东) 提到:
因为指针没有指向真正的地址空间。
1 (共1页)
进入JobHunting版参与讨论
相关主题
[合集] 说说老印的面试电面又挂了
[合集] 读书还是工作?听听版上各位的意见问一个链表的问题
也问个OPT缴税的问题微软onsite有behaviral 问题吗
问个关于简历地址的问题链表中每三个数逆转的题?
请教一个入门级的C的指针问题find i < j < k 使得 A[i] < A[j] < A[k]
想成为嵌入式程序员应知道的0x10个基本问题 zz发面经攒RP
[合集] 想成为嵌入式程序员应知道的0x10个基本问题 zz请教狗狗题:复制带随机指针的链表
一道老题面试问了个“master c++”的这个问题,答不出来,直接给枪毙了 (转载)
相关话题的讨论汇总
话题: 问个话题: 基本