Go Down

Topic: an easier way of working with arrays (Read 951 times) previous topic - next topic

mickman112

Sep 03, 2014, 06:30 pm Last Edit: Sep 03, 2014, 06:41 pm by mickman112 Reason: 1
Hi there,

During the last Arduino project I've been working on, one of the biggest time consumers and annoyances I've had with writing code, was dealing with the lack of functionality and error handling from arrays. During that project I've made a few things that I needed, such as multi-dimensional arrays that can be accessed based on ID numbers and a few more things like that.

I'm planning to write a library that improves a lot of those things and eases up my workflow a bit. The main thing that concerns me is (and correct me if I'm wrong), is there a specific reason some of these issues with arrays have hardly been addressed?

For instance, often I find myself getting stuck because I'm querying values from an array that aren't there, and what you get is whatever is reserved in memory on that location. There's no check if you query things that are out of bounds, or simply allocated but not used. I know these things are often things you could track down easily, but when you're used to a workflow from more elaborate languages it can be quite frustrating.

Here's my plan in terms of functionality, some thoughts on feasibility and other suggestions would be most welcome!


/////////////////// ROADMAP //////////////////

- Proper memory allocation
   * make a smart behind the scenes allocation system? (will this work?)
   * prevent misunderstandings from data retrieved from a point in memory, such as writing -1 or null or something to any array/reference/entry that is removed

- Proper error handling (when in debug mode, needs serial connection)
   * check out of bounds queries (on compiling too)
   * check if query is filled (query is lower than index)
   * check if array is full (index becomes higher than array size)

---- Default array (int, float or string)
- methods
   * set array type
   * push and pop values (add, subtract from index)
   * set static (values stick to their array index number)
   * delete/clear array

---- Multi dimensional array (int, float or string with id, hash and or more values than one of one or more types, maybe struct will work well here)
- methods
   * get by id
   * get by hash (is this possible? Means you might need to return a list)
   * get by value (is this possible? Same as previous)
   * add/remove dimension/column (only when creating?)
   * remove passed value (recognize id for instance)

---- General stuff
- methods
   * sorting
       * sort by value/alphabetical
       * reverse array
       * shuffle (randomize the order)
   * getting
       * get array size (based on used space in array)
       * get allocated array size (based on creation of array)
       * min (return the smallest value)
       * max (return the highest value)
   * array (create a new array copied from the array)

I've tried to do my homework and see if I could find anything out there that comes close to what I'm looking for, if there's a solution for this kind of stuff already please let me know.

Best,
Mick

AWOL

Quote
The main thing that concerns me is (and correct me if I'm wrong), is there a specific reason some of these issues with arrays have hardly been addressed?

Because it's a non-issue and any methods would be inappropriate on a RAM-starved architecture.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Graynomad

#2
Sep 04, 2014, 01:51 am Last Edit: Sep 04, 2014, 01:52 am by Graynomad Reason: 1
Quote
There's no check if you query things that are out of bounds, or simply allocated but not used.

Welcome to the world of C, the language that allows you to do anything including kill the machine :)

Sure you can write a class that does all this, trouble is C is designed for memory-challenged CPUs and normally you can't afford the overhead for something like that.

This is slowly changing with all the 32-bit processors being released and I agree that faulty array indexing is a major cause of bugs.

Maybe there's a reason to have this in the same way that we have strings and the String class, one is bare metal and the other does neat stuff and is safer (I assume, I've never looked at the code). But as you have probably seen the String class is frowned upon around here. That said maybe that's just because it was unreliable (been fixed I think) and it's a resource hog.

Personally I miss some things from other languages like associative arrays, whether or not they are appropriate on a tiny embedded CPU is another story, but with the new CPUs maybe we can start getting a few goodies from higher-level languages.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Robin2


I'm planning to write a library that improves a lot of those things and eases up my workflow a bit.


My view is that your library may help you but, due to the very limited memory space and CPU speed on an Arduino, it may just be wasting time and memory for other users. The problem is that everyone's need is subtly different.

Also, I think it is better for newcomers to learn to manage the limited memory directly and to learn at a fundamental level how to use arrays - they are such an enormous part of programming in any environment.

Are you going to stick around to provide assistance to newbies WHEN they run into problems with your library.

Are you going to write comprehensive documentation for the library?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mickman112

#4
Sep 04, 2014, 12:16 pm Last Edit: Sep 04, 2014, 05:29 pm by mickman112 Reason: 1
Hi Robin, Graynomad and AWOL,

Thanks for your thoughts, you guys all provide completely valid points that I can only agree with.

Nevertheless I still think it's a good topic for a discussion, as Graynomad pointed out the release of more powerfull Arduino's show that there would be room for some higher-level programming methods. And as long as you have proper knowledge about RAM allocation and use something like this with caution it could be quite handy.

I also fully agree that a library like this should not just be thrown into the wild with the chance that novice users end up getting into trouble or try to evade getting a good understanding of memory usage and the functioning of arrays altogether. As you must have noticed from my initial post, the motivation for this library is completely based on personal needs, and they probably don't reflect those of the gross Arduino userbase.

I'll most likely only implement the basic functionality described in the initial post and will leave most of the fancy stuff out of the way. I'm curious, you would still recommend releasing something like this? I would include documentation and examples, but targeted to the audience that I deem to be actually getting a benefit from such a library, and probably with some proper capital warnings about memory usage and best practice.

I'd also really like to hear your thoughts on some of the specifics that I've described, what are things you would benefit from and deem feasible?

Best,
Mick

update: actually found a similar effort made that is already looking quite nice https://github.com/rodolfo-picoreti/array

westfw

Quote
the release of more powerfull Arduino's show that there would be room for some higher-level programming methods.

When the processor gets more powerful, why not use the various standard C++ mechanisms that implement such things?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy