is possible somehow declare global array of string or int?

Hello, I will use global array, because i read data file, then use the values. Because data file has a lot columns with different type of value, I am using more arrays - each of them for specific columns.

In setup is all OK. But I want to use it outside in all files all tabs…

Thank you.

so my code is:

int *aRoutesMotor1; // array
int iRoutesCount; // count of record in data file

void setup() {

iRoutesCount = GetRoutesCsvFileRecordsCount(); // function, which return int.. the count of records.. because data file can be changed..
int aRoutesMotor1[iRoutesCount];

GetRecords(aRoutesMotor1);   // this puts records in to array

for (int i = 0; i <= iRoutesCount-1; i++) {
      Serial.print(" aRoutesMotor1 : -" + String(aRoutesMotor1[i]) + "-"); // in setup is this working fine      
    }
}


void loop() {
for (int i = 0; i <= iRoutesCount-1; i++) {
      Serial.print(" aRoutesMotor1 : -" + String(aRoutesMotor1[i]) + "-"); // but here NOT..    
    }
}

You have two identifiers named aRoutesMotor1. The global one and the one in the scope of setup(). They are not the same except in name.

int *aRoutesMotor1; // array

That is NOT an array. That is a pointer. You may, or may not, actually make that pointer point to some contiguous memory locations that you can then treat as an array. Ditch the incorrect comment.

      Serial.print(" aRoutesMotor1 : -" + String(aRoutesMotor1[i]) + "-"); // in setup is this working fine

Quit being so f**king lazy. It is not that much more effort to type 3 Serial.print() calls, and not piss away memory uselessly.

When you say it works fine here but not there, what does it actually do? I don't see where you've given either the pointer named aRoutesMotor1 nor the array local to setup with the same name any values. So what do you expect it to print?

outside setup is returning rubbish… Without end.

Yes it is possible to create global arrays. Just define then before setup().

int myArray[12];
char myChars[32];

...R

Solution found:
when I want to declare array and use it everywhere then I need “new”

aRoutesMotor1 = new int [iRoutesCount];

updated code is here:

int *aRoutesMotor1; // array
int iRoutesCount; // count of record in data file

void setup() {

iRoutesCount = GetRoutesCsvFileRecordsCount(); // function, which return int.. the count of records.. because data file can be changed..
 aRoutesMotor1 = new int [iRoutesCount];

GetRecords(aRoutesMotor1);   // this puts records in to array

for (int i = 0; i <= iRoutesCount-1; i++) {
      Serial.print(" aRoutesMotor1 : -" + String(aRoutesMotor1[i]) + "-"); // in setup is this working fine      
    }
}


void loop() {
for (int i = 0; i <= iRoutesCount-1; i++) {
      Serial.print(" aRoutesMotor1 : -" + String(aRoutesMotor1[i]) + "-"); // Now the same result like in setup loop... NICE We have dynamic array set in one procedure and then used in another.. Including other files..    
    }
}

vfp:
Solution found:
when I want to declare array and use it everywhere then I need "new"

That's a rubbish solution. Look at reply #5. It's what we've been doing since 1969.

It's what we've been doing since 1969.

Which works fine if you know how many elements to create, or don't mind statically allocating more space than is needed.

If you have a variable number of records, and need to use the least amount of memory, new/malloc is the way to go.

Okay, I didn’t read carefully… yes for variable length it is. I’ve been answering too many “shooting fish in a barrel” posts.

PaulS:
If you have a variable number of records, and need to use the least amount of memory, new/malloc is the way to go.

Does using dynamic allocation really buy you anything in this case? You need to be able to handle the largest possible number of records anyway, so why not just reserve it statically? I only see dynamic as helpful if other parts of the code also use dynamic allocation and everyone free()'s the space as soon as they're done with it. That way a memory-hungry application could get by with limited RAM.

Given what I've seen so far, I really doubt that this application is sophisticated enough for that technique. And, of course, it brings you up against less-than-ideal garbage collection. Finally, OP is using String class for God's sake. I'd say get rids of Strings and go with static allocation. Only go dynamic if there's a good reason as mentioned above.

If you really don't know the maximum number of records, you need an exception handler. That's the case no matter whether you pre-allocate a fixed size buffer, or rely on dynamic allocation. If you have multiple data vectors, dynamic allocation is better because it can allocate to one or the other vector depending on demand (because it can allocate atomically).

aarg:
If you really don't know the maximum number of records, you need an exception handler. That's the case no matter whether you pre-allocate a fixed size buffer, or rely on dynamic allocation. If you have multiple data vectors, dynamic allocation is better because it can allocate to one or the other vector depending on demand (because it can allocate atomically).

All true, if the memory is properly released. As I said, I from what I've seen of the code, I don't see that happening in this case. Nor do I see an exception handler for when 'new' returns NULL.

gfvalvo:
All true, if the memory is properly released. As I said, I from what I’ve seen of the code, I don’t see that happening in this case. Nor do I see an exception handler for when ‘new’ returns NULL.

Nobody of you shows me examples. You are speaking about some release, about RAM hungry… OK. nice, but Where I can get more information?

When I start using Arduino two months ago… I get the information, that is based on Wirign program language. Reference is here on this site… OK nice. But now it sounds, that is used clasic C++. So then I need learn it. Buy some book and start learning on my desktop first.

Because no everybody become from C or C++ environments, then don’t know, what is difference. For example I was PHP, .Net, VB programmer.

Sorry for my stupid questions. Many thanks fro your help. But what will be nice to have here = some examples.

Yes, I need array available on whole code… Because there can be a lot records, all of them are important, no reason to read and parse this big file again and again. And it’s allows me to use simple index… So I know on which line I am.

Again thank you.

Please remove information, that Arduino use Wiring…

vfp:
Nobody of you shows me examples.

Reply #5 ?

Using dynamic memory allocation in the small memory of an Arduino is very likely to lead to a program crash unless the programmer is very experienced.

...R

vfp:
Please remove information, that Arduino use Wiring..

Why? Arduino does use Wiring. Wiring is the platform. C++ is the language. What's the issue?