Pages: [1] 2   Go Down
Author Topic: how to make public variables declared in setup()  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26634
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I come from a Java background
it'll take a while, but we can help you.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

:-P any ideas how to fix my issue?
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26634
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void dSize() {
     length++;
  }

the array would need to be:

Code:
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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple example using an int array.

Code:
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
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple example using an int array.

Code:
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:
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
}
?
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I use the method listed above, I get these error messages. 
the code:
Code:
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'
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26634
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mismatched brackets
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

maybe you can try for a 2-d char array instead
Code:
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.
Logged

Pages: [1] 2   Go Up
Jump to: