Pages: [1]   Go Down
Author Topic: 2D dynamic boolean array  (Read 2824 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi,
I want make a 2D dynamic boolean array, but I'm not sure with this code .I need add indexes gradually. example - define array [2][2] and later expand to array [5][3]

Code:
array = ( boolean** )malloc( first_index*sizeof( boolean* ) );
 array[first_index] = ( boolean* )malloc(second_index*sizeof(boolean ) );


--------------
Sorry for my english
« Last Edit: December 31, 2010, 11:47:28 am by kamtar » Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An example of how to allocate and deallocate memory for 2-D arrays.  But: See Footnote.

Code:
void setup()
{
    Serial.begin(9600);
}


int counter = 0;
void loop()
{
    // Pointer to pointer will be used to implement 2-D dynamically allocated array
    int **iarray;

    int nrows = 2;
    int ncols = 3;
    
    // Allocate an "array" of pointers for the rows
    iarray = (int**)malloc(nrows*sizeof(iarray[0]));
    if (!iarray) {
        Serial.print("Couldn't allocate memory for rows.");
        return;
    }


    // For each row, allocate an "array" of ints.
    for (int i = 0; i < nrows; i++) {
        iarray[i] = (int *)malloc(ncols*sizeof(iarray[0][0]));
        if (!iarray[i]) {
            Serial.print("Couldn't allocate memory for row ");
            Serial.println(i);
            return; // Actually you should de-allocate memory for all
                    // of the rows that did get allocated and then
                    // de-allocate memory for iarray.
        }
        for (int j = 0; j < ncols; j++) {
            iarray[i][j] = counter + j + ncols*i; // Put something different in each one
        }
    }
    for (int i = 0; i < nrows; i++) {
        Serial.print("Row ");Serial.print(i);Serial.print(": ");
        for (int j = 0; j < ncols; j++) {
            Serial.print("  ");Serial.print(iarray[i][j]);
        }
        Serial.println();
    }
    Serial.println();

    // De-allocate storage for each of the rows
    for (int i = 0; i < nrows; i++) {
        free(iarray[i]);
    }

    // Deallocate storage for the array of row pointers
    free(iarray);
    // After previous memory has been cleared, you can reallocate with
    // whatever values of nrows and ncols you want (subject to "heap"
    // size limits).
    ++counter;
    delay(1000);
}

Output:

Row 0:   0  1  2
Row 1:   3  4  5

Row 0:   1  2  3
Row 1:   4  5  6

Row 0:   2  3  4
Row 1:   5  6  7

Row 0:   3  4  5
Row 1:   6  7  8

Row 0:   4  5  6
Row 1:   7  8  9
.
.
.


This is just an illustration of how to do it.  In your loop() you would deallocate the storage then assign new values to nrows and ncols and go through the allocation steps again.  I might even write a function to allocate storage for nrows and ncols and another function to deallocate them.  I'll leave that up to you.



Regards,

Dave
Footnote:
This example is (I believe) perfectly valid C and C++ code.

However...

I am very reluctant to use dynamic memory allocation in any embedded system that I expect to run forever.  Why do I have misgivings?  Lack of garbage collection in the standard library function malloc(). That's why.

It is possible to allocate and deallocate memory  (repeatedly) in such a way that there is enough memory in the heap to do the job but it is fragmented so that that not enough contiguous memory is available to finish it.

I mean, if the application is supposed to do something and quit, that's one thing, but if it is supposed to run forever and you don't know ahead of time how much memory you actually need, then, you are really, really (really) asking for trouble.  For simple cases, you might convince yourself that such fragmentation will not occur, but...

That's just an opinion (my opinion), and it's worth exactly what you want it to be worth.
« Last Edit: December 31, 2010, 12:44:31 pm by davekw7x » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I programing a robot to "High school competition".His task is mapped a "room". He use array to save information about room as square site.
False - square is clear True - is there a block
The informatioun about squares will be save in array under indexes
  • [y] but I must used negative coordinates.
I use 4-quadrants this means 4x 2D array...

I have idea use one array with offset zero because if I use array [100][100] and as zero is 50.The "room" can be long 7,5m*2 (one square is 15cm)  which is enough But I'd solved it universally and more elegant.

Again, sorry for the English, perhaps it will possible to understand smiley-grin
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
array [100][100]
But a 100x100 array has 10,000 elements.  That won't fit into any of the chips used in Arduino systems.

Sorry.


Regards,

Dave
« Last Edit: December 31, 2010, 01:45:50 pm by davekw7x » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ou epic fail xD
so 36x36
zero is 18
and this is 2,7m on every side
I test robot with this and later...
« Last Edit: December 31, 2010, 02:03:22 pm by kamtar » Logged

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

A boolean still has 8 bits.

http://arduino.cc/en/Reference/BooleanVariables
Quote
(Each boolean variable occupies one byte of memory.)

That boolean has enough room to hold information about 8 squares. Proper indexing would allow you to store information about the room in a 1D array, and in less space, if stored bit-by-bit.
Logged

Greenwood, Indiana
Offline Offline
God Member
*****
Karma: 0
Posts: 508
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://en.wikipedia.org/wiki/Bit_array
http://en.wikipedia.org/wiki/Bitfield

http://michael.dipperstein.com/bitlibs/index.html
http://michael.dipperstein.com/bitlibs/#bitarray

The bitarray library in the last two links might be worthwhile to look at the source code, it's not an Arduino library.
« Last Edit: December 31, 2010, 02:53:24 pm by mrmeval » Logged

If it was designed by man it can be repaired by man.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote

I am very reluctant to use dynamic memory allocation in any embedded system that I expect to run forever.  Why do I have misgivings?  Lack of garbage collection in the standard library function malloc(). That's why.

It is possible to allocate and deallocate memory  (repeatedly) in such a way that there is enough memory in the heap to do the job but it is fragmented so that that not enough contiguous memory is available to finish it.

I mean, if the application is supposed to do something and quit, that's one thing, but if it is supposed to run forever and you don't know ahead of time how much memory you actually need, then, you are really, really (really) asking for trouble.  For simple cases, you might convince yourself that such fragmentation will not occur, but...

That's just an opinion (my opinion), and it's worth exactly what you want it to be worth.

not to mention that even if you did everything right it is still really tough to figure out just how much memory you are allocating.  And if you do know, why does it need to be dynamic?


Quote
[The robot's] task is mapped a "room". He use array to save information about room as square site.


They make SD card shields for arduino and it sounds like you may need to store a lot of data (>8k).  It may be worth it to think of a linear data format that can be parsed later and store it to an SD card since they are cheap, easy, and give virtually unlimited storage.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No I didnt use arduino board. I made own board but I can use bluetooh for send data to PC or any device ... I use this robot in "test arena" c. 4x4square and for test the functionality of the algorithm in larger areas can be used bluetooh.
« Last Edit: December 31, 2010, 04:35:41 pm by kamtar » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ah i see.  In that case you can go to town with malloc() (in the pc's code). ;D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I searched and found this http://arduiniana.org/libraries/flash/ can I use it to solved my problem? is it usable solution?
« Last Edit: January 01, 2011, 09:16:16 am by kamtar » Logged

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

Quote
can I use it to solved my problem?
If the array is static (a fixed size), yes.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

c. 15kB is enought I can presume define large static array . for my demonstration is it good.For the function of the algorithm in the larger areas I will use the Bluetooth.
Thanx
« Last Edit: January 01, 2011, 10:52:39 am by kamtar » Logged

Pages: [1]   Go Up
Jump to: