是否可以将内存分配给具有特定索引的动态数组? [英] Is it possible to allocate memory to dynamic array with specific indexes?

查看:63
本文介绍了是否可以将内存分配给具有特定索引的动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有动态数组

type TCharArr = Array of byte;
type PcharArr = ^TCharArr;
var charArr: PcharArr;

我想用New(charArr);在堆中分配内存;

Which I want to allocate memory in Heap in the way of New(charArr);

但是,如何指定大小和索引?动态数组是否可以具有索引,例如.从512 ..到1024?

However, how can I specify size and indexes? Is it possible dynamic array to have indexes eg. from 512.. to 1024?

推荐答案

动态数组始终基于零.如果要使用具有不同基数的数组索引,则需要封装考虑到索引偏移量的数组访问.像这样:

Dynamic arrays are always zero based. If you want to use array indices with a different base, then you would need to encapsulate the array access accounting for the offset to the indices. Something like this:

const
  Offset = 512;

function GetValue(Index: Integer): Byte;
begin
  Result := Arr[Index - Offset];
end;

procedure SetValue(Index: Integer; Value: Byte);
begin
  Arr[Index - Offset] := Value;
end;

这篇关于是否可以将内存分配给具有特定索引的动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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