C风格的铸造 [英] C style casting

查看:60
本文介绍了C风格的铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否有人知道以下代码有什么问题,我被告知它将会编译并运行它b * b某些值会崩溃。


假设变量已经启动。


char * c;

long * lg;


c =(char *)lg;

lg =(long *)c;


谢谢,

Ramesh

解决方案

Ramesh Tharma< ra ******** @ yahoo.com>写道:

有没有人知道下面的代码有什么问题,我被告知它会编译运行但是它会因某些值而崩溃。
假设变量已经启动。


怎么样?

char * c;
long * lg;
c =(char *)lg;
lg =(long *)c;




没有什么大不了的,除了在最后一行

如果'c''没有对齐类型'long'',则调用UB并且

可能发生任何事情。一切都取决于'c'指向的地方,

以及实施的对齐要求。


-

Stan Tobias

mailx`echo si *** @FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`




S.Tobias写道:

Ramesh Tharma< ra ******** @ yahoo.com>写道:

是否有人知道下面的代码有什么问题,我被告知它会编译运行但是会因某些值而崩溃。 / blockquote>

假设变量已经启动。



如何?

char * c ;
long * lg;


c =(char *)lg;
lg =(long *)c;



没有什么大不了的,除了在最后一行
如果`c''没有对齐'long'',那么调用UB和
任何东西可能会发生。一切都取决于'c'指向的位置,
以及实现的对齐要求。




此外,取消引用c对于具有不同字节顺序的实现可能会给出不同的结果




Ramesh Tharma写道:

有没有人知道下面的代码有什么问题,我被告知
它会编译运行但是它会因某些值而崩溃。

假设变量已经开始了。

char * c;
long * lg;

c =(char *)lg;
lg =(long *)c ;




我记得有些系统允许char指针指向任何

类型的地址但是将长指针限制为偶数地址。

(这些Amiga不是很好吗?)所以如果你强行指向

一个奇怪的地址会导致异常。


最好的问候

Steffen


Hi,

Is any one knows what''s wrong with the following code, I was told that it
will compile and run but it will crash for some values.

Assume that variables are initilized.

char* c;
long* lg;

c = (char*) lg;
lg = (long*) c;

Thanks,
Ramesh

解决方案

Ramesh Tharma <ra********@yahoo.com> wrote:

Is any one knows what''s wrong with the following code, I was told that it
will compile and run but it will crash for some values. Assume that variables are initilized.
How?
char* c;
long* lg; c = (char*) lg;
lg = (long*) c;



There''s nothing generally wrong, except that in the last line
if `c'' is not aligned for type `long'', then UB is invoked and
anything may happen. Everything depends on where `c'' points to,
and on the implementation''s alignment requirements.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`




S.Tobias wrote:

Ramesh Tharma <ra********@yahoo.com> wrote:

Is any one knows what''s wrong with the following code, I was told that it
will compile and run but it will crash for some values.


Assume that variables are initilized.



How?

char* c;
long* lg;


c = (char*) lg;
lg = (long*) c;



There''s nothing generally wrong, except that in the last line
if `c'' is not aligned for type `long'', then UB is invoked and
anything may happen. Everything depends on where `c'' points to,
and on the implementation''s alignment requirements.



Also, dereferencing "c" may give different results on implementations
that have different endianness.


Ramesh Tharma wrote:

Is any one knows what''s wrong with the following code, I was told
that it will compile and run but it will crash for some values.

Assume that variables are initilized.

char* c;
long* lg;

c = (char*) lg;
lg = (long*) c;



I remember there are systems which allow char pointers to point to any
kind of address but restrict long pointers to only even addresses.
(Wasn''t good old Amiga one of these?) So if you force a long pointer to
an odd address it will lead to an exception.

Best regards
Steffen


这篇关于C风格的铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆