0

UPDATE: Considerations: this is on an embedded platform, I am memory limited and not wanting to have to use any extra Libs to perfom things like hashing..

I am running a program that uses a multi dimensional array.

uint8_t array[8][32]

This array is constantly updating, I need to run a comparison to determine if in the last 3 steps, there is a repetition.

It occoured to me that the array is just memory, so rather than iterate through the array, and create a comparison temp array (memory and processor wasted) why not approach this almost like making a hash of the array. I dont need any unique crypto so why not just read the memory location of the whole array as an integer?

Thoughts? How would I go about addressing the array to read in an integer which I could store in another simple array for my much easier comparison

unsigned long arrayTemp[3]

Nick
  • 908
  • 12
  • 29
  • 1
    `memcmp` was meant specifically for situations like this. – hegel5000 Jul 03 '21 at 12:13
  • You may also keep an `int` variable `noOfChanges` which you increment when you update your array. – Kitswas Jul 03 '21 at 12:20
  • Hmm, comparing 256 bytes should not frighten a modern computer... But comparing the addresses is nonsense because it carries no information about the content. – Serge Ballesta Jul 03 '21 at 12:24
  • @hegel5000 looking at memcmp, I think I will have to create a much larger temp array to hold the entire multidimensional array for comparison. This is what I am trying to avoid. I want to read the array as pure bytes and store that value as an int..... this is where I am struggling – Nick Jul 03 '21 at 12:25
  • I'd just use an existing hash function. It's a good idea when warranted, but the example given here doesn't warrant it because your data is barely bigger than a hash. You're talking more like a checksum, which has a high collision rate. (unless ... oh it is an embedded platform, ok) – Kenny Ostrom Jul 03 '21 at 12:28
  • https://stackoverflow.com/questions/4567089/hash-function-that-produces-short-hashes recommends taking a hash (e.g. SHA), then truncating or reducing with xor (but I'm not sure how much memory it needs when actually computing the hash). – Kenny Ostrom Jul 03 '21 at 12:35
  • yeah the hash is gonna be a problem, I already looked into it and the library size makes it a no go.... hence my idea to simply read the entire memory location and store as an integer – Nick Jul 03 '21 at 12:37
  • position dependent checksums like Fletcher, Adler or CRC? – Kenny Ostrom Jul 03 '21 at 13:20
  • The problem with hashes if that you cannot have a 100% guarantee that same hash correspond to the same original data, and the shorter the hash the higher the risk of collision (different inputs giving same hash). BTW what are the characteristics of your *embedded system* (available memory, and *class* of processor, and available/forbidden parts of the standard libraries (C - C++)? – Serge Ballesta Jul 03 '21 at 13:35
  • @KennyOstrom: Well I immediately thought of CRC, because they are a very nice tool requiring very few resources. But they are aimed at detecting transmission errors, not arbitrary changes. Unsure of their actual performance here... – Serge Ballesta Jul 03 '21 at 13:39
  • IMHO, you should try to generate a number of possible arrays, and test CRC *of size of your int size*. If you find no collision despite a rather large number of tries, then it could be a possible way. – Serge Ballesta Jul 03 '21 at 13:43
  • 1
    When you say _" if in the last 3 steps, there is a repetition"_, what do you mean exactly? Can you give some examples of situations you would like to detect? – Ted Lyngmo Apr 06 '22 at 22:54

0 Answers0