是((无效*)-1)一个有效的地址? [英] Is ((void *) -1) a valid address?

查看:122
本文介绍了是((无效*)-1)一个有效的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

逐字从男人的shmat

返回值

[...]出错(无效*)是返回-1,并设置errno以指示错误的原因。

[...] on error (void *) -1 is returned, and errno is set to indicate the cause of the error.

有没有强制性规则或确定指标(标准)指出(无效*)-1 可能不是一个有效的地址?

Is there any mandatory rule or defintion (standard?) that (void *) -1 may not be a valid address?

推荐答案

要直接回答,没有,也没有强制性规则,定义,标准或规范,内容是(无效*) - 1 可能不是一个有效的地址。

To answer the question directly, no, there is no mandatory rule, definition, standard, or specification that says (void *) -1 may not be a valid address.

(当然,没有规则,定义,标准,或约内存地址规范是强制性的。我看到人们每天走在街上不符合C标准,比如,但是我从来没有见过任何人被捕它但是,即使我们忽略了强制性部分,使用(无效*)-1 作为地址一般不常用规格禁止的。)

(Of course, no rules, definitions, standards, or specifications about memory addresses are mandatory. I see people walking down the street every day without conforming to the C standard, for example, but I have never seen anybody arrested for it. But, even if we omit the mandatory part, using (void *) -1 as an address is generally not prohibited by common specifications.)

然而,这是没有必要为(无效*)-1 不是一个有效的地址,以便执行shmat工作。它仅仅是必要的一个成功来电SHMAT不会返回(无效*)-1 (无效*) -1 由编译器从测试的shmat的返回值的目的支持。如果这两个条件都满足,则程序总是可以区分从一个不成功的shmat呼叫成功的shmat呼叫

However, it is not necessary for (void *) -1 not to be a valid address in order for shmat to work. It is merely necessary that a successful call to shmat never returns (void *) -1 and that (void *) -1 be supported by the compiler for purposes of testing the return value from shmat. If these two conditions are met, then a program can always distinguish a successful shmat call from an unsuccessful shmat call.

对于第二个条件,C标准不保证(无效*)-1 可以使用,所以POSIX,在指定这是一个错误的回报的shmat,含蓄地要求C(或其他语言)实现,以支持它。所以这是一个扩展,通过POSIX所需的语言,它是一般的编译器支持简单的事情。

Regarding the second condition, the C standard does not guarantee that (void *) -1 can be used, so POSIX, in specifying that this is an error return from shmat, implicitly requires the C (or other language) implementation to support it. So this is an extension to the language required by POSIX, and it is generally a simple thing for compilers to support.

对于第一个条件,考虑当我们可能想的shmat返回(无效*)-1 一个成功的呼叫。的shmat可与用户要求的地址或无,在这种情况下,实现选择的地址被调用。在任何正常的计算机体系结构,也有使用是不同的值的倍数的地址的多个原因。对于的shmat,最明显的是内存映射。上与虚拟存储器结构,存储器被映射以页为单位,和的shmat,当它映射存储器的段,将映射到一个页面的开始。任何偶数页大小没有倍数是(无效*)-1 ,因为后者是奇数,所以从来没有的shmat选择映射段 (无效*)-1 。即使的shmat没有用的页大小,它通常会使用一些其他取向,例如4,8或16字节,因为提供对准的存储器装置中存储在该存储器的起始结构将被对齐,这导致更快的存储器在许多处理器上的访问。

Regarding the first condition, consider when we might want shmat to return (void *) -1 for a successful call. shmat can be called with a user-requested address or without, in which case the implementation chooses an address. In any normal computer architecture, there are multiple reasons to use addresses that are multiples of various values. For shmat, the most obvious is memory mapping. On architectures with virtual memory, memory is mapped in units of pages, and shmat, when it maps memory for the segment, will map to the start of a page. Any even page size has no multiples that are (void *) -1, since the latter is odd, so shmat never chooses to map a segment to (void *) -1. Even if shmat did not use page size, it would typically use some other alignment, such as 4, 8, or 16 bytes, because providing aligned memory means that structures stored at the start of that memory will be aligned, which results in faster memory access on many processors.

这离开的情况下用户请求(无效*)-1 为一个地址。这将是不寻常的,它可以工作只有在内存段是一个单字节或内存模型允许缠绕(或编译器presented一个很奇怪的内存模型,其中(无效*) -1 不是在地址空间中的最后一个字节)。我不能肯定地说任何POSIX系统是否支持此与否。但是,很显然,这是基本上是无用的,没有人有任何理由这样做以外的好奇心。因此,它是安全,合理排除这种情况下出来的shmat的,只是说,不支持,不这样做。

That leaves the case where the user requests (void *) -1 as an address. This would be unusual, and it could work only if the memory segment were a single byte or the memory model allowed wrapping around (or the compiler presented a very strange memory model in which (void *) -1 were not the last byte in the address space). I cannot say for sure whether any POSIX systems support this or not. However, it is clear that this is essentially useless, and nobody has any reason for doing it other than curiosity. Therefore, it is safe and reasonable to rule this case out of shmat, simply saying that is not supported, do not do it.

这篇关于是((无效*)-1)一个有效的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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