Go Down

Topic: Dynamic matrix (Read 1 time) previous topic - next topic

kostas_koz


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: [Select]



#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)




AWOL

#1
Mar 22, 2011, 10:27 am Last Edit: Mar 22, 2011, 10:31 am by AWOL Reason: 1
There is no "new" operator in Arduino.

Quote
halt:
  goto halt;


Run for the hills!
"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.

kostas_koz

How to make a dynamic matrix in the arduino IDE?

AWOL

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

kostas_koz

Can you give me an example please?

AWOL

Code: [Select]
int* vector = (int*)malloc(sizeof (int) * vect_size);
"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.

kostas_koz

Last question, I hopeā€¦.
I want to create 2D array, that's wrong with this code?

Code: [Select]

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);
  }


AWOL

Quote
I want to create 2D array, that's wrong with this code?

A vector is a one-dimensional array.
"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.

kostas_koz

Can you give me an example please?

AWOL

#9
Mar 22, 2011, 11:42 am Last Edit: Mar 22, 2011, 11:45 am by AWOL Reason: 1
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.
"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.

PaulS

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.

Go Up