0

I'm with few questions about how to implement a function for adding a byte matrix to some "pattern" structure vector.

Here is my code structure:

struct pattern<br>
{
    byte** map;
    int size;
};

struct pattern* pvec;
int patterns = 0;

void add_pattern(byte** map, int size)
{
    struct pattern p;
    int i;

    p.size = size;

    p.map = (byte**) malloc(p.size * sizeof(byte*));
    for (i = 0; i < size; i++)
        p.map[i] = (byte*) malloc(4 * sizeof(byte));

    p.map = map;

    pvec[patterns] = p;
    patterns++;
}

And an example of a byte matrix:

{
    {B1000,B0000,B0000,B0000},
    {B0100,B0000,B0000,B0000},
    {B0010,B0000,B0000,B0000},
    {B0001,B0000,B0000,B0000},
    {B0000,B0001,B0000,B0000},
    {B0000,B0000,B0001,B0000},
    {B0000,B0000,B0000,B0001},
    {B0000,B0000,B0000,B0010},
    {B0000,B0000,B0000,B0100},
    {B0000,B0000,B0000,B1000},
    {B0000,B0000,B1000,B0000},
    {B0000,B1000,B0000,B0000}
}

But I think this isn't a good way to do that, because I don't know exactly how to send this byte** argument to the function, considering that I have some patterns with few "rows of bytes" than others.

How can I do this in another way? Maybe sending a pre-built pattern structure as an argument?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramon Saraiva
  • 518
  • 1
  • 5
  • 14
  • `byte` is not a standard type in C, consider using `uint8_t` if your compiler is recent enough. Also, [don't cast the return value of `malloc()` and friends](http://stackoverflow.com/a/605858/28169). – unwind Mar 18 '13 at 16:54
  • I think you would be best to stick to rectangular blocks. You can provide **width** and **height** arguments to your function. – grahamj42 Mar 18 '13 at 19:59
  • @unwind: Actually, the cast is necessary, since in C++ void * are not automagically promoted, and Arduino sketches are C++. And `byte` **is** a defined type in Arduino sketches: this is the standard way of treating `uint8_t` in Arduino. – angelatlarge Mar 19 '13 at 01:29
  • How about coding a class that manages its own memory (i.e. malloc()s at construction time and free()s inside the destructor) and also remembers its own size ? – Marcello Romani May 20 '13 at 12:21
  • Also, what does size represent ? Number of rows ? Number of columns ? Number of significant bits ? – Marcello Romani May 20 '13 at 12:23

0 Answers0