Go Down

Topic: Linked list tree and dynamic pointers. (Read 2 times) previous topic - next topic

syphex

I am trying to create a menu with adafruits 16x2 RGB LCD keypad shield (5 buttons). I have heard of "linked lists", but would like to access multiple menu's from one screen, instead of a linear list. The max number of menu's per screen can be 6, 3 on each row. My question is if I use a multidimensional array of size [2][3], is it bad to just set them all to NULL and only use the ones I need? I have heard of issues with dynamic allocation of arrays in structs/classes due to lack of "new" and have no experience with MALLOC.

Here is my class so far:

Code: [Select]
class menu
{
   public:
   char name[10];
   unsigned int xpos;// position on the screen
   unsigned int ypos;
   menu* menulist[2][3]; //array pointer to group members
   menu* previous_menu; //pointer to previous menu
   float value;
   
   menu(char *myname,int x1,int y1)
   {
   strcpy(name, myname);
   xpos=x1;
   ypos=y1;
   }
   
};

PaulS

Quote
I have heard of issues with dynamic allocation of arrays in structs/classes due to lack of "new"

The Arduino now has a new function.

Quote
is it bad to just set them all to NULL and only use the ones I need?

Of course not.

syphex

#2
Feb 17, 2014, 04:10 am Last Edit: Feb 17, 2014, 04:17 am by syphex Reason: 1
How would I go about generating the array of pointers dynamically?

I know it better practice to make variables private, but in this case I cannot see why limiting the scope of the variables provides any benefit. I want to be able to set the lcd cursor to
Code: [Select]
current_menu.menulist[i][p].xpos etc.. Otherwise I have to write a seperate function for each get_name, get_pos, get menulist, get value etc... Am I missing something?

Why do many people choose to avoid using SRAM for an array of pointers in a class such as this? What is the difference between MALLOC and PROGMEM and what is actually being stored? The address's of the pointers or something else?

I have a mega so much more memory, is it still worth saving the SRAM? Doesn't saving and retrieving information from the Flash memory take more time? Especially if it is constantly being displayed on the LCD?

Graynomad

A lot of questions there, I'll respond to one or two of them

Quote
What is the difference between MALLOC and PROGMEM and what is actually being stored?

Malloc() is a function that "grabs" a section of SRAM for you, PROGMEM is a macro that forces data to stay in flash memory and not get copied to SRAM.

When you malloc() some SRAM memory you get a pointer to that memory, what you put in there is up to you.

Quote
Doesn't saving and retrieving information from the Flash memory take more time?

Yes, but not enough to worry you in this case I think, the LCD is a 1000x slower anyway.

But if you have static data in flash memory why would you need to dynamically allocate SRAM with malloc()? Does the data change at run time?

_______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

syphex

#4
Feb 17, 2014, 08:42 pm Last Edit: Feb 17, 2014, 09:03 pm by syphex Reason: 1

A lot of questions there, I'll respond to one or two of them

Malloc() is a function that "grabs" a section of SRAM for you, PROGMEM is a macro that forces data to stay in flash memory and not get copied to SRAM.

When you malloc() some SRAM memory you get a pointer to that memory, what you put in there is up to you.

Yes, but not enough to worry you in this case I think, the LCD is a 1000x slower anyway.

But if you have static data in flash memory why would you need to dynamically allocate SRAM with malloc()? Does the data change at run time?



I don't have data in flash memory yet, I'm trying to figure out if I need it and if so, how to go about it. Is it if you never change the variable being pointed at, and only read it, then its a viable option for PROGMEM in this case?

As for MALLOC. Why do you need to "grab" a section of SRAM? Isn't it available anyway? Or is it to prevent it being overwritten by other variables?

The problem is that at least one menu will have 6 items, but the rest are likely to have 2 or so. I need help initializing a multidimensional array in a class of max size [2][3].

I can't do:
Code: [Select]
 
int size;
 menu* menulist[2][size];
So how do I do it? Or I don't need to? Just seems a waste to have that [2][3] declaration if im only using the max size once.

Go Up