Pages: [1]   Go Down
Author Topic: Memory Question  (Read 321 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I've just started writing my menu for my Arduino project. I was wondering what the best way of storing a large amount of text is.
I need 2 large array's of text for my menu. One array has 128 values which are strings with max. length around 30 chars and one with 60 values. I need these lines of text to display menu item options on a LCD screen, so i might change the values to a max length of 16 so it fits one line of my LCD.

Is there a difference in memory size between string and String? Or should I define these as 2 dimensional char array's?
What is the least memory consuming way of storing them? Should I just declare them as a field of my menu class?
Note that these values will never need to change...

Another question I have is this :
I have my sketch, in this sketch I use my custom class Menu. When the user select "save" in the menu, is there a way I can trigger a function in my main sketch from within my Menu class?

Kind regards.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a difference in memory size between string and String?
A String is a class that wraps a string. What do you think the relative sizes will be?

Quote
Or should I define these as 2 dimensional char array's?
That differs from an array of strings how?

Quote
Note that these values will never need to change...
Then a foray into the wonderful world of PROGMEM is in your future (and the only way you will store that volume of data).

Quote
When the user select "save" in the menu, is there a way I can trigger a function in my main sketch from within my Menu class?
The class could have a method to register a callback. That callback would be a function in the sketch.
Logged

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

Quote
Is there a difference in memory size between string and String?
A String is a class that wraps a string. What do you think the relative sizes will be?
I don't know for sure, but what I do know is that there are no stupid questions, but there are stupid answers. Do you expect me to come to some kind of conclusion by telling me that String wraps string? So what is the conclusion then?  "Ofcourse they are bigger because it wraps around a string and adds methods/fields" or "Ofcourse it's the same size cause the base data is the same and only some methods are added" ?
Quote
Or should I define these as 2 dimensional char array's?
That differs from an array of strings how?
I don't know if it differs, maybe because string wraps an 2 dimensional char array and something magically happens (or doesn't), but I wouldn't know that...

Quote
Note that these values will never need to change...
Then a foray into the wonderful world of PROGMEM is in your future (and the only way you will store that volume of data).

Quote
When the user select "save" in the menu, is there a way I can trigger a function in my main sketch from within my Menu class?
The class could have a method to register a callback. That callback would be a function in the sketch.
Thanks for answering these 2 questions, allready found pretty good information about PROGMEM. I'm going to look into that.
Could anybody direct me to a link were they explain the callback functions? It seems to be working kind of like the way delegates do in the realm of C#...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know for sure, but what I do know is that there are no stupid questions, but there are stupid answers. Do you expect me to come to some kind of conclusion by telling me that String wraps string?
Yes, I do. If you can fit a shoe in a box, doesn't that imply that the box needs to be bigger than the shoe?

Quote
"Ofcourse they are bigger because it wraps around a string and adds methods/fields"
Exactly!

Quote
"Ofcourse it's the same size cause the base data is the same and only some methods are added" ?
How could that possibly be true?

Logged

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

Quote
"Ofcourse it's the same size cause the base data is the same and only some methods are added" ?
How could that possibly be true?
If seems possible to me that when you create a class/type , in this case String, you define the extra methods you want only in the source file of the class. You include that source file once, so the methods are defined once.
I would presume when you create an instance of this class, the base data (a field of your class) would still be a string but you can use
the extra methods (that were defined in the String class source file once) on the instance of the String class.

I think it's weird that when you create an instance it stores the methods in the memory again for each instance... the methods don't change...
With a class that has extra fields I understand it's bigger, because for each instance these fields could have a different values... Therefore it needs more space in the memory...
Logged

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

Code:
char *buffer;         // the actual char array
unsigned int capacity;  // the array length minus one (for the '\0')
unsigned int len;       // the String length (not counting the '\0')
unsigned char flags;    // unused, for future features
Is the storage requirement of the String class, plus, of course, whatever "buffer" points to.
(from WString.h")
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.

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

Code:
char *buffer;         // the actual char array
unsigned int capacity;  // the array length minus one (for the '\0')
unsigned int len;       // the String length (not counting the '\0')
unsigned char flags;    // unused, for future features
Is the storage requirement of the String class, plus, of course, whatever "buffer" points to.
(from WString.h")
So, theoretically speaking if the String class would not have these fields, but it does have its methods (subString, Replace, etc...)
It would require just as much memory as the *buffer 's size to store an instance of String?
Sorry if I'm asking annoying questions, but I'm new to c/c++ ...  I've been taught programming with VB6/java/c# and computers that have so much memory all these little differences don't matter.
Logged

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

The manipulation methods exist, but in program memory.
The block I posted is the RAM footprint of a String object.
The two memory spaces are distinct on the Arduino (AVR) processor.
RAM is in very limited supply.
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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, theoretically speaking if the String class would not have these fields, but it does have its methods (subString, Replace, etc...)
It would require just as much memory as the *buffer 's size to store an instance of String?
You could test this yourself.

Code:
char *s = "someText";
String S = "someText";

Serial.print("s size: ");
Serial.println(sizeof(s));

Serial.print("S size: ");
Serial.println(sizeof(S));
Logged

Pages: [1]   Go Up
Jump to: