Pages: [1] 2 3   Go Down
Author Topic: Linked list tree and dynamic pointers.  (Read 2244 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
« Last Edit: February 16, 2014, 10:17:50 pm by syphex » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 
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.
« Last Edit: February 17, 2014, 03:03:12 pm by syphex » Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The compiler wants to know where everything in SRAM is located. You are free to create static buffers, or use malloc(), but you won't get very far if you create a pointer to a random location in SRAM and start putting data there.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So if I create menu objects then assign the pointers stored in them to other menu objects without using Malloc then they will eventually get overwritten?

The Arduino now has a new function.
So new and delete are now automatically included in 1.05-r2?

The only pointer that changes is the current_menu pointer, which is supposed to point to a menu object, and access the other object(s) being pointed to by that object (instances?). The objects pointed to by the array itself don't change but some of the variables inside them might.

They don't need to change at runtime, but how can I initialize different size arrays for different objects of the same/derived class?
« Last Edit: February 18, 2014, 01:07:28 am by syphex » Logged

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

Quote
but how can I initialize different size arrays for different objects of the same/derived class?
malloc() or new.
Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you allocating these menu objects? As long as you don't assign them to random locations in SRAM, you are fine.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
ukash kart
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your solution
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you allocating these menu objects? As long as you don't assign them to random locations in SRAM, you are fine.

Does the new keyword solve this problem?

Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A static buffer is fine, too.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
class menu{
 
  public:
  char name[10];
  byte xpos;
  byte ypos;
 // menu** menulist;
  menu* previous_menu;
  double value;
 
    menu(char *mname, byte y, byte x){
     
      menu** menulist = new menu*[y];
      for (byte i=0; i<y; i++)
      {
        menulist**[i] = new menu*[x];
      }
     
    strcpy(name, mname);
  }
 
 /* ~menu()
  {
    delete [] menulist;
  }*/
};

I am having trouble with the line menulist**[y] = new menu*[x]; . It keeps saying: expected primary-expression before'[' token.

If I call a different size [x] for each of [y] can each array in y be a different size?
« Last Edit: February 22, 2014, 07:22:37 am by syphex » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so they need to be constructed on allocation.. Since my class has pointers to its own members at the moment this wont really work. How can I initialise the pointers to menu objects without constructing them first? I thought it only allocates the memory without assigning it first "its up to me what I put in there".

Ideally I'd like to initiate all the menu objects/pointers and then do something like:

Code:
EC_PH.menulist[][]={
{this->menu_previous, EC, PH},
{DOSING, CALIBRATION}
}
where the menu index[0][0] is for the previous menu

That would be a neat way of showing all the menu's inside a menu, and also give an idea of their layout.
« Last Edit: February 24, 2014, 10:59:02 pm by syphex » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
class menu{
 
  public:
  char name[10];
  byte xpos;
  byte ypos;
  //char menuname[10];
  menu** menulist;
  menu* previous_menu;
  double value;
 
    menu(char *mname="", byte y=0, byte x=0){
     
      menulist = new menu*[y];
     
    strcpy(name, mname);
  }
 
 /* ~menu()
  {
    delete [] menulist;
  }*/
};

menu EC_PH("EC PH",1,3);
menu EC("EC ONLY",0,0);

menu* current_menu=&EC_PH;

void setup(){
  Serial.begin(9800);
  Serial.println("STARTING");
  EC_PH.menulist[6]=&EC;
  Serial.println((*current_menu->menulist[6]).name);
}

void loop(){
}

Why does it allow me to assign and dereference an array in position 6 in allocated memory when the array is supposed to be of size 1? Is this just overwriting memory which hasent been dynamically allocated?
« Last Edit: February 28, 2014, 09:24:46 pm by syphex » Logged

Pages: [1] 2 3   Go Up
Jump to: