X的动态数组的Delphi动态数组的内存布局是什么?

发布于 2024-08-06 10:48:23 字数 881 浏览 3 评论 0原文

我正在尝试从 C# 调用 Delphi DLL 中的过程。该过程期望调用者预先分配并输入一个TSomeRecord 数组 的数组,然后它将操作其中的TSomeRecord 元素作为返回结果的方法。所以,我需要手工制作 X 数组的 Delphi 动态数组。
现在,我在这里找到了< /a> X 的动态数组由指向动态数组第一个元素的指针组成,并且该第一个元素具有引用计数和前面附加的数组长度(元素数) (都是 32 位整数),并且元素是内联且连续存储的,因此整个事情在内存中看起来像这样:

rrrrllll000...000111...12...
        ^

rrrr 是引用计数,llll 是长度,0123 是元素,^ 是指针指向的位置。这证明了;我已经测试过它并且有效。
对于多维动态数组,我假设可以用array of Y替换array of X中的X,换句话说,外部维度只是动态数组(指向)的动态数组,如下所示:

rrrrllll000011112222...
        ^

其中元素 0000、1111 等现在是指向独立分配的动态数组的 32 位指针。然而,这样做会给我带来访问冲突,因为我的麻烦。这显然不是德尔福希望我这样做的方式。谁能向我解释一下我应该如何做到这一点?

I am trying to call a procedure in a Delphi DLL from C#. The procedure expects the caller to preallocate and input an array of array of TSomeRecord, of which it will then manipulate the TSomeRecord elements as a means of returning results. So, I need to hand-craft Delphi dynamic arrays of arrays of X.
Now, I have found here that a dynamic array of X consists of a pointer to the first element of the dynamic array, and that that first element has a reference count and the length (number of elements) of the array prepended (both 32-bit integers), and that the elements are stored inline and contiguously, so that the whole thing looks like this in memory:

rrrrllll000...000111...12...
        ^

with rrrr the reference count, llll the length, 0123 the elements, and ^ where the pointer points to. This bears out; I have tested it and it works.
For multidimensional dynamic arrays I have assumed that I can substitute array of Y for the X in array of X, in other words that the outer dimension is simply a dynamic array of (pointers to) dynamic arrays, like so:

rrrrllll000011112222...
        ^

where the elements 0000, 1111 etc are now 32 bit pointers to independently allocated dynamic arrays. Doing it this way, however, earns me an access violation for my troubles. This is apparently not how Delphi expects me to do it. Can anyone explain to me how I am supposed to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2024-08-13 10:48:23

动态数组是指向元素压缩块的指针。

所以 TSomeRecord 的数组的数组是一个指向指针数组的指针,每个指针都指向一个具有 length(array[firstlevel]) 元素的块内存,如果没有则为 nil。

换句话说,您的假设大致是正确的,此外,具有零个元素的数组为零。请注意,除非您真的知道自己在做什么,否则您不应该自己更改引用计数和长度。

如果没有示例代码,则很难确定导致崩溃的原因。请记住,对于所有自动化Delphi类型(宽字符串除外),所有动态内存都必须由delphi内存管理器分配。

尝试使用您所连接的任何语言的内存管理器都是不可能的。

A dynamic array is a pointer to a packed block of elements.

So array of array of TSomeRecord is a pointer to an array of pointers, each of which points to a block memory with length(array[firstlevel]) elements, or nil if there are none.

In other words, what you assume is roughly correct, with the addition that arrays with zero elements are nil. Note that you are not supposed to change reference count and length yourself unless you REALLY know what you are doing.

Determining what causes your crash will be hard without example code. Keep in mind that, as for ALL automated Delphi types (except widestring), all dynamic memory must be allocated by the delphi memory manager.

Attempts to that using the memory manager of whatever language you are interfacing to is not possible.

痴意少年 2024-08-13 10:48:23

语言指南(曾经作为非常有用的印刷手册提供,现在在在线帮助中找到此信息非常困难)指出:

“多维数组存储时最右边的维度首先增加。”

因此,据我所知,你没有一个指针数组 - 只是每个维度数据一个接一个,从最右边的一个开始,我想它更快,因为没有更多的间接寻址。

The Language Guide (once available as very useful printed manuals, now finding this info in the online help is very difficult) states:

"A multidimensional array is stored with the rightmost dimension increasing first."

Thereby AFAIK you have not an array of pointers - simply each dimension data one after another, starting from the rightmost one, I guess it's faster because there's no more indirections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文