Notifications
Clear all

Out of memory?  

Page 2 / 2

tperry724
(@tperry724)
Eminent Member
Joined: 3 months ago
Posts: 33
Topic starter  

Would I use somehting like this?  Am I on the right track?

int strcmp_P (const char*, const char*)


ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 2 years ago
Posts: 670
 

@tperry724

Posted by: @tperry724

Would I use somehting like this?  Am I on the right track?

int strcmp_P (const char*, const char*)

That's not the right 'C' function name, so not sure where you got it... nevertheless, here is the correct one, with an example of how to use it:

#include <string.h>

if(strcmp(birthdays[index].name, "name") == 0){
  Serial.println("Match");
 } else {
  Serial.println("No Match");
 }

[edit]

I forgot to mention that F() is a preprocessor macro, and that PROGMEM only works if the variables are globally defined.

Cheers.


Sid and tperry724 liked
ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 2 years ago
Posts: 670
 

@tperry724

The following is very wasteful, and I spoke about the automatic length handled by using const char* in my previous post.

char name[21];

...this can make a massive difference in memory usage, for example:

I recreated your struct version with just 1 person array entry, and here are the results:

Sketch uses 1550 bytes (4%) of program storage space. Maximum is 32256 bytes.
Global variables use 212 bytes (10%) of dynamic memory, leaving 1836 bytes for
local variables. Maximum is 2048 bytes.

Then I simply replaced char name[21]; with const char* name;, and got the following results:

Sketch uses 1486 bytes (4%) of program storage space. Maximum is 32256 bytes.
Global variables use 200 bytes (9%) of dynamic memory, leaving 1848 bytes for
local variables. Maximum is 2048 bytes.

Savings summary:
1550 - 1486 = 64 bytes * 92 = 5,888 bytes difference in program storage space
212 - 200 = 12 * 92 = 1,104 bytes difference in dynamic memory space

Cheers.


Sid and tperry724 liked
ReplyQuote
Sid
 Sid
(@sid)
Trusted Member
Joined: 3 months ago
Posts: 68
 
Posted by: @frogandtoad

but if the number is larger, you can have less elements, but not more than what you specified.

So if I have MAX_ELEMENTS 6, I can have an array like {"N", "2", "4"} - if so, this is something I have been unaware of. I always thought and used {"N", "2", "4", ' ', ' ', ' '} to create those blank elements - so that the numbers would match up.

Posted by: @frogandtoad

macros do not use up any extra memory space

Well, I agree, macros do not use up any extra memory (from the regular RAM) and also that it increases the ease of Management of codes. But then, wont this number (4 in the example) be needed to be placed before the compilation or in any other stage to substitute the MAX_ELEMENT's actual occurrence in the codes? - I think somewhere the codes would definitely need to be adjusted to read (difficult to explain for me) array[][4] (that is the declaration of array[][MAX_ELEMENTS] or even the loop - for(ctr=0; ctr<MAX_ELEMENTS; ctr++) to be interpreted as - for(ctr=0;ctr<4;ctr++)

PS: Sorry, I have no intentions of altering what the thread was supposed to discuss, but I know, if I ask it elsewhere, I might not be able to discuss it out or have my confusion clarified.

Life is exploring and learning


tperry724 liked
ReplyQuote
Sid
 Sid
(@sid)
Trusted Member
Joined: 3 months ago
Posts: 68
 
Posted by: @tperry724

{ "Patricia C. ", 1, 2, 1 }, // Name, Month, Day, 1=Bday 2=Anniversary 3=RIP
{ "Chris H. ", 1, 4, 1 },
{ "Ann W. ", 1, 7, 3 },

I understand that this extra space at the end of every name is to be used to make the display easier (you know, separate the previous word from the next with a space in between). But adding an extra space to every element in the array would use more bytes (1 per space to be precise).

How about storing that space in one variable and using it anywhere you need a space to be printed? You can save some useful bytes for sure.

I am not good with Arduino codes related to LCD - as I have never programmed one yet, so I am not the right person to comment on that aspect of your code yet.

Thank you for extending yourself to learn about structures, switch case and experimenting with those. You are on your way to be a good programmer. 🙂

Life is exploring and learning


tperry724 liked
ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 2 years ago
Posts: 670
 

@sid

Posted by: @frogandtoad
Posted by: @frogandtoad

but if the number is larger, you can have less elements, but not more than what you specified.

So if I have MAX_ELEMENTS 6, I can have an array like {"N", "2", "4"} - if so, this is something I have been unaware of. I always thought and used {"N", "2", "4", ' ', ' ', ' '} to create those blank elements - so that the numbers would match up.

That's correct... you don't need to populate all the memory slots, in the same way you don't have to always populate a simple character array to it's full extent, for example:

char name[30] = {"foo"};

We've allowed for a name to be up to 30 bytes long, but doesn't mean we have to use it all up, which can be quite wasteful for that reason, because not all peoples names are the same length, so it's much better to do as I said previously:

char name[] = {"foo"};

The compiler calculates the length for you, and saves the allocation of additional bytes.

The latter version is far better, and allows the sizeof operator to properly provide you with the actual length of the string instead of how many you allocated.

Cheers.


Sid liked
ReplyQuote
Sid
 Sid
(@sid)
Trusted Member
Joined: 3 months ago
Posts: 68
 
Posted by: @frogandtoad

That's correct... you don't need to populate all the memory slots, in the same way you don't have to always populate a simple character array to it's full extent, for example:

char name[30] = {"foo"};

This is one reason I have always loved such forums where - members do not mind when asked and are always up for a much needed help. As I mentioned, I never had realized that this was possible with 2-D or multi Dimension arrays. Have used the name[30]="foo" many a times, but never ever thought of applying the same to the populating of memory slots.

Thanks again mate for all your time to clarify my questions. 🙂 Much appreciated.

Life is exploring and learning


tperry724 liked
ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 2 years ago
Posts: 670
 

@sid

Posted by: @frogandtoad
Posted by: @frogandtoad

That's correct... you don't need to populate all the memory slots, in the same way you don't have to always populate a simple character array to it's full extent, for example:

char name[30] = {"foo"};

This is one reason I have always loved such forums where - members do not mind when asked and are always up for a much needed help. As I mentioned, I never had realized that this was possible with 2-D or multi Dimension arrays. Have used the name[30]="foo" many a times, but never ever thought of applying the same to the populating of memory slots.

Thanks again mate for all your time to clarify my questions. 🙂 Much appreciated.

You're welcome... I'm more than happy to help where I can.

Cheers.


Sid liked
ReplyQuote
Page 2 / 2