Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Array Pointers (Especially for the attention of IanM)

Author
Message
The Wilderbeast
20
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 3rd Nov 2011 22:56 Edited at: 3rd Nov 2011 22:59
First of all Ian, thank you very very much for your wonderful set of plugins - I don't think I would have gotten very far with this project without them.





This code results in something along the lines of:
Quote: "Hello, world!
...
[Crash - Array subscript out of bounds]"


How closely is foo() linked with bar()? I would expect the code to insert a new row into bar(), but apparently it hasn't. Why is this, and are there any workarounds?


Cheers,
Mike


10% TGC Discount!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Nov 2011 23:11
Quote: "Why is this,"

There is some behaviour of arrays that I can't do too much about, and one of these is the fact that when you enlarge an array in any way it will allocate a new array, copy the old stuff over to the new, then return the old array memory to be reused at a later point.

It's also worth noting that arrays are simply glorified pointers to array-related data

With that out of the way, let's analyse what's going on.

First relevant line, which makes foo() and bar() represent the same array in memory:


Next relevant line:

This allocates a new array (as explained above), and creates an entirely new array. At this point foo() represents an array, and bar() points to freed memory (ie, it's not pointing to an array any more).

BTW, when you insert into an array, the first newly inserted item becomes the current item, so using ARRAY INDEX TO BOTTOM is not needed in your code.

Next relevant line:

Accessing random memory as if it were an array ... basically, you're lucky that a subscript error was what you got. The alternative would be that it works, but you overwrite some random memory which could be anything from other arrays, strings, loaded sounds/textures/objects etc and have corruption that you wouldn't be able to explain.

Quote: "and are there any workarounds?"

Let me know what you are trying to accomplish and I might be able to suggest an alternative.

The Wilderbeast
20
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 3rd Nov 2011 23:28 Edited at: 3rd Nov 2011 23:32
Thank you for picking up on this so quickly! I hope you didn't take it as a rant, I was just intrigued to know what was going on.

Quote: "BTW, when you insert into an array, the first newly inserted item becomes the current item, so using ARRAY INDEX TO BOTTOM is not needed in your code."

Thanks for that - you learn something new every day!

Essentially I need a way of modifying arrays without actually knowing exactly which array it is.

ie. So I have a load of arrays of myType. I have a single array which serves as a lookup table between a name and an array. Then using a set of functions I need to be able to manipulate these arrays by adding . removing data. The whole point is that the process is as abstracted as possible.

To explain more clearly, I have an assets type. I have several type of assets which I would like to keep separate and so I define them all individually. I then have an array which has the name of each asset, a unique identifier, and a pointer to the array that holds all of the assets of that type.

the aim is to be able to do pretty much anything I want to those arrays using the following commands:


Some of those are redundant for obvious reasons (it was copy+pasted from a rough prototype I made earlier).


10% TGC Discount!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Nov 2011 00:29
I don't fully understand what you want, so I coded to my own ideas (and minimally at that).

Here's the basic example:


There's only minimal functionality there, but it seems to hang together OK.

However, before you jump any further into this, I'll just let you know that you can get pretty much the same functionality from lookup tables, and it's probably a lot easier too.

The Wilderbeast
20
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 4th Nov 2011 00:41 Edited at: 4th Nov 2011 01:22
Wow thanks a lot, I'm heading off now but I'll grab a look in the morning and report back.

Many thanks,
Mike


[Edit]
Ahhhhhh, so as soon as you've linked the temp array to the one you're modifying and added the extra row, the array we wanted to modify is just pointless. But if we update the pointer in our lookup table to point to the same same thing as the temp array then we can just reference this array any time we want by linking a temporary array to the pointer in the lookup.

I take my hat off to you sir. If you leave your moon base for a visit to earth, drop by and I'll buy you a beer some time!

Cheers.


10% TGC Discount!

Login to post a reply

Server time is: 2026-07-10 18:21:42
Your offset time is: 2026-07-10 18:21:42