Reading a value from an address, using an array

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Good day! I need to read 12 values from 12 addresses, which are reserved only for these 12 values no matter how many times the application is run, I read the information from a binary file, record it on the 12 addresses and than use an array to compare the 12 values from the addresses, to 12 values in the array. The idea is not to compare every variable 1 by 1, because that will to bad for me. Perhaps there is a way to reserve the addresses and just compare it to the value in the array which is the same as the address? Meaning "if value on address 0x105 equals the value in array element 5, do something".
How about like this:

for (i=0; i<12 ;i++)
parameter = 0x100 + i;

How do I use it afterwards in order to compare with an address in the array.
In array "parameter[2]" I have recorded "0x102", I guess I have to somehow reserve addresses from "0x100" to "0x111" for use only by putting something there by using the address in the array, can someone tell me how to do that please?

The whole idea is that I dont declare the variables separetaly, but use one array in order to compare a value with the value on address "what is recorded in the array" instead of using 1 by 1 the variables, which makes the program too long.

So I compare the value read from a file "100 decimal" to the value in address "0x102" which is in the array element "paremeter[2]".
I also need to reserve the addresses from "0x100" to "0x111", so the value from them is always read and always coresponds to the same value for comparison.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
I was thinking will this work:

int parameter [12];
for (i=0; i<12 ;i++)
{
parameter = 0x100+1; /* The parameter element "i" is equal to the value of address "0x100 + i" */
}

Will this set the value of the array to "0x100 - 0x111"?

Or maybe this:
int parameter [12];
int value [12];
for (i=0; i<12 ;i++)
parameter = *value; /* Parameter element i, equals the value in array "value i" */

Since the site edits my code, you can check it here:
https://www.dropbox.com/s/9fs9b46z3udt2m8/All-about-circuits-arrays-addresses.txt?dl=0
 

ErnieM

Joined Apr 24, 2011
8,377
First off, if you use the Code Tags feature in the reply box you can post code without the formatting getting removed:
CODE TAGS.jpg
I am not quite sure what data you are trying to define or compare beyond you seem to need to reserve 12 locations, and you seem to be working in the C language.

You want 12 elements ranging from 0x100 (4) to 0x111 (7). That is just 4 elements, so something is off. Instead let me show you how to define the elements of the array from 1 to 12

C has a "const" keyword (for constant) that allows you to create data in your program that you can use but never change.

Code:
   const int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1001, 0x1010, 0x1011, 0x1100};
Or perhaps even easier:
Code:
   const int parameter [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Either way you get an array called "parameter" with 12 elements numbered 0 thru 11 with a value of 1 thru 12.

Now if you thing you will ever want to change the values of this array you can make a regular array that is not constant by simple not using the "const" keyword:

Code:
    int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1010, 0x1011, 0x1100};
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Sorry for the mistakes, I wrote it too fast.

1. I am using "ANSI C" with the GNU C Compiler
2. I need to reserve 12 addresses, to read the value from them, and if its different than what I need, to increment a counter. The addresses and value in them must not change, no matter how many times the application is run,

I understand this, despite that you wrote it with binary values as hexadecimal:

Code:
const int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1010, 0x1011, 0x1100};
I guess it should be from "0x100" to "0x10B". We both wrote it wrong :D.
I understand that "const" should place the array in the permanent memory (ROM) as an initialization of the "retlw" instruction, but I dont think its what I need.

How do I reserve 12 addresses, record a value in them, read the value and compare it to something, using an array in order to shorten the program?
 

djsfantasi

Joined Apr 11, 2010
9,156
I don't understand why you don't just use another array? If you want to make sure it is not changed, used the const keyword as ErnieM suggested. Why do you think it is not what you need? But you did say, record a value, so the const keyword will not work.

How about the new or malloc keywords? This will allocate a block of memory to which you can save values. But you will have to calculate pointers to the locations to which you are saving those values. And just using an array is much simpler. You can use malloc and new to define the memory used by the array, if the automatic assignment is not good for you. See the attached sketch for my technique using "new".

At this stage, I am assuming the "something" to which you are comparing these values is another array. A simple for loop, comparing two arrays is all you need. And even if you decide to use pointers to the values you recorded, the loop will still work, substituting the pointer calculation for the array reference in the case of the recorded values.

Note that if you decide to use only pointers (why you would, I don't know yet), you must take into account the size of the values recorded. a char value is shorter that an int value which is shorted than a long value... In constructing an aray with the new keyword, you must supply the value type and it takes the length in account when reserving memory.

The attached sketch is a proof of concept, to dynamically allocate memory to an array, depending on input data.

Good luck!

Code:
#define halt while(-1){}
unsigned int Size = 3; // value of the desired array to pass

void setup() {
  Serial.begin(9600);
}

void loop() {
  char* anArray=NULL; // define a pointer to the array

  anArray = makeArray(Size); // create the array

  for (int i = 0; i <= Size; i++) { // print out the test
    Serial.println(anArray[i]);
  }
  Serial.println();
  halt;
  // normally, array memory should be deleted around here
}

char* makeArray(int mySize) { // function returns a pointer

  char* myArray = new char[mySize]; // create the array

  for (int i = 0; i <= mySize; i++) { // *Debug* fill the array with test values
    myArray[i] = 65+ i; // *Debug* only used for testing
  }
  return myArray; // return the pointer to the allocated array
}
Here is the code with the test code stripped out:

Code:
#define halt while(-1){}
unsigned int Size = 3; // value of the desired array to pass

void setup() {
  Serial.begin(9600);
}

void loop() {

  [B]char* anArray=NULL; // define a pointer to the array

  anArray = makeArray(Size); // create the array[/B]

// the remainder of the sketch, referencing the array created

   Serial.println();
   halt;
// normally, array memory should be deleted around here
}

char* makeArray(int mySize) { // function returns a pointer
  char* myArray = new char[mySize]; // create the array
  return myArray; // return the pointer to the allocated array
}
 
Last edited:

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Thank you for the explanation and code. The idea is that normally I program microcontrollers and real time operating systems are not that known to me, however I needed to record in a file values, and then read them again. The file must contain 2 addresses for 1 parameter, and we have 12 parameters. Then we must read from the binary file 2 values for every parameter, from a specific address. If this was a microcontrtollers things would be different, like the "#byte" directive which will alocate 1 memory address at the address I choose.

For real time operating systems I was thinking of this:

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
                                            /*Assign the address to a pointer */

char* pointer = (int*) 0xff73000;
                                            /* Access the value */

char value = *pointer;
char fist_byte = pointer[0];
char second_byte = pointer[1];

for (i=0; i<12 ;i++)
{
     if (pointer [i] <= sensor || pointer [i] >= sensor)
           event(record);
}
I dont know the "new" command, I remember about "callos()" and "malloc()" something, but not much and I believe "calloc()" was better?

Note that if you decide to use only pointers (why you would, I don't know yet), you must take into account the size of the values recorded. a char value is shorter that an int value which is shorted than a long value... In constructing an aray with the new keyword, you must supply the value type and it takes the length in account when reserving memory.
I know in real time operating systems "char = 8-bits", "int = 16-bits", I though every element will be the same, I dont know the "new" keyword.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
Sorry for the mistakes, I wrote it too fast.

1. I am using "ANSI C" with the GNU C Compiler
2. I need to reserve 12 addresses, to read the value from them, and if its different than what I need, to increment a counter. The addresses and value in them must not change, no matter how many times the application is run,

I understand this, despite that you wrote it with binary values as hexadecimal:

Code:
const int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1010, 0x1011, 0x1100};
I guess it should be from "0x100" to "0x10B". We both wrote it wrong :D.
I understand that "const" should place the array in the permanent memory (ROM) as an initialization of the "retlw" instruction, but I dont think its what I need.

How do I reserve 12 addresses, record a value in them, read the value and compare it to something, using an array in order to shorten the program?
Yeah, is should have been
Code:
const int parameter [] = {0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0110, 0b0111, 0b1000, 0b1010, 0b1011, 0b1100};
Let's concentrate on this:
2. I need to reserve 12 addresses, to read the value from them, and if its different than what I need, to increment a counter. The addresses and value in them must not change, no matter how many times the application is run,
Why do you need to know the address of the data? Why can't this address ever change? Why not just a nice variable name?

If the value cannot change, when is it known? When you are writing the program (called "compile time) ? At program start up? At some other time? If at compile time you can use a bunch of constants, most probably an array or some other structured element.

Thank you for the explanation and code. The idea is that normally I program microcontrollers and real time operating systems are not that known to me, however I needed to record in a file values, and then read them again. The file must contain 2 addresses for 1 parameter, and we have 12 parameters. Then we must read from the binary file 2 values for every parameter, from a specific address. If this was a microcontrtollers things would be different, like the "#byte" directive which will alocate 1 memory address at the address I choose.

For real time operating systems I was thinking of this:
<SNIP>
I dont know the "new" command, I remember about "callos()" and "malloc()" something, but not much and I believe "calloc()" was better?


I know in real time operating systems "char = 8-bits", "int = 16-bits", I though every element will be the same, I dont know the "new" keyword.
Why does one parameter require two addresses? Or is it two parameters for each address?

What is this "binary file"?
 

djsfantasi

Joined Apr 11, 2010
9,156
...

For real time operating systems I was thinking of this:

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
                                            /*Assign the address to a pointer */

char* pointer = (int*) 0xff73000;
                                            /* Access the value */

char value = *pointer;
char fist_byte = pointer[0];
char second_byte = pointer[1];

for (i=0; i<12 ;i++)
{
     if (pointer [i] <= sensor || pointer [i] >= sensor)
           event(record);
}
What is this "real time operating systems" you are talking about?

And I totally agree with ErnieM...
Why do you need to know the address of the data? Why can't this address ever change? Why not just a nice variable name?

If the value cannot change, when is it known? When you are writing the program (called "compile time) ? At program start up? At some other time? If at compile time you can use a bunch of constants, most probably an array or some other structured element.
I suggest that if you answer these questions, we can help you better. IMHO, I think you think that you think you know what you think. But to use, it is not making sense. It looks like you have a simple requirement but don't understand enough to recognize it. So you have come up with a complicated scheme to solve your problem. Except the scheme does not solve your problem.

Help! Help us help you...
 

djsfantasi

Joined Apr 11, 2010
9,156
Oh, and one last word. This line of code is missing the binary value for 9, which is shown in your decimal list.

Code:
const int parameter [] = {0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0110, 0b0111, 0b1000, 0b1010, 0b1011, 0b1100};
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Here is the situation. I think this: normally for low level embedded coding we have master devices and slave devices, lets say that a master device will write the information at a certain address every time, that way, the slave device does not need any confirmation from the master, does not need to send requests for data, does not need an interface or to acknowledge the data. The master records the data in a file on addresses 0 to 23, from 0 to 11 are the low border values, from 11 to 23 are the high border values for the 12 parameters, or the slave can write how many times the error was met with the system data gotten from elswhere, in a file, from where the master can read it at any time. This is why the addresses must not change, since we will check them every second or so, to see the amount of errors occurred.
 

djsfantasi

Joined Apr 11, 2010
9,156
So the "address" is a location within a file? Not a location in memory?

I still am having trouble seeing how this will work. What platform are you using that supports a shared file system? Or shared memory?
 

ErnieM

Joined Apr 24, 2011
8,377
So far every explanation of what task needs to be accomplished contains a fair amount of what can be called gibberish.

Lets try to be concrete. What is this master device? What is the slave device?

And what is this mysterious "file" you keep referring to? What holds the file? Does it have a part number? WHAT IS IT???

Post part numbers, not vague descriptions.

A system block diagram would be helpful.
 

djsfantasi

Joined Apr 11, 2010
9,156
Once again, I totally agree. This is beginning (or has begin) to appear like the ramblings of a troll.
  • You state that you are experienced in microcontrollers.
  • Yet, you are proposing a structure that is not available in any platform that I know.
  • You refuse to provide the details of the platform you plan on using, which would address my concerns.
  • We gave gone on for 10 posts before you mention this is a master-slave configuration.
  • There is no functional description of any detail provided. Like a block diagram.
  • There is gibberish instead of concrete facts.
Troll? You decide. I am waiting for more detail.
 

ErnieM

Joined Apr 24, 2011
8,377
There is nothing in this "specification" that contradicts using an array for both the parameters and the error counts.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
What about "The application shall have 12 parameters (unsigned integers), each of them on a defined memory
address (the same through multiple run of the application)." which is on the 9th-10th row?
 

djsfantasi

Joined Apr 11, 2010
9,156
I suspect this requirement cometh from an old environment and are stated with that environment's constraints in mind.

It's just my opinion.

The requirement may be the two processors share results between them. Which can be done in a number of ways without using static memory locations. And I did note, they must be persistent. Also doable with the caveat that how depends on what. I.e., EEPROM, local file system, remote file system, etc.

Besides responding with "This" when asked "What", consider adding "Why" and "How" to your response.
 

ErnieM

Joined Apr 24, 2011
8,377
What about "The application shall have 12 parameters (unsigned integers), each of them on a defined memory
address (the same through multiple run of the application)." which is on the 9th-10th row?
Asked and answered.
There is nothing in this "specification" that contradicts using an array for both the parameters and the error counts.
Variables do not change addresses.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
But the addresses need to be on a defined memory address from the programmer!

If this was a microcontroller, I would be able to easily define the address and then use the variable for this address.
But lets see if possible how to do it for real time operating systems?

Perhaps if possible I can use a pointer? The pointer points towards an address, on which address we have the value.
I know "&" returns the address.
 

djsfantasi

Joined Apr 11, 2010
9,156
What don't you understand? An array is a defined fixed address.

What do you mean, "by the programmer?" And I have asked before, what "real time operating system"? Without details, there is no way your question can be answered.
 
Top