Redian新闻
>
dealsea上那两个hp显示器怎么样,打游戏行么?
avatar
dealsea上那两个hp显示器怎么样,打游戏行么?# Hardware - 计算机硬件
h*m
1
下面这个程序为啥会segmentation fault呢?
int main(void)
{
char *p1, *p2;
*p1 = 'a';
*p2 = 'b';
printf("%c %c\n", *p1, *p2);
return 0;
}
如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。
avatar
x*i
2
《忠犬八公的故事》改编自1935年发生在日本的真实故事,发生在1924年,秋田犬八公
被它的主人上野秀三郎带到东京,上野秀三郎是东京大学农业系的教授。每天早上,八
公都在家门口目送着上野秀三郎出门上班,然后傍晚时分便到附近的涩谷火车站迎接他
下班回家。这样的幸福生活一直持续到1925年,一天晚上,上野秀三郎并没有如常般回
到家中,他在大学里突然中风,抢救无效。他死了,再也没有回到那个火车站,可是八
公依然忠实地等着他。直到死去。
这个是以前日本拍的。
avatar
B*n
3
HP Pavilion 23xi $90
HP Pavilion 23xw $100
哪个好点?
avatar
c*p
4
p1和p2浮空,指向未定义的地址空间。
对未定义的地址空间进行读写操作会导致未定义的行为,包括段错。

【在 h********m 的大作中提到】
: 下面这个程序为啥会segmentation fault呢?
: int main(void)
: {
: char *p1, *p2;
: *p1 = 'a';
: *p2 = 'b';
: printf("%c %c\n", *p1, *p2);
: return 0;
: }
: 如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。

avatar
x*i
5
这个是后来美国翻拍的,同样爱狗的理查基尔主演。
avatar
x*4
6
一般吧,xi那个是镜面屏,有dvi
价格够便宜了

【在 B****n 的大作中提到】
: HP Pavilion 23xi $90
: HP Pavilion 23xw $100
: 哪个好点?

avatar
m*l
7
这个看printf()怎么定义
显然, 他们不要你放星星

【在 h********m 的大作中提到】
: 下面这个程序为啥会segmentation fault呢?
: int main(void)
: {
: char *p1, *p2;
: *p1 = 'a';
: *p2 = 'b';
: printf("%c %c\n", *p1, *p2);
: return 0;
: }
: 如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。

avatar
h*d
8
看过美国拍的,很感人。十年啊,他老婆回去看到的时候都呆住了,竟然也就那么离开
avatar
M*y
9
HP的电子消费品,让人不放心啊。一提HP,俺就想起俺
1000多刀的高档HP本子,一年多点,显示屏完了。CPU
占用一高,直上100F。现在只能外接显示器上上网。

【在 B****n 的大作中提到】
: HP Pavilion 23xi $90
: HP Pavilion 23xw $100
: 哪个好点?

avatar
h*m
10
不太明白,*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;
}

【在 c****p 的大作中提到】
: p1和p2浮空,指向未定义的地址空间。
: 对未定义的地址空间进行读写操作会导致未定义的行为,包括段错。

avatar
x*i
11
唉, 是呀

【在 h**********d 的大作中提到】
: 看过美国拍的,很感人。十年啊,他老婆回去看到的时候都呆住了,竟然也就那么离开
: 了

avatar
d*f
12
不是镜面

【在 x******4 的大作中提到】
: 一般吧,xi那个是镜面屏,有dvi
: 价格够便宜了

avatar
l*g
13
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

【在 h********m 的大作中提到】
: 不太明白,*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;

avatar
t*e
14
嗯,好感人的!!
avatar
f*d
15
同样的血泪史啊

【在 M********y 的大作中提到】
: HP的电子消费品,让人不放心啊。一提HP,俺就想起俺
: 1000多刀的高档HP本子,一年多点,显示屏完了。CPU
: 占用一高,直上100F。现在只能外接显示器上上网。

avatar
b*n
16
char *p1 这种定义,编译器会默认给 p1 赋值为 null,
然后你 *null 当然会出现 segment fault

【在 h********m 的大作中提到】
: 下面这个程序为啥会segmentation fault呢?
: int main(void)
: {
: char *p1, *p2;
: *p1 = 'a';
: *p2 = 'b';
: printf("%c %c\n", *p1, *p2);
: return 0;
: }
: 如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。

avatar
Z*i
17
看过美国版,不知道哪里可以看到日本版
哭的稀里哗啦的
avatar
n*l
18
一样啊,HP不能碰

【在 M********y 的大作中提到】
: HP的电子消费品,让人不放心啊。一提HP,俺就想起俺
: 1000多刀的高档HP本子,一年多点,显示屏完了。CPU
: 占用一高,直上100F。现在只能外接显示器上上网。

avatar
f*r
19
加一句
p1 = new char;

【在 h********m 的大作中提到】
: 不太明白,*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;

avatar
x*i
20
PPSTREAM 上面有。

【在 Z**i 的大作中提到】
: 看过美国版,不知道哪里可以看到日本版
: 哭的稀里哗啦的

avatar
l*g
21
that's cpp not c leh.

【在 f*****r 的大作中提到】
: 加一句
: p1 = new char;

avatar
x*u
22
japanese version is more sad :(

【在 Z**i 的大作中提到】
: 看过美国版,不知道哪里可以看到日本版
: 哭的稀里哗啦的

avatar
i*e
23
不会。
C 或者 C++ 定义 pointer 是没有默认值的,pointer 的值就是 garbage
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 b******n 的大作中提到】
: char *p1 这种定义,编译器会默认给 p1 赋值为 null,
: 然后你 *null 当然会出现 segment fault

avatar
Z*i
24
找到了
谢谢

【在 x******i 的大作中提到】
: PPSTREAM 上面有。
avatar
i*e
25
p1 一直没有被赋值。
你赋的值是 *p1,也就是 p1 指向的地方。
但由于 p1 里的值没有被初始化,所以 p1 可以指向任意地方,这当然会造成
segmentation fault。
没有 segmentation fault 会更加恐怖,这意味着你程序里的 data 可能暗地里被修改
了!
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 h********m 的大作中提到】
: 不太明白,*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;

avatar
Z*i
26
就当锻炼一下泪腺。。。。

【在 x********u 的大作中提到】
: japanese version is more sad :(
avatar
r*y
27
should initialize p1, p2 firstly

【在 h********m 的大作中提到】
: 下面这个程序为啥会segmentation fault呢?
: int main(void)
: {
: char *p1, *p2;
: *p1 = 'a';
: *p2 = 'b';
: printf("%c %c\n", *p1, *p2);
: return 0;
: }
: 如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。

avatar
b*n
28
c c++ 中的全局指针应该是默认 null 的,
c++ 种类成员应该也是默认 null 的,
至于临时变量应该是和编译器相关吧

【在 i**********e 的大作中提到】
: 不会。
: C 或者 C++ 定义 pointer 是没有默认值的,pointer 的值就是 garbage
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

avatar
c*2
29
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/

【在 h********m 的大作中提到】
: 下面这个程序为啥会segmentation fault呢?
: int main(void)
: {
: char *p1, *p2;
: *p1 = 'a';
: *p2 = 'b';
: printf("%c %c\n", *p1, *p2);
: return 0;
: }
: 如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。

avatar
h*m
30
你说的对,我试了一下 if(p1== NULL)也不对,所以p1应该不是NULL,而是garbage。
最前面malloc一下就好了。

【在 i**********e 的大作中提到】
: 不会。
: C 或者 C++ 定义 pointer 是没有默认值的,pointer 的值就是 garbage
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

avatar
p*x
31
p1 and p2是指针 , 就是表示的是地址
地址在没有赋值前, 利用该地址访问内存空间 会发生随机错误 因为你不知道该地址
到底指向哪个内存单元
所以在使用某个地址 即指针之前 必须对它赋值(赋予有效地址)
然后才能利用*p1 去访问p1指向的内存单元进行读写操作

【在 h********m 的大作中提到】
: 不太明白,*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;

avatar
p*x
32

这个是因为全局变量放在 程序地址空间的initdata区
在程序被运行, 进行初始化的时候 设置0 利于减小开销
临时变量(local variables )如果在stack frame里面, 一般不进行初始化 如gcc
就不对它进行初始化
如果进行初始化 会增加开销

【在 b******n 的大作中提到】
: c c++ 中的全局指针应该是默认 null 的,
: c++ 种类成员应该也是默认 null 的,
: 至于临时变量应该是和编译器相关吧

avatar
i*e
33
Yes, you are right.
This include static variables.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

gcc

【在 p******x 的大作中提到】
:
: 这个是因为全局变量放在 程序地址空间的initdata区
: 在程序被运行, 进行初始化的时候 设置0 利于减小开销
: 临时变量(local variables )如果在stack frame里面, 一般不进行初始化 如gcc
: 就不对它进行初始化
: 如果进行初始化 会增加开销

avatar
m*l
34
牛人。
so, where do static variables reside? which segment in the process?

【在 i**********e 的大作中提到】
: Yes, you are right.
: This include static variables.
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com
:
: gcc

avatar
i*e
35
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

【在 m********l 的大作中提到】
: 牛人。
: so, where do static variables reside? which segment in the process?

avatar
c*p
36
现在data segment还分read only和writeable么。。。
我学汇编的时候data segment里的东西全是可读写的。。
我上学期给学生带32位MIPS汇编的时候,见过把只读变量放在text segment的样例。

the
data

【在 i**********e 的大作中提到】
: 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

avatar
m*l
37
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

【在 i**********e 的大作中提到】
: 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

avatar
i*e
38
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

【在 m********l 的大作中提到】
: 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";

avatar
c*p
39
我猜cpp的object的底层实现类似于一个C的结构体。
struct{
DTYPE1 ...
DTYPE2 ...
...
FTYPE1 (*f)(...);
FTYPE2 (*f)(...);
...
} ...;
这样运行ob的成员函数的时候,只是调用了成员函数的入口地址,
而函数的参数还是该ob的成员数据。
这样就不用拷code了。

【在 m********l 的大作中提到】
: 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";

avatar
p*x
40
Right.

【在 i**********e 的大作中提到】
: 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

avatar
m*l
41
啊。。。谢谢
我不懂的就是这个。
我还以为运行成员函数时要把ob的指针传进去, 不然函数就无法operate on the data.

【在 c****p 的大作中提到】
: 我猜cpp的object的底层实现类似于一个C的结构体。
: struct{
: DTYPE1 ...
: DTYPE2 ...
: ...
: FTYPE1 (*f)(...);
: FTYPE2 (*f)(...);
: ...
: } ...;
: 这样运行ob的成员函数的时候,只是调用了成员函数的入口地址,

avatar
L*1
42

因为指针没有指向真正的地址空间。

【在 h********m 的大作中提到】
: 下面这个程序为啥会segmentation fault呢?
: int main(void)
: {
: char *p1, *p2;
: *p1 = 'a';
: *p2 = 'b';
: printf("%c %c\n", *p1, *p2);
: return 0;
: }
: 如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。