由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C puzzle 一日一题
相关主题
这里的牛人很多,都读过标准吗c vs c++
C arrayC语言重复定义问题
怎么得到char *分配空间的大小?C++ func overload question
array allocation in c问两个C++语法问题
问一个volatile和memcpy一起用的问题C语言的变量都一定要放在stack上吗?
C++ pointer to function is buggyask a simple question about int pointer.
谁来解释一下这个是compiler问题吗?what's wrong with this C++ code?
[合集] reinterpret_cast a 4 byte unsigned char to integer请教C++里面这个表达
相关话题的讨论汇总
话题: int话题: malloc话题: implicit话题: compiler话题: byte
进入Programming版参与讨论
1 (共1页)
F********g
发帖数: 475
1
http://www.gowrikumar.com/c/
The following C program segfaults of IA-64, but works fine on IA-32.
int main()
{
int* p;
p = (int*)malloc(sizeof(int));
*p = 10;
return 0;
}
Why does it happen so?
是不是CONSTANT LITERAL被默认为8BYTE,COMPILER分配的指针指向4BYTE INT导致?
求点解!
p***o
发帖数: 1252
2
If your compiler is not that buggy, then the malloc implementation
is not standard conforming.
The malloc() and calloc() functions return a pointer to the
allocated memory that is *suitably aligned* for *any kind* of
variable.

【在 F********g 的大作中提到】
: http://www.gowrikumar.com/c/
: The following C program segfaults of IA-64, but works fine on IA-32.
: int main()
: {
: int* p;
: p = (int*)malloc(sizeof(int));
: *p = 10;
: return 0;
: }
: Why does it happen so?

F********g
发帖数: 475
3
Sounds like a bus error.
Still confused why it's a problem for 64 bit architecture.

【在 p***o 的大作中提到】
: If your compiler is not that buggy, then the malloc implementation
: is not standard conforming.
: The malloc() and calloc() functions return a pointer to the
: allocated memory that is *suitably aligned* for *any kind* of
: variable.

X****r
发帖数: 3557
4
you need to #include before using malloc, otherwise the implicit
declaration of this function may not match its implementation.
there is no point wasting time in puzzles like this.

【在 F********g 的大作中提到】
: http://www.gowrikumar.com/c/
: The following C program segfaults of IA-64, but works fine on IA-32.
: int main()
: {
: int* p;
: p = (int*)malloc(sizeof(int));
: *p = 10;
: return 0;
: }
: Why does it happen so?

w***g
发帖数: 5958
5
牛!

implicit

【在 X****r 的大作中提到】
: you need to #include before using malloc, otherwise the implicit
: declaration of this function may not match its implementation.
: there is no point wasting time in puzzles like this.

L***n
发帖数: 6727
6
说个无关的,点解就点解,求什么点解,不会广东话就别说了

【在 F********g 的大作中提到】
: http://www.gowrikumar.com/c/
: The following C program segfaults of IA-64, but works fine on IA-32.
: int main()
: {
: int* p;
: p = (int*)malloc(sizeof(int));
: *p = 10;
: return 0;
: }
: Why does it happen so?

a****l
发帖数: 8211
7
actually problems like this could be real-world puzzles that costs people
days of work time... And knowing the possible solutions like this is what I call "real programming experience".

implicit

【在 X****r 的大作中提到】
: you need to #include before using malloc, otherwise the implicit
: declaration of this function may not match its implementation.
: there is no point wasting time in puzzles like this.

c****p
发帖数: 6474
8
这个会导致seg fault么?

implicit

【在 X****r 的大作中提到】
: you need to #include before using malloc, otherwise the implicit
: declaration of this function may not match its implementation.
: there is no point wasting time in puzzles like this.

X****r
发帖数: 3557
9
Yes, because a implicitly declared function always returns 'int',
which may or may not be the same size as 'void *', which is the
actual return type of 'malloc'. If they are of different sizes,
say 'int' is 32-bits while 'void *' is 64-bits, the result from
malloc is cut-off by half before used as a pointer, which would
surely result in seg fault.
Note that both C99 and C++ disallow implicit function declarations,
and the compiler is probably going to warn you even if you compile
in C90. Implicit function declarations are relics from K&R. Just
don't do it.

【在 c****p 的大作中提到】
: 这个会导致seg fault么?
:
: implicit

F********g
发帖数: 475
10
Just found out that the default supported standard by gcc is
gnu89....zzzzz
Thank you for you pointer. Have a good weekend.

【在 X****r 的大作中提到】
: Yes, because a implicitly declared function always returns 'int',
: which may or may not be the same size as 'void *', which is the
: actual return type of 'malloc'. If they are of different sizes,
: say 'int' is 32-bits while 'void *' is 64-bits, the result from
: malloc is cut-off by half before used as a pointer, which would
: surely result in seg fault.
: Note that both C99 and C++ disallow implicit function declarations,
: and the compiler is probably going to warn you even if you compile
: in C90. Implicit function declarations are relics from K&R. Just
: don't do it.

相关主题
C++ pointer to function is buggyc vs c++
谁来解释一下这个是compiler问题吗?C语言重复定义问题
[合集] reinterpret_cast a 4 byte unsigned char to integerC++ func overload question
进入Programming版参与讨论
j*a
发帖数: 14423
11
always use -Wall?

【在 F********g 的大作中提到】
: Just found out that the default supported standard by gcc is
: gnu89....zzzzz
: Thank you for you pointer. Have a good weekend.

c****p
发帖数: 6474
12
我在64位平台上跑没问题。
把p打出来也是64位的地址。【 在 Xentar (思考猪) 的大作中提到: 】
N***m
发帖数: 4460
13
相信思考朱德。
套用C++ faq的一句话,
不要说在你的机器上,用你的程序经过编译器编译后能跑,
just don't do it!

【在 c****p 的大作中提到】
: 我在64位平台上跑没问题。
: 把p打出来也是64位的地址。【 在 Xentar (思考猪) 的大作中提到: 】

c****p
发帖数: 6474
14
代码:
int main()
{
int* p;
p = 0;
p = (int*)malloc(sizeof(int));
printf("%dbits: %x\n", sizeof(p), p);
*p = 10;
return 0;
}
结果:
8bits: 1bf3010
这个结果不能说明malloc的返回值是32位的int啊。

【在 N***m 的大作中提到】
: 相信思考朱德。
: 套用C++ faq的一句话,
: 不要说在你的机器上,用你的程序经过编译器编译后能跑,
: just don't do it!

t****t
发帖数: 6806
15
你这显然验证了xentar的说法啊. 一个好好的地址只剩低32位了.
我为什么要浪费时间发这么个贴子.
啊我知道了, 你不会数数!

【在 c****p 的大作中提到】
: 代码:
: int main()
: {
: int* p;
: p = 0;
: p = (int*)malloc(sizeof(int));
: printf("%dbits: %x\n", sizeof(p), p);
: *p = 10;
: return 0;
: }

l******d
发帖数: 530
16
我还真碰到类似问题,忘记include头文件,编译过了,结果就是不对,花了好久时间
才发现。
c****p
发帖数: 6474
17
- -

【在 t****t 的大作中提到】
: 你这显然验证了xentar的说法啊. 一个好好的地址只剩低32位了.
: 我为什么要浪费时间发这么个贴子.
: 啊我知道了, 你不会数数!

k***5
发帖数: 583
18
去了这个网站看了看,很多puzzle属于基本违背了编程规范的东西。当然你可以费力气
去弄清楚,不过容易的答案就是follow编程规范。
L***n
发帖数: 6727
19
这种问题最好的解决方法应该是写个预处理程序检查源代码,不合规范的指出来,
而不是事后犯错误后绞尽脑汁去想可能的原因。

【在 k***5 的大作中提到】
: 去了这个网站看了看,很多puzzle属于基本违背了编程规范的东西。当然你可以费力气
: 去弄清楚,不过容易的答案就是follow编程规范。

F********g
发帖数: 475
20
有助于弄清LANGUAGE SPEC。
C99标准俺是看不下去的ZZ。。

【在 L***n 的大作中提到】
: 这种问题最好的解决方法应该是写个预处理程序检查源代码,不合规范的指出来,
: 而不是事后犯错误后绞尽脑汁去想可能的原因。

1 (共1页)
进入Programming版参与讨论
相关主题
请教C++里面这个表达问一个volatile和memcpy一起用的问题
a simple question about constructorC++ pointer to function is buggy
a c++ question.谁来解释一下这个是compiler问题吗?
A question about class size[合集] reinterpret_cast a 4 byte unsigned char to integer
这里的牛人很多,都读过标准吗c vs c++
C arrayC语言重复定义问题
怎么得到char *分配空间的大小?C++ func overload question
array allocation in c问两个C++语法问题
相关话题的讨论汇总
话题: int话题: malloc话题: implicit话题: compiler话题: byte