Go Down

Topic: how to make public variables declared in setup() (Read 636 times) previous topic - next topic

Gatt427

I come from a Java background and I am used to being able to make a public variable in a subfunction like so: (with Java syntax, this is purely theoretical)

Code: [Select]

void loop(){
    public String hello = "Hi World";
}


Is this possible in Arduino C++?  I am trying to define an array size without actually knowing the size.  In order to know the size I need to run a different function which would calculate the files to be in the array prior to creation.  (I have that, I just dont know how to change the size of a public array.)  There may be an even easyer way that i'm not even thinking of.  If there is, please say so.

AWOL

Quote
I come from a Java background
it'll take a while, but we can help you.
"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.


Arrch

If you want a dynamically sized array, then you'll need to use malloc(). If you want to re-size it, then you'll need free() as well, but there is currently a bug with free() right now that can lead to memory leakage. I think it's fixed in 1.0.4; someone correct me if I'm wrong. It sounds like you are only sizing the array once, so a single malloc() call should suffice.

AWOL

Quote
In order to know the size I need to run a different function which would calculate the files to be in the array prior to creation.
you need to explain that sentence.
"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.

Gatt427

I have a different function.  it basically runs thru the array without saving it.  just incrementing all the values to figure out how many there are.  That value is stored as the int I need to be the array size.  lets call it length. 

Code: [Select]

void dSize() {
     length++;
  }


the array would need to be:

Code: [Select]

String files[length]; 


but because files[] is used in multiple subfunctions, it needs to be public, or I need to find a way to share its data.

Arrch

Simple example using an int array.

Code: [Select]
int *myArray; // pointer to an int

void setup()
{
   int size = getSizeForMyArray(); // determine the size of the array
   myArray = (int*) malloc(size * sizeof(int)); // allocate the space on the heap.
}

vod loop()
{
  // do stuff with my array
}

Gatt427


Simple example using an int array.

Code: [Select]
int *myArray; // pointer to an int

void setup()
{
   int size = getSizeForMyArray(); // determine the size of the array
   myArray = (int*) malloc(size * sizeof(int)); // allocate the space on the heap.
}

vod loop()
{
  // do stuff with my array
}



Thats exactially what I need!
so to do that with a String array I world do:

Code: [Select]
String *myArray; // pointer to an int

void setup()
{
   int size = getSizeForMyArray(); // determine the size of the array
   myArray = (String*) malloc(size * sizeof(String)); // allocate the space on the heap.
}

vod loop()
{
  // do stuff with my array
}

?

Arrch

I don't recommend using Strings; they have numerous documented (on these forums, at least) issues.

In this particular instance, you're also going to have issues with determining the size of a single String, as they are intended to be dynamically sized.

Gatt427

I need String arrays to hold the title of the songs I am storing.  Those strings are used directly by a library to print to my lcd.

Gatt427

When I use the method listed above, I get these error messages. 
the code:
Code: [Select]

songs = (String*)malloc(length * sizeof(String);


error:
jukebox.ino: In function 'void setup()':
jukebox:36: error: expected primary-expression before '*' token
jukebox:36: error: expected primary-expression before ')' token
jukebox:36: error: expected `;' before 'malloc'

AWOL

"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.

Gatt427

LOLLLOLOLOL thanks for catching my complete fail... I should probably take a nap now...

Gatt427

ok... that previous fix compiled.  But it diddnt make an array.  I need an array of Strings, not a String of x length.

mistergreen

maybe you can try for a 2-d char array instead
Code: [Select]
char songs[3][20] = { {"song a"}, {"song b"}, {"song c"} };

Song title is limited to 20 characters. You can use the malloc to add new songs titles.

Go Up