乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: Bill Tillman

[编程交流] vlax-make-safearray Problem

[复制链接]

11

主题

968

帖子

919

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
99
发表于 2022-7-6 08:02:23 | 显示全部楼层
That is actually a reason NOT to use an array and to rather use a linked list. Growing / shrinking arrays is a very wasteful operation, whereas growing / shrinking lists is next to simply assigning to a variable.
回复

使用道具 举报

180

主题

639

帖子

463

银币

中流砥柱

Rank: 25

铜币
897
发表于 2022-7-6 08:07:24 | 显示全部楼层
You're absolutely right about that. I was toying around with a list method and got that working rather quickly.
 
F.Y.I. this method of using an array was working but when the quantity was an odd number the code I wrote was setting the boundary ok, but the nature of the code was that it would try to assign a value out of the bounds. My fix for that was just to allow the bounds of the array to be a little larger than needed, usually only by one element. And since this program was designed to run low and fast, and once it was completed it was done, the elements of the array would never need to be changed. Still I agree with everyone that the list method is a bit cleaner and easier to work with. In fact I used Find/Replace to change the code and switching from one the other took only a minute or two. I still have this block of code in it's own file and it gets called on demand and only if it's needed.
 
Learning something new each day keeps the mind young.
回复

使用道具 举报

11

主题

968

帖子

919

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
99
发表于 2022-7-6 08:10:46 | 显示全部楼层
That's the usual fix for this scenario. All structures like collections use either linked lists, or such "growable" arrays as their base structure. If they use arrays they tend to keep the array slightly (or sometimes a lot) larger than the actual needed size. The most usual method is to grow the array to double its current size when its bounds is reached - this is so the extremely inefficient recreation of the array is not done many times over. 
Note you need a separate variable to keep track of how many items are in fact used, since the bounds only shows "available-space" not "used-space". Some designs use one of the slots of the array to keep track of such. E.g. a Pascal String is an array of 256 bytes, the 1st of which contains the number of used characters. Another alternative is doing something similar to a C-String, where the last used slot contains a null value to indicate a "stop" - but this only works is null is not a valid normal value.
 
E.g. say the array bounds is (0 . 7), and you've used all 8 items from that. When you add another, you make a new array of (0 . 15), then copy each item from the old array into the new one, then add the new item. Then discard the old array. This is literally what happens with such "growable" arrays. Some implementations "might" be able to grow the array if the physical ram "in-front" of the array is not used by something else - but such happens very rarely.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-10 15:19 , Processed in 0.828359 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表