为C结构的一部分分配内存 [英] Allocating memory for part of a C struct

查看:50
本文介绍了为C结构的一部分分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是WinIoCtl.h中结构的声明:


//

// FSCTL_TXFS_READ_BACKUP_INFORMATION的结构

//


typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {

union {


//

//如果返回代码是用来返回所需的缓冲区大小

STATUS_BUFFER_OVERFLOW

//


DWORD BufferLength;


//

//成功后,数据将被复制到这里。

//


BYTE缓冲区[1];

} DUMMYUNIONNAME;

} TXFS_READ_BACKUP_INFORMATION_OUT,* PTXFS_READ_BACKUP_INFORMATION_OUT;


现在Buffer可以是各种长度而且我知道只有一种方法可以为它分配

内存(通过为整个结构分配内存)。我已经看到了

其他类似的结构,它们不是一个联合,并且有多个数组与

类似的声明。有没有办法为这样的数组分配内存?


问候

Chris Saunders

Here is the declaration of a struct from WinIoCtl.h:

//
// Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION
//

typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_OVERFLOW
//

DWORD BufferLength;

//
// On success the data is copied here.
//

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKUP_INFORMATION_OUT, *PTXFS_READ_BACKUP_INFORMATION_OUT;

Now Buffer can be various lengths and I know of only one way to allocate
memory for it (by allocating memory for the whole struct). I have seen
other similar structs that are not a union and have multiple arrays with a
similar declaration. Is there any way to allocate memory for such arrays?

Regards
Chris Saunders

推荐答案

Chris Saunders写道:
Chris Saunders wrote:

这是WinIoCtl.h中结构的声明:


//

// FSCTL_TXFS_READ_BACKUP_INFORMATION的结构

//


typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {

union {


//

//如果返回代码是用来返回所需的缓冲区大小

STATUS_BUFFER_OVERFLOW

//


DWORD BufferLength;


//

//成功时数据被复制到这里。

//


BYTE缓冲区[1];

} DUMMYUNIONNAME;

} TXFS_READ_BACKUP_INFORMATION_OUT,* PTXFS_READ_BACKUP_INFORMATION_OUT;


现在Buffer可以是vario我们知道只有一种方法可以为它分配

内存(通过为整个结构分配内存)。我已经看到了

其他类似的结构,它们不是一个联合,并且有多个数组与

类似的声明。有没有办法为这样的数组分配内存?
Here is the declaration of a struct from WinIoCtl.h:

//
// Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION
//

typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_OVERFLOW
//

DWORD BufferLength;

//
// On success the data is copied here.
//

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKUP_INFORMATION_OUT, *PTXFS_READ_BACKUP_INFORMATION_OUT;

Now Buffer can be various lengths and I know of only one way to allocate
memory for it (by allocating memory for the whole struct). I have seen
other similar structs that are not a union and have multiple arrays with a
similar declaration. Is there any way to allocate memory for such arrays?



分配内存很简单 - 您只需使用Marshal.AllocHGlobal或

stackalloc(尽管您必须自己进行大小计算,

占额外字节,并注意布局,特别是最后的

填充。)


问题是这样的一个东西。 .NET P / Invoke marshaller

并不了解VLA结构,所以你能得到的最好的是与C ++中相同的b $ b技巧 - 声明一个固定的最后的单元素数组,

然后,在为结构分配足够的内存之后,将其视为

N元素数组。 C#没有对固定阵列进行任何绑定检查,所以

你可以做得很好。

Allocating memory is easy - you just use Marshal.AllocHGlobal or
stackalloc (though you have to do the size calculation yourself,
accounting for extra bytes, and mind the layout, particularly the
padding at the end).

The problem is working with such a thing. .NET P/Invoke marshaller
doesn''t understand VLA structs, so the best you can get is the same
trick as in C++ - declare a "fixed" single-element array at the end,
and then, after you allocate enough memory for the struct, treat it as
N-element array. C# doesn''t do any bound checks for fixed arrays, so
you can do it just fine.


我要感谢Pavel Minaev。


问候

Chris Saunders
My thanks to Pavel Minaev.

Regards
Chris Saunders


Pavel Minaev写道:
Pavel Minaev wrote:

Chris Saunders写道:
Chris Saunders wrote:

>这是WinIoCtl.h中结构的声明:

//
// FSCTL_TXFS_READ_BACKUP_INFORMATION的结构
//

typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {

//
/ /用于返回所需的缓冲区大小,如果返回代码为
STATUS_BUFFER_OVERFLOW
//

DWORD BufferLength;

//
//成功后,数据将被复制到此处。
//

BYTE缓冲区[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKUP_INFORMATION_OUT,
* PTXFS_READ_BACKUP_IN FORMATION_OUT;

现在缓冲区可以有各种长度,我只知道为它分配内存的一种方法(通过为整个结构分配内存)。
我见过其他类似的结构不是一个联合,并且具有多个具有类似声明的数组。有没有办法为这样的数组分配内存?
>Here is the declaration of a struct from WinIoCtl.h:

//
// Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION
//

typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_OVERFLOW
//

DWORD BufferLength;

//
// On success the data is copied here.
//

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKUP_INFORMATION_OUT,
*PTXFS_READ_BACKUP_INFORMATION_OUT;

Now Buffer can be various lengths and I know of only one way to
allocate memory for it (by allocating memory for the whole struct).
I have seen other similar structs that are not a union and have
multiple arrays with a similar declaration. Is there any way to
allocate memory for such arrays?



分配内存很简单 - 您只需使用Marshal.AllocHGlobal或

stackalloc(尽管您必须自己进行大小计算,

占额外字节,并注意布局,特别是最后的

填充。)


问题是这样的一个东西。 .NET P / Invoke marshaller

并不了解VLA结构,所以你能得到的最好的是与C ++中相同的b $ b技巧 - 声明一个固定的最后的单元素数组,

然后,在为结构分配足够的内存之后,将其视为

N元素数组。 C#对固定数组没有任何绑定检查,所以

你可以做得很好。


Allocating memory is easy - you just use Marshal.AllocHGlobal or
stackalloc (though you have to do the size calculation yourself,
accounting for extra bytes, and mind the layout, particularly the
padding at the end).

The problem is working with such a thing. .NET P/Invoke marshaller
doesn''t understand VLA structs, so the best you can get is the same
trick as in C++ - declare a "fixed" single-element array at the end,
and then, after you allocate enough memory for the struct, treat it as
N-element array. C# doesn''t do any bound checks for fixed arrays, so
you can do it just fine.



为什么C#和p /调用此组中的信息,为什么要使用

Marshal.AllocHGlobal?

C ++ interop非常简单。

Why the C# and p/invoke information in this group, and why use
Marshal.AllocHGlobal?

C++ interop is so much easier.


这篇关于为C结构的一部分分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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