Pages: [1]   Go Down
Author Topic: Dynamic matrix  (Read 1321 times)
0 Members and 1 Guest are viewing this topic.
Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I have written below code for dynamic matrix, and it has got an error compiling
C:\Users\kostas\AppData\Local\Temp\build6344196874205377199.tmp/dynamic_matrix.cpp:83: undefined reference to `operator new[](unsigned int)'
C:\Users\kostas\AppData\Local\Temp\build6344196874205377199.tmp/dynamic_matrix.cpp:86: undefined reference to `operator new[](unsigned int)'

 and I don’t understand, that is the wrong?
Code:


#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 6, 7);

int **matrix;
int m;  //col
int n;  //row

/////////////////////////DILOSI METAVLITON GIA TO PLIKTROLOGIO
int analogInputPin=0;
int totalOutputDigit=9;
int KeyForEnder='D';
int keyForClear='C';
int rowToPrint=0;
int columnToPrint=0;
int keyboardMode='mod0';
int IncomingString;
///////////////////////////DILOSI METAVLITON GIA TO PLIKTROLOGIO



void printmatrix(int **matrix, int m, int n);
void readmatrix(int **matrix, int m,int n);


void setup()
{
  Serial.begin(1200);
  lcd.begin(6,2);


}
void loop()
{
  rowToPrint=15;
  columnToPrint=1;
  lcd.print("row");
   n=getPressString(IncomingString,analogInputPin,KeyForEnder,keyForClear,rowToPrint,columnToPrint,keyboardMode);
  Serial.print("number row:");
  Serial.print(n);
  lcd.clear();
  lcd.print("collumn");
   m=getPressString(IncomingString,analogInputPin,KeyForEnder,keyForClear,rowToPrint,columnToPrint,keyboardMode);
  Serial.print("number col:");
  Serial.print(m);
  lcd.clear();
 
  matrix= new int*[n];
  for(int i=0; i<n; i++)
  {
    matrix[i]=new int[m];
  }
 
  readmatrix(matrix, m, n);
  printmatrix(matrix, m, n);

halt:
  goto halt;
}


void readmatrix(int **matrix, int m,int n)
{
  int i,j;
  int rowToPrint=15;
  int columnToPrint=1;
  for(i=0; i<=n; i++)
  {
    for(j=0; j<=m; j++)
    {
      lcd.print("stoixeio ");
      lcd.print(i);
      lcd.print(j);
      matrix[i][j]=random(200);
      Serial.print(matrix[i][j]);
    } 
  } 
}


void printmatrix(int **matrix, int m, int n)
{
  int i,j;

  for(i=0; i<=n; i++)
  {
    for(j=0; j<=m; j++)
    {
      Serial.print(matrix[i][j]);
    }
    Serial.println(" ");
  }
}


The function getPressString read the keyboard (4 row, 4 column, 16 button)


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is no "new" operator in Arduino.

Quote
halt:
  goto halt;

Run for the hills!
« Last Edit: March 22, 2011, 04:31:41 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How to make a dynamic matrix in the arduino IDE?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

malloc?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you give me an example please?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int* vector = (int*)malloc(sizeof (int) * vect_size);
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Last question, I hope….
I want to create 2D array, that’s wrong with this code?

Code:
int x,y;
 int* vector = (int*)malloc(sizeof (int) * x);
 for(int i=0;i<2; i++)
  {
    int*  vector[i]=(int*)malloc(sizeof (int) *y);
  }
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to create 2D array, that’s wrong with this code?
A vector is a one-dimensional array.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you give me an example please?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26526
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Reply #5.
Your code in reply #6 simply creates vectors, and when the inner loop finishes, one will no longer exist, and you've got no way of freeing it.
« Last Edit: March 22, 2011, 05:45:15 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Quote
I want to create 2D array, what’s wrong with this code?

Code:

int x,y;
 int* vector = (int*)malloc(sizeof (int) * x);
 for(int i=0;i<2; i++)
  {
    int*  vector=(int*)malloc(sizeof (int) *y);
  }
You have not defined values for x and y, so they will contain garbage. You are then defining an array of garbage size.

You are declaring a local variable, vector, in the for loop. It will go out of scope when the loop ends. The variable is an array of pointers to ints that you are trying to value with a single pointer to int, of garbage size.

Do you realize how much (actually, how little) SRAM the Arduino has for arrays?

If you correctly define vector, which, as AWOL points out, is a lousy name for a 2D array, you will need to pass the dimensions to any function that uses that array, and to free the memory used by the array when you are done with it.
Logged

Pages: [1]   Go Up
Jump to: