我是Ada的初学者,网上的大部分资源都是用C语言编写的,我很难把它翻译成Ada。
我应该将POSIX与shmget和shmat一起使用,还是应该将SysV shm与mmap和shm_open一起使用?
你能给我一个使用这两个过程(写,然后读)的Ada程序的例子吗?例如,假设我想写,然后读取字符串"Butterflies“。
万分感谢!
发布于 2018-08-07 04:23:07
有几种方法可以做到这一点。也许最简单的是内存覆盖。假设您为$33FF
保留了一块内存$3300
,您可以使用$3300
处的字节来指示字符串的长度,使用$3301
..$33FF
作为字符串的内容。
With Interfaces;
Package ShortString is
Type String( Length : Interfaces.Unsigned_8 ) is private;
-- Convert a shortstring to a standard string.
Function "+"( Input : String ) Return Standard.String;
-- Convert a standard string to a short-string.
Function "+"( Input : Standard.String ) Return String
with Pre => Input'Length <= Positive(Interfaces.Unsigned_8'Last);
Private
-- Declare a Positive subtype for a byte.
Subtype Positive is Interfaces.Unsigned_8 range 1..Interfaces.Unsigned_8'Last;
-- Use the byte-sized positive for indexing the short-string.
Type Internal is Array(Positive range <>) of Character;
-- Declare a varying-length record for the short-string implementation.
Type String( Length : Interfaces.Unsigned_8 ) is record
Data : Internal(1..Length);
end record;
-- We must ensure the first byte is the length.
For String use record
Length at 0 range 0..7;
end record;
Function "+"( Input : String ) Return Standard.String is
( Standard.String(Input.Data) );
Function "+"( Input : Standard.String ) Return String is
( Length => Interfaces.Unsigned_8(Input'Length),
Data => Internal( Input )
);
End ShortString;
然后对于内存覆盖:
Overlayed_String : ShortString.String(255)
with Import, Address => System.Storage_Elements.To_Address( 16#3300# );
https://stackoverflow.com/questions/51712397
复制相似问题