我们可以输入一个INTEGER AS(VOID *)X ..(就像我可以返回一个值(void *)x) [英] CAN WE TYPE CAST AN INTEGER AS (VOID *)X..(Like can I return a value (void *)x)

查看:49
本文介绍了我们可以输入一个INTEGER AS(VOID *)X ..(就像我可以返回一个值(void *)x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在假设我已经在函数内声明了一个整数值,如果函数的返回类型是类型(void *),则现在声明为
int x;

我可以写b $

返回((void *)x)在函数的一边吗?

我在一篇关于多线程的文档中遇到过这个问题C..I我可以发布

确切的代码部分,如果你想要的话,可以正确使用这样的作业。

请澄清我的怀疑。我希望我已经正确地传达了它。

now suppose I have declared an integer value inside a function as
int x;
now if the return type of the function is of type (void *) then can I
write
return((void *)x) in side the function?
I came across this in a document on multithreading in C..I can post the
exact portion of code which works correctly with such an assignment if
you people want.
Please clarify my doubt.I hope I have conveyed it properly.

推荐答案

你好Abhishek,


请不要在ALL UPPERCASE中写句子。这只会导致

的人忽略你的帖子,因为这是垃圾邮件发送者经常使用的风格

。此外,尽量保持你的主题简短,因为有些人可能会使用无法显示太长的新闻阅读器

主题行。你的帖子的好主题可能是什么

喜欢Casting int to void *


Abhishek< ab ****** *******@gmail.com>写道:
Hello Abhishek,

Please do not write sentences in ALL UPPERCASE. This will only cause
people to ignore your posts, since this is a style which is often used
by spammers. Also, try to keep your subject short, since some people
might be using newsreaders which are not able to display too long
subject lines. A good subject for your post might have been something
like "Casting int to void *"

Abhishek <ab*************@gmail.com> wrote:
现在假设我已在函数内声明了一个整数值为int
x;现在如果函数的返回类型是类型(void *)那么我可以在函数的一边写return((void *)x)吗?
now suppose I have declared an integer value inside a function as int
x; now if the return type of the function is of type (void *) then can
I write return((void *)x) in side the function?




是的,这是可能的,但我怀疑这可能导致未定义的

行为。通过将int转换为void指针,您可以获取

x中的数字,并将其转换为地址。你告诉你的编译器`trust

我,我知道我在做什么。虽然这只是一个常规数字,但我确定这个数字是一个有效地址,而且没有特定类型的'b $ b'。这可能会或可能不会在您的平台上运行,因为

不能保证int可能适合指针。我不知道使用整数类型存储指针的任何好理由,

虽然我知道其他代码这是常见的做法(

Linux内核)。也许其他人可以解释为什么这样做。


Ico


-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



Yes, this is possible, but I suspect this might result in undefined
behaviour. By casting an int to a void pointer, you take the number in
x, and convert it to an address. You are telling your compiler `trust
me, I know what I am doing. Although this is just a regular number, I am
sure this number is a valid address of something which doesn''t have a
specific type''. This might or might not work on your platform, since
there is no guarantee that an int might fit into a pointer. I am not
aware of any good reasons for using an integer type to store a pointer,
although I am aware of other code where this is common practice (the
Linux kernel). Maybe somebody else can explain why this is done.

Ico

--
:wq
^X^Cy^K^X^C^C^C^C


Ico写道:
Ico wrote:
你好Abhishek,

请不要用ALL UPPERCASE写句子。这只会导致人们忽略你的帖子,因为这是垃圾邮件发送者经常使用的一种风格。


我必须说,我对所有CAPS的直接反应往往是停止

SHOUTING! ;-)


<剪掉更多好建议>

Abhishek< ab ************* @gmail。 COM>写道:
Hello Abhishek,

Please do not write sentences in ALL UPPERCASE. This will only cause
people to ignore your posts, since this is a style which is often used
by spammers.
I must say, my immediate reaction to ALL CAPS tends to be STOP
SHOUTING! ;-)

<snipped more good advice>
Abhishek <ab*************@gmail.com> wrote:
现在假设我已在函数内声明了一个整数值为int
x;现在如果函数的返回类型是类型(void *)那么
可以
我在函数的一边写回((void *)x)?
now suppose I have declared an integer value inside a function as int
x; now if the return type of the function is of type (void *) then
can
I write return((void *)x) in side the function?



是的,这是可能的,但我怀疑这可能导致未定义的行为。通过将int转换为void指针,您可以获取
x中的数字,并将其转换为地址。你告诉你的编译器'信任
我,我知道我在做什么。虽然这只是一个常规数字,但我相信这个数字是一个有效的地址,它没有特定的类型''。这可能会或可能不会在您的平台上运行,因为
无法保证int可能适合指针。我不知道使用整数类型存储指针的任何好理由,虽然我知道其他代码这是常见的实践(Linux内核)。也许其他人可以解释为什么这个
已经完成。



Yes, this is possible, but I suspect this might result in undefined
behaviour. By casting an int to a void pointer, you take the number in
x, and convert it to an address. You are telling your compiler `trust
me, I know what I am doing. Although this is just a regular number, I
am sure this number is a valid address of something which doesn''t have
a specific type''. This might or might not work on your platform, since
there is no guarantee that an int might fit into a pointer. I am not
aware of any good reasons for using an integer type to store a
pointer, although I am aware of other code where this is common
practice (the Linux kernel). Maybe somebody else can explain why this
is done.




这肯定是未定义的行为,如果不是更糟。一旦从函数返回

,就C标准而言,所有对象都是本地函数_cease_to_exist_(如clc中其他地方提到的那样,

标准对堆栈和类似的现实世界概念一无所知。)

在函数之外引用它们应该真的让你的头脑旋转
spin。 ;-)


当然,允许实现快捷方式不会因为物理破坏此类对象而烦恼,因此您可能无法获得

非法访问或任何适当的错误。但是,即使改变了优化级别,更不用说整个编译器可能会使你的(OP')

代码以完全不同的方式运行。


干杯


弗拉基米尔


干杯


弗拉基米尔

-

我们正在提高我们的标准......所以提升你的标准。

- Pat Paul为总统, 1988年。



This is certainly undefined behaviour, if not worse. Once you return
from the function, as far a C Standard is concerned, all objects local
to that function _cease_to_exist_ (as was mentioned elsewhere in c.l.c,
Standard knows nothing about stacks and similar real world concepts).
Referring to them outside the function should really make your head
spin. ;-)

Of course, an implementation is allowed to take shortcuts by not
bothering to physically destroy such objects, and so you may not get
illegal access or whatever error is appropriate. However, even changing
optimisation level, let alone the whole compiler may make your (OP''s)
code behave in a totally different way.

Cheers

Vladimir

Cheers

Vladimir

--
"We are upping our standards ... so up yours."
-- Pat Paulsen for President, 1988.


Vladimir S. Oka< no **** @ btopenworld.com>写道:
Vladimir S. Oka <no****@btopenworld.com> wrote:
Ico写道:
Ico wrote:
你好AbhishV00ek,

Abhishek< ab ********** ***@gmail.com>写道:
Hello AbhishV00ek,

Abhishek <ab*************@gmail.com> wrote:
现在假设我已在函数内声明了一个整数值为int
x;现在如果函数的返回类型是类型(void *)那么
可以
我在函数的一边写回((void *)x)?
now suppose I have declared an integer value inside a function as int
x; now if the return type of the function is of type (void *) then
can
I write return((void *)x) in side the function?



是的,这是可能的,但我怀疑这可能导致未定义的行为。通过将int转换为void指针,您可以获取
x中的数字,并将其转换为地址。你告诉你的编译器'信任
我,我知道我在做什么。虽然这只是一个常规数字,但我相信这个数字是一个有效的地址,它没有特定的类型''。这可能会或可能不会在您的平台上运行,因为
无法保证int可能适合指针。我不知道使用整数类型存储指针的任何好理由,虽然我知道其他代码这是常见的实践(Linux内核)。也许其他人可以解释为什么这个
已经完成。



Yes, this is possible, but I suspect this might result in undefined
behaviour. By casting an int to a void pointer, you take the number in
x, and convert it to an address. You are telling your compiler `trust
me, I know what I am doing. Although this is just a regular number, I
am sure this number is a valid address of something which doesn''t have
a specific type''. This might or might not work on your platform, since
there is no guarantee that an int might fit into a pointer. I am not
aware of any good reasons for using an integer type to store a
pointer, although I am aware of other code where this is common
practice (the Linux kernel). Maybe somebody else can explain why this
is done.



这肯定是未定义的行为,如果不是更糟。一旦从函数返回
,就C标准而言,该函数的所有对象都是_cease_to_exist_(如clc中其他地方所述,
标准对堆栈和类似的实际情况一无所知)世界概念)。
在功能之外引用它们应该真的让你的头旋转。 ;-)

当然,允许实现快捷方式,而不是打扰物理破坏这些对象,因此您可能无法获得非法访问或任何错误适当。但是,即使更改优化级别,更不用说整个编译器可能会使您的(OP')代码以完全不同的方式运行。



This is certainly undefined behaviour, if not worse. Once you return
from the function, as far a C Standard is concerned, all objects local
to that function _cease_to_exist_ (as was mentioned elsewhere in c.l.c,
Standard knows nothing about stacks and similar real world concepts).
Referring to them outside the function should really make your head
spin. ;-)

Of course, an implementation is allowed to take shortcuts by not
bothering to physically destroy such objects, and so you may not get
illegal access or whatever error is appropriate. However, even changing
optimisation level, let alone the whole compiler may make your (OP''s)
code behave in a totally different way.




弗拉基米尔,我觉得你误解了OP问题中的细节:到目前为止我没有回复x的地址,但*值*

of x,转换为void *:



Vladimir, I think you misread the details in the OP''s question: as far
as I onderstood, he is not returning the address of x, but the *value*
of x, casted to void * :

return((void *)x)
return((void *)x)




除了演员之外,这和返回x intself一样有效。


Ico


-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



Apart from the cast, this is just as valid as returning x intself.

Ico

--
:wq
^X^Cy^K^X^C^C^C^C


这篇关于我们可以输入一个INTEGER AS(VOID *)X ..(就像我可以返回一个值(void *)x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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