ADA 中的地址表示
我在下面粘贴了 Ada 语言的代码。我需要对某些实现进行一些说明。
C : character;
Char : character;
type Myarr_Type is array (character range 'A'..'K') of character;
Myarr : Myarr_Type := ('A','B','C','D','E','F','G','H','I','J','K');
Next_Address := Myarr'address --'
Last_Address := Next_Address + Storage_Offset'(40); --'
return P2 + Storage_Offset'(4); --'
Last_Address := Next_Address + Storage_Offset'(4); --'
现在我的疑问是 1) P2 + Storage_Offset'(4) 实际上意味着什么。这是否意味着它返回数组中下一个元素的地址,即 Ada 中的 'B'.Storage_Offset'(4) --这样做表示 4 位或 4 个字节的内存。 2)如果我假设 Last_Address 指向数组的最后一个元素“K”,那么算术 Storage_Offset'(40) 如何满足实际实现?
如果您需要更多说明,请回复我。
请假设该函数不存在。 事实上,我有一些 ada 文件,我的工作是将它们转换为 C 文件。由于我是 ada 的初学者,我遇到了很多问题。如有任何混淆,请原谅,
谢谢 麦迪
I have pasted a code below which is in Ada language.I need some clarification on some implementations.
C : character;
Char : character;
type Myarr_Type is array (character range 'A'..'K') of character;
Myarr : Myarr_Type := ('A','B','C','D','E','F','G','H','I','J','K');
Next_Address := Myarr'address --'
Last_Address := Next_Address + Storage_Offset'(40); --'
return P2 + Storage_Offset'(4); --'
Last_Address := Next_Address + Storage_Offset'(4); --'
Now my doubt is 1) what does P2 + Storage_Offset'(4) actually mean.Does that mean that its returning the address of the next element in the array which is 'B'.Storage_Offset'(4) in Ada --does this mean 4 bits or 4 bytes of memory. 2) If i assume that Last_Address points to last element of the array which is 'K', how does the arithmentic Storage_Offset'(40) satisfies the actual implementation?
Please get back to me if u need any more clarifications.
Please assume that the function does not exist.
As a matter of fact,i have some ada file and my job is to convert them to C files.Since i am a beginner in ada,i faced a lot of issues with that.Please pardon in case of any confusion
Thanks
Maddy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Storage_Offset 是一种特殊的整数类型位于包
System.Storage_Elements
中,可以添加到System.Address
类型的对象。Address
和Storage_Offset
的单位到底是什么是实现定义的,但可能几乎现有的每个实现都使用字节。因此Next_Address + Storage_Offset'(4)
表示“Next_Address 引用的地址后四个字节的地址”。您谈到了 Ada 移植。在 99% 的情况下,这是一个非常愚蠢的想法(%1 是当您需要移植到没有 Ada 编译器的平台时)。无论您移植哪种语言,我都会说同样的事情。这是一场傻瓜游戏。移植代码时您可以期望的最好结果是,经过大量的努力后,它可以像以前一样工作。通过编码,最好的情况永远不会发生。
Ada 可以很好地与 C 接口,因此在 Ada 中保留不变的代码并且仅“移植”需要更改的内容会更明智。
如果您遇到任何任务代码、受保护类型或自定义流,您将陷入困境。这些东西并没有简单的 C 语言类似物。
如果你的老板真的对 C 或其他东西很痴迷,我建议你研究一下 Sofcheck 的 AdaMagic,它提供了将 Ada 代码转换为 ANSI C 的服务。回到过去(之前有两个所有者),他们曾经声称它生成了可维护的 C 代码。不管怎样,它可能比让一个没有经验的(在 Ada 中)开发人员尝试手工完成这一切要便宜得多。
Storage_Offset is a special integeral type in package
System.Storage_Elements
that can be added to objects of typeSystem.Address
. What exactly the units ofAddress
andStorage_Offset
are is implementation defined, but probably just about every implementation in existence uses bytes. SoNext_Address + Storage_Offset'(4)
means "the address four bytes past whatever Next_Address refers to."You talked a bit about Ada porting. In 99% of cases, that is a very stupid idea (the %1 being when you need to port to a platform that has no Ada compiler). I'd say the same thing no matter what language you are porting. It's a fool's game. The best outcome you can hope for when porting code is that after a ton of effort it works as well as it did before. With coding the best case never happens.
Ada can interface to C just fine, so it would be far smarter to keep unchanged code in Ada and only "port" stuff you need to change.
If you come across any tasking code, protected types, or custom streams you will be in a world of hurt. Those things don't really have easy C analogs.
If your bosses really have a boner for C or something, I'd suggest looking into Sofcheck's AdaMagic, which provides a service to transform Ada code to ANSI C. Back in the day (two owners prior) they used to claim that it produced maintainable C code. Either way, it will probably be far cheaper than having an inexperienced (in Ada) developer try to do it all by hand.
嗯,这是一个 C 函数,其主体是用 Ada 编写的。没有使用 Integer 和 Storage_Offset 的“+”运算符;也许您正在寻找
并且您打算调用
my_func(something, Next_Address)
?在这种情况下,表达式将返回
Myarr('A')
之后 4 个Storage_Element
的地址,即字节。Myarr_Type
是一个Character
数组,对于任何通用架构上的任何普通编译器来说,它都将是一个标准的 8 位字节。因此Myarr_Type
对象的长度将是 11 个字节,而不是 44,并且Myarr('A')'Address + 4
将是Myarr('E' )
。如果您想要 Myarr 最后一个元素的地址,请尝试
Well, this is a C function whose body is written in Ada. There is no "+" operator taking an Integer and a Storage_Offset; perhaps you're looking for
and perhaps you meant to call
my_func(something, Next_Address)
?In that case, the expression will return the address of whatever is 4
Storage_Element
s, ie bytes, afterMyarr('A')
.Myarr_Type
is an array ofCharacter
, which with any normal compiler on any common architecture is going to be a standard 8-bit byte. SoMyarr_Type
objects will be 11 bytes long, not 44, andMyarr('A')'Address + 4
will be the address ofMyarr('E')
.If you want the address of the last element of
Myarr
, try