从指针类型转换为非指针类型 [英] Casting from pointer types to non-pointer types

查看:113
本文介绍了从指针类型转换为非指针类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方API(用C语言编写,如果那个

定义:


void event_handler(int event_code,unsigned long user_data);


函数接受事件代码和用户数据,但不是
行动或更改用户数据。

在我的应用程序中,我想传递指针作为用户数据。这个

指针指向一个用new分配的数组,比如说如下:


char * array = new char [100];


并按如下方式传递给事件处理程序:


event_handler(1,reinterpret_cast< unsigned long>(array));


我的问题是如果我将

想要在以后重复使用它是否可以安全地转换为无符号长类型?具体会做这样的事情

给我带来麻烦吗?


无效some_function(无符号长指针)

{

char * array = reinterpret_cast< char *>(指针);

/ *用数组做东西* /

delete [] array;

}


在我的机器上,sizeof(char *)和sizeof(unsigned long)都是8,所以

这是一个漂亮的丑陋的解决方案,乍一看它看起来似乎是安全的(至少从溢出开始)。

I am interfacing with a third party API (written in C, if that
matters) that has an "event handler" function with the following
definition:

void event_handler(int event_code, unsigned long user_data);

The function takes an event code along with the user data but does not
act on or change the user data.
In my application, I want to pass a pointer as the user data. This
pointer is to an array allocated with new, say, as follows:

char *array = new char[100];

and passed to the event handler as follows:

event_handler(1, reinterpret_cast<unsigned long>(array));

My question is if it''s safe to cast this to an unsigned long type if I
want to reuse it later? Specifically will doing something like this
cause me any trouble?

void some_function(unsigned long pointer)
{
char *array = reinterpret_cast<char*>(pointer);
/* do stuff with array */
delete [] array;
}

On my machine both sizeof(char*) and sizeof(unsigned long) is 8 so
while this is a pretty ugly solution, at first glance it would seem
safe (at least from overflow).

推荐答案

jo***********@gmail.com 写道:

我正在使用第三方API(用C语言编写,如果那个

定义:


void event_handler(int event_code,unsigned long user_data);


函数接受事件代码和用户数据,但不是
行动或更改用户数据。

在我的应用程序中,我想传递指针作为用户数据。这个

指针指向一个用new分配的数组,比如说如下:


char * array = new char [100];


并按如下方式传递给事件处理程序:


event_handler(1,reinterpret_cast< unsigned long>(array));


我的问题是如果我将

想要在以后重复使用它是否可以安全地转换为无符号长类型?特别会做这样的事情

给我带来麻烦吗?
I am interfacing with a third party API (written in C, if that
matters) that has an "event handler" function with the following
definition:

void event_handler(int event_code, unsigned long user_data);

The function takes an event code along with the user data but does not
act on or change the user data.
In my application, I want to pass a pointer as the user data. This
pointer is to an array allocated with new, say, as follows:

char *array = new char[100];

and passed to the event handler as follows:

event_handler(1, reinterpret_cast<unsigned long>(array));

My question is if it''s safe to cast this to an unsigned long type if I
want to reuse it later? Specifically will doing something like this
cause me any trouble?



这是安全且定义明确的_if_''unsigned long''具有与

相同的大小。重新铸造它。

It is safe and well-defined _if_ ''unsigned long'' has the same size as
the pointer to which you''re casting it.


>

void some_function(unsigned long pointer)

{

char * array = reinterpret_cast< char *>(指针);

/ *用数组做东西* /

delete [] array;

}


在我的机器上,sizeof(char *)和sizeof(unsigned long)都是8,所以

这是一个非常丑陋的解决方案,乍一看它似乎是安全的(至少从溢出)。
>
void some_function(unsigned long pointer)
{
char *array = reinterpret_cast<char*>(pointer);
/* do stuff with array */
delete [] array;
}

On my machine both sizeof(char*) and sizeof(unsigned long) is 8 so
while this is a pretty ugly solution, at first glance it would seem
safe (at least from overflow).



它是。


V

-

请在通过电子邮件回复时删除大写''A'

我没有回复最热门的回复,请不要问

It is.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

jo***********@gmail.com

我正在使用第三方API(用C语言编写,如果那是

很重要)与事件处理程序相关联。具有以下功能

定义:


void event_handler(int event_code,unsigned long user_data);


函数接受事件代码和用户数据,但不是
行动或更改用户数据。

在我的应用程序中,我想传递指针作为用户数据。这个

指针指向一个用new分配的数组,比如说如下:


char * array = new char [100];


并按如下方式传递给事件处理程序:


event_handler(1,reinterpret_cast< unsigned long>(array));


我的问题是如果我将

想要在以后重复使用它是否可以安全地转换为无符号长类型?具体会做这样的事情

给我带来麻烦吗?


无效some_function(无符号长指针)

{

char * array = reinterpret_cast< char *>(指针);

/ *用数组做东西* /

delete [] array;

}


在我的机器上,sizeof(char *)和sizeof(unsigned long)都是8,所以

这是一个漂亮的丑陋的解决方案,乍一看它似乎是安全的(至少从溢出)。
I am interfacing with a third party API (written in C, if that
matters) that has an "event handler" function with the following
definition:

void event_handler(int event_code, unsigned long user_data);

The function takes an event code along with the user data but does not
act on or change the user data.
In my application, I want to pass a pointer as the user data. This
pointer is to an array allocated with new, say, as follows:

char *array = new char[100];

and passed to the event handler as follows:

event_handler(1, reinterpret_cast<unsigned long>(array));

My question is if it''s safe to cast this to an unsigned long type if I
want to reuse it later? Specifically will doing something like this
cause me any trouble?

void some_function(unsigned long pointer)
{
char *array = reinterpret_cast<char*>(pointer);
/* do stuff with array */
delete [] array;
}

On my machine both sizeof(char*) and sizeof(unsigned long) is 8 so
while this is a pretty ugly solution, at first glance it would seem
safe (at least from overflow).



我认为它通常是不安全的,但如果你的无符号长点

确实是8字节,那么它将起作用。仍然,你的代码中某处的断言()

sizeof(unsigned long)== sizeof(char *)

不会受到伤害(它会帮助其他人)在20年的时间跟踪

错误,因为他们使用__ Further将代码移植到机器上,

16字节指针:>)。


HTH,

- J.

I think it would be "generally unsafe", but if your unsigned longs
really are 8 byte, then it will work. Still, an assert() that
sizeof(unsigned long)==sizeof(char*) somewhere in your code
won''t hurt (and it will help others in 20 years'' time track
the bug as they port your code to machines using "__further",
16-byte pointers :>).

HTH,
- J.


应该是安全的。我相信所有当前的编译器,long和指针

都有相同的大小。请谷歌LP64了解更多信息。

It should be safe. I believe in all current compiler, long and pointer
have same size. Please google "LP64" for more information.


这篇关于从指针类型转换为非指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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