从 C++ 到 Delphi 的类型转换
Typecasting from C++ to Delphi
我如何将其翻译成 Delphi?
typedef struct ext2dirent {
EXT2_DIR_ENTRY *next;
EXT2_DIR_ENTRY *dirbuf;
Ext2File *parent;
lloff_t read_bytes; // Bytes already read
lloff_t next_block;
} EXT2DIRENT;
typedef struct tagEXT2_DIR_ENTRY {
uint32_t inode; /* Inode number */
uint16_t rec_len; /* Directory entry length */
uint8_t name_len; /* Name length */
uint8_t filetype; /* File type */
char name[EXT2_NAME_LEN]; /* File name */
} __attribute__ ((__packed__)) EXT2_DIR_ENTRY;
EXT2DIRENT *dirent;
int blocksize = 4096;
dirent->dirbuf = (EXT2_DIR_ENTRY *) new char[blocksize]; //<-- This line
我想过这样做;
Type
PExt2_Dir_Entry = ^Ext2_Dir_Entry;
Ext2_Dir_Entry = packed Record
inode: Cardinal;
rec_len : Word;
name_len : Byte;
filetype : Byte;
name : Array[0..EXT2_NAME_LEN-1] of AnsiChar;
End;
var
temp : array of AnsiChar;
if dir = NIL then
Result := nil;
SetLength(temp,self.block_size-1);
dir.dirbuf := PExt2_Dir_Entry(@temp);
但是我没有得到我在 dir.dirbuf
中期望的结果。我不明白 new char
特性在 C++ 中的作用。不过我觉得可能和我的失败有关。
带有 new
的 C++ 代码行分配 blocksize
字节内存并将指针转换为所需类型。
您的 @temp 是指向指针的指针。这不是我们想要的。
在 Delphi 你可以做:
GetMem(dir.dirbuf, blocksize);
从上面的代码中不清楚,为什么分配大小是 4096 而不是 SizeOf(Ext2_Dir_Entry)
我如何将其翻译成 Delphi?
typedef struct ext2dirent {
EXT2_DIR_ENTRY *next;
EXT2_DIR_ENTRY *dirbuf;
Ext2File *parent;
lloff_t read_bytes; // Bytes already read
lloff_t next_block;
} EXT2DIRENT;
typedef struct tagEXT2_DIR_ENTRY {
uint32_t inode; /* Inode number */
uint16_t rec_len; /* Directory entry length */
uint8_t name_len; /* Name length */
uint8_t filetype; /* File type */
char name[EXT2_NAME_LEN]; /* File name */
} __attribute__ ((__packed__)) EXT2_DIR_ENTRY;
EXT2DIRENT *dirent;
int blocksize = 4096;
dirent->dirbuf = (EXT2_DIR_ENTRY *) new char[blocksize]; //<-- This line
我想过这样做;
Type
PExt2_Dir_Entry = ^Ext2_Dir_Entry;
Ext2_Dir_Entry = packed Record
inode: Cardinal;
rec_len : Word;
name_len : Byte;
filetype : Byte;
name : Array[0..EXT2_NAME_LEN-1] of AnsiChar;
End;
var
temp : array of AnsiChar;
if dir = NIL then
Result := nil;
SetLength(temp,self.block_size-1);
dir.dirbuf := PExt2_Dir_Entry(@temp);
但是我没有得到我在 dir.dirbuf
中期望的结果。我不明白 new char
特性在 C++ 中的作用。不过我觉得可能和我的失败有关。
带有 new
的 C++ 代码行分配 blocksize
字节内存并将指针转换为所需类型。
您的 @temp 是指向指针的指针。这不是我们想要的。
在 Delphi 你可以做:
GetMem(dir.dirbuf, blocksize);
从上面的代码中不清楚,为什么分配大小是 4096 而不是 SizeOf(Ext2_Dir_Entry)