re-use global variable space for character arrays

Status
Not open for further replies.

grease_lighting

Well-known member
I am using a character array to add tags to a value output. I do this to format the data into a table to be used in excel. I have declared the array to be adequate size for all the tags used. The contents of these tags get changed frequently and hence end up using more and more Global variable space. What can I do to prevent this from happening. I am aware of the 'F' macro for printing. Is there a means to redefine the character array contents without eating up more of the Global variable space?

Code:
char temp[21] = "0123456789abcdefghik";

void setup() {
  strcpy(temp, "wertyuiopasdfghjklbx");       // put your setup code here, to run once:
  output();
  strcpy(temp, "fghjklbx");       // change the tag
  output();                             //  output next value
  strcpy(temp, "wertyui");
  strcpy(temp, "wertyjklbx");
  strcpy(temp, "wesdfghjklbx");
  strcpy(temp, "wertyuiopasdfghjklbx");
  strcpy(temp, "zyxwvutsrqponmlkjihg");
  strcpy(temp, "wertyuix");
  strcpy(temp, "wertyuiopasdfghjklbx");
  strcpy(temp, "zyxwvutsqponmlkjihg");
  strcpy(temp, "wertyuioasdfghjklbx");      //  everytime the contents
  strcpy(temp, "zyxwvutsqponmlkjihg");      //  get changed, it takes
  strcpy(temp, "wertyuioasdfghjklbx");      //  up more Global variable
  strcpy(temp, "zyxwvutsqponmlkjihg");      //  space.
  strcpy(temp, "wertyuioasdfghjklbx");
  strcpy(temp, "zyxwvutsqponmlkjihg");
  strcpy(temp, "wertyuioasdfghjklbx");
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

void outPut() {
  Serial.println(temp);
}
 
You did not mention what Teensy you are using.

If you using the Teensy 2.0/2.0++, you have to use PROGMEM (which includes the F macro) to put constants in the read-only program memory, and when you want to use it, you have to use special functions to copy it back into read/write memory.

If you are using a Teensy 3.0, 3.1, 3.2, 3.5, 3.6 or LC, the compiler already puts these constants in the read-only program memory. Because program memory is part of the address space, you can use the pointer to the constant normally like you would any constant pointer (C++ will complain if you pass a pointer to constant memory to a function expecting a read/write pointer).

Note if you have initialized arrays, such as:
Code:
char foo[] = "abcdef";

you can add the const keyword:
Code:
const char foo[] = "abcdef";
 
I googled "teensy string constants stored in flash". The top result was Flash strings and RAM. I think it will answer your questions in greater depth.

But it looks like the executive summary is this: For ARM based Teensy, C-style string constants are stored in Flash and don't occupy any additional RAM.

Did I understand your question?
 
@MichaelMeissner This is more of an Arduino (Mega 2560) question, but I share similar code with a Teensy 3.1/3.2 for other tools.

@markonian In my example I use the char array 'temp' to tag my print data. Since I am changing its contents it cannot be defined as 'const char'.
 
Try it like this:

Code:
  strcpy_P(temp, PSTR("wertyuiopasdfghjklbx"));

Of course, this issue is only on the old 8 bit AVR chips. On newer 32 bit ARM, this is so much easier.
 
Thanks a bunch Paul & Michael.

Paul's answer was spot on.

I'll have to study up on the PROGMEM stuff. I'm an old electrical test guy three years in on a new adventure at a different company. Most of my previous experience was in HP Basic or stumbling thru various *nix environments.
 
Status
Not open for further replies.
Back
Top