简体   繁体   English

将指针转换为int从C中的int函数返回是否安全?

[英]Is it safe to cast a pointer as an int to return from an int function in C?

I'm managing a linked list and I use a int checker(...) function to return 1 if an element is found or 0 otherwise. 我正在管理一个链表,并且使用int checker(...)函数返回1如果找到一个元素),否则返回0 I also have to delete elements, so while I'm checking I would like to get the address of a certain found element if it has to be deleted. 我还必须删除元素,所以在检查时,我想获取某个找到的元素的地址(如果必须删除)。

My idea is to put a "delete mode" if(..) that makes checker return the element's address instead of return 1; 我的想法是放置一个“删除模式” if(..) ,使checker返回元素的地址而不是return 1; and then use it to free the space. 然后使用它释放空间。

Now my question is, am I going toward big problems writing return (int)pointeraddress in the function checker and then recasting it like this outside? 现在我的问题是,我是否会遇到在函数checker编写return (int)pointeraddress ,然后像这样在外部重铸的大问题? I never tried doing a cast like this. 我从未尝试过像这样的演员表。

someint=checker(..);
free((pointertype)someint);```

Maybe it is safer to use long int for checker ? 也许将long int用作checker更安全吗? I can't use uintptr_t as suggested in other questions because for this task I'm required to use only standard C library. 我不能按照其他问题的建议使用uintptr_t ,因为对于此任务,我仅需要使用标准C库。

Is it safe to cast a pointer as an int to return from an int function in C? 将指针转换为int从C中的int函数返回是否安全?

The cast to int itself is not a problem, but info may be lost for the next steps. 强制转换为int本身不是问题,但后续步骤可能会丢失信息。

An int is not certain to round-trip back to an equivalent pointer. 一个int不确定会往返返回等效的指针。

An int may lack enough bits to store all needed info about the pointer. 一个int可能缺少足够的位来存储有关该指针的所有所需信息。

someint=checker(..);
free((pointertype)someint); // bad

C provides optional integer types (u)intptr_t in standard C library. C在标准C库中提供了可选的整数类型(u)intptr_t

I can't use uintptr_t as suggested in other questions because for this task I'm required to use only standard C library. 我不能按照其他问题的建议使用uintptr_t,因为对于此任务,我仅需要使用标准C库。

This is curious as uintptr_t ubiquitously exists in all standard C library since C99. 很好奇,因为自C99以来,所有标准C库中普遍存在uintptr_t @Thomas Jager @托马斯·杰格

These types are wide enough to convert an object_pointertype --> (u)intptr_t --> object_pointertype successfully - results in a pointer that equates to the original. 这些类型足够广泛,可以成功地转换object_pointertype --> (u)intptr_t --> object_pointertype产生的指针等于原始指针。

#include <stdint.h>

Maybe it is safer to use long int for checker? 也许使用long int进行检查更安全?

Perhaps, yet not really. 也许,但不是真的。 long may still be too narrow. long可能仍然太狭窄。

long long may be too narrow also, yet that is less likely. long long也可能太窄,但这不太可能。


Converting to an integer type may be the wrong approach 转换为整数类型可能是错误的方法

Instead of "return 1 if an element is found or 0 otherwise.", consider 与其考虑“如果找到一个元素则返回1,否则返回0”。
"return the address if an element is found or NULL otherwise." “如果找到元素,则返回地址,否则返回NULL 。”

or something like the below and store the found pointer in *destination , 或类似下面的内容,并将找到的指针存储在*destination

bool find(void **destination, input parameters );

checker() needs to indicate 2 things checker()需要指出两件事

From a design standpoint, the found pointer, when converted to an integer, may be any value, even 0 or 1. A robust design would simply indicate 2 independent things: pointer found, value of that pointer. 从设计的角度来看, 找到的指针在转换为整数时可以是任何值,甚至可以是0或1。健壮的设计将仅指示2个独立的事物:找到的指针,该指针的值。 To roll those together implies some pointer is special and can never be "found". 将它们汇总在一起意味着某些指针是特殊的,并且永远不能被“发现”。 A generic linked list would support storing and retrieving all pointers, including null pointers . 通用链表将支持存储和检索所有指针,包括null指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM