FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   strangest bug ever?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: strangest bug ever?  (Read 858 times)
benelek

35160983516098 WWW Email
strangest bug ever?
« on: Feb 20th, 2003, 8:03am »

i think there's a bug found here, but can't figure out exactly what it is. try it. you should be able to see all the strange things im talkin' bout. help!
 
Code:

void setup() {
  size(400,400);
  background(255);
  viewTool = new checkBoxMenu();
    viewTool.addBox("box1");
    viewTool.addBox("box2");
    viewTool.addBox("box3");
}
 
checkBoxMenu viewTool;
 
void loop() {
  viewTool.drawA();
}
 
class checkBoxMenu {
   
  checkBox[] boxes = new checkBox[0];
   
  checkBoxMenu () {
  }
   
  void addBox(String newLabel) {
    checkBox[] temp = new checkBox[boxes.length+1];
    temp[boxes.length] = new checkBox(newLabel);
    boxes=temp;
  }
   
  void drawA() {
 fill(#FFCC00);
 rectMode(CORNER);
 rect(10,15,20,50);
 println(boxes[2].name);
 println("p5 only reckognises tha last member of the boxes array. length of boxes is " + boxes.length);
 println(boxes[1].name + " is boxes[1]");  //try commenting this line.
 println(boxes[0].name + " is boxes[0]");  //try commenting this line.
    }
   
}
 
 
class checkBox {
  String name;
  checkBox (String Iname) {
    name=Iname;
  }
}
 
 
 
REAS


WWW
Re: strangest bug ever?
« Reply #1 on: Feb 20th, 2003, 5:17pm »

i don't think it was a bug, i think a logic error. i'm not sure exactly what you were trying to do, but it seems you were trying to dynamically grow an array? try the code below where the number of boxes is fixed at 10.
 
Code:

checkBoxMenu viewTool;  
 
void setup()  
{  
  size(400,400);  
  background(255);  
  viewTool = new checkBoxMenu();  
  viewTool.addBox("box1");  
  viewTool.addBox("box2");  
  viewTool.addBox("box3");  
}  
 
void loop()  
{  
  viewTool.drawA();  
}  
 
class checkBoxMenu  
{    
  CheckBox[] boxes = new CheckBox[10];  
  int numboxes;
   
  void addBox(String newLabel) {  
    CheckBox temp = new CheckBox(newLabel);  
    boxes[numboxes] = temp;
    numboxes++;  
  }  
   
  void drawA() {  
    fill(#FFCC00);  
    rectMode(CORNER);  
    rect(10,15,20,50);  
    println(boxes[2].name);  
    println("p5 only reckognises tha last member of the boxes array. length of boxes is " + boxes.length);  
    println(boxes[1].name + " is boxes[1]");  //try commenting this line.  
    println(boxes[0].name + " is boxes[0]");  //try commenting this line.  
  }  
}  
 
 
class CheckBox  
{  
  String name;  
   
  CheckBox(String Iname)  
  {  
    name = Iname;  
  }  
}  
 
benelek

35160983516098 WWW Email
Re: strangest bug ever?
« Reply #2 on: Feb 21st, 2003, 1:02am »

mm, growing an array in this manner has worked fine in the past. what's the difference between the above code and the following (which has no problems), in terms of logic?
 
Code:

void setup() {
  size(200,200);
  noBackground();
}
 
int[] theArray = new int[5];
 
void grow(int newNum) {
  int[] temp = new int[theArray.length+1];
  System.arraycopy(theArray,0,temp,0,theArray.length);
  temp[theArray.length]=newNum;
  theArray=temp;
}
 
void loop() { }
 
void mousePressed() {
  grow(29);
  println(theArray.length);
}
 
fry


WWW
Re: strangest bug ever?
« Reply #3 on: Feb 22nd, 2003, 4:16pm »

i think you're just missing the arraycopy in the original code you posted, so your older checkboxes don't get copied over.. growing an array as such should be fine.
 
fwiw, growing an array element by element like that is expensive/more processor intensive than necessary. i'd recommend the following instead:
 
Code:
int boxCount = 0;
checkBox boxes[] = new checkbox[10];
 
void addBox(String newLabel) {  
  // grow the array if full
  if (boxCount == boxes.length) {
    checkBox[] temp = new checkBox[boxCount*2];  
    System.arraycopy(boxes, 0, temp, 0, boxCount);
    boxes = temp;  
  }
  boxes[boxCount++] = new checkBox(newLabel);  
}
 
benelek

35160983516098 WWW Email
Re: strangest bug ever?
« Reply #4 on: Feb 23rd, 2003, 12:23am »

ah. thanx Fry
 
Pages: 1 

« Previous topic | Next topic »