create a class which creates an array[a,b] of arbitrary sizes

hi,
I want to create a class which creates an array[a,b] of arbitrary sizes by the constructor.

I now found a way to create a 1-dim array by assert, but I always fail by trying to create a 2-dim array.

exit status 1
array size in operator new must be constant

Which would be the correct way to do that?

#include <cassert>
 
class tMenu {
  
  protected:         
        char * list;
        char MENULEN, CAPTLEN;      
  
  public:      
    
     tMenu (char menulen, char captlen) // constructor
     {
        assert(menulen > 0); 
        assert(captlen > 0); 
        list = new char[menulen];            // works fine!
    //  list = new char[menulen][captlen];   // alternat. >> error!
        MENULEN = menulen;
        CAPTLEN = captlen;
     }      

 
     ~tMenu() // destructor
     {
       // Dynamically delete the array we allocated earlier
       delete[] list ;
     }

};

It is unusual to use char for a number. What does it do if you make the two lengths bytes?

You could use malloc() to allocate a memory area of variable size instead of asking the compiler to do it - it expects a fixed size.

changing char to int doesn't make it possible either. Did it work for you when you tried it? What would be the correct way for using malloc? (which also would work for the destructor then respectively?)

I didn't try it. I'm typing on my phone here.

The best way would be to look up an online reference for malloc().

Arduino classes rarely have destructors. You don't want highly dynamic memory usage because it is too easy to run out of memory. You didn't say, but usually an Arduino program will start up a bunch of global objects that exist for the life of the program.

thanks, but I actually would like to have a way to delete an object during runtime.

Perhaps another user knows a way which is supposed to be able to create 2-dim arrays in objects dynamically?

MorganS:
It is unusual to use char for a number.

Could be handy, if you want a negative number of elements in your array.

AWOL: Could be handy, if you want a negative number of elements in your array.

no, on my ARM mcu char always is unsigned char. I also might have used byte alternatively. But CMIIW: wouldn't have int also negative values arbitrarily?

Anyway, please return to the topic question: Who knows a way which is supposed to be able to create 2-dim arrays in objects dynamically?

I also might have used byte alternatively.

But in Java byte is signed. Best to make it unambiguous.

now come on: we're talking about Arduino which uses C++, and to Arduino char is unsigned for ARM and signed for AVR mcus, and for Arduino byte is always unsigned char. So what? And in which respect does that solve the problem?

again, please: Who knows a way which is supposed to be able to create 2-dim arrays in objects dynamically?

now come on: we're talking about Arduino,

And on AVR Arduinos, char is signed.

Now, come on.

what did I say? "and to Arduino char is unsigned for ARM and signed for AVR mcus"

again, please: Who knows a way which is supposed to be able to create 2-dim arrays in objects dynamically?

on my ARM mcu

Which ARM mcu?

Pete

it's irrelevant. the question is:

Who knows a way which is supposed to be able to create 2-dim arrays in objects dynamically?

I said "rarely". You can write a destructor if you want.

What happened when you tried it?

char *list;
list = malloc(length * width * sizeof(list[0]));

it’s irrelevant

I was trying to help but apparently you know what is relevant and what isn’t, so I’ll leave you to muddle along on your own.
Welcome to my $h1tl1$t.

Pete

For EVERY 32bit ARM and every ESP cpu (M0, M3, M4, esp8266) char will be unsigned by gpp, irrelevant which one I have (I have all of them, but the class has also to be compatible later on).

MorganS: I said "rarely". You can write a destructor if you want.

What happened when you tried it?

char *list;
list = malloc(length * width * sizeof(list[0]));

How can I make a 2-dim array list[length][width]then out of that (without memcpy of course)?

a 2-dim array list[length][width]

Is it an Iliffe vector you're looking for?

I don't know what you mean by lliffe, ftm it will be used as an array of cstrings, e.g. this->char list[10][20]={"line0", "line1", "line2", "line3",..., "line9"}; Serial.println(this->list[k]);

But actually the problem is more a general one, even for creating 2-dim (or perhaps 3-dim) arrays o different variant types for different classes for different purposes. I don't see the general method how to create multiple-dim arrays dynamically e.g. in an object)

I don't know what you mean by lliffe

Ah! Too lazy to Google, huh?

I actually wrote what it's for:

dsyleixa: ftm it will be used as an array of cstrings, e.g. this->char list[10][20]={"line0", "line1", "line2", "line3",..., "line9"}; Serial.println(this->list[k]);

But actually the problem is more a general one, even for creating 2-dim (or perhaps 3-dim) arrays o different variant types for different classes for different purposes. I don't see the general method how to create multiple-dim arrays dynamically e.g. in an object)