Thread: malloc?
View Single Post
  #4   Spotlight this post!  
Unread 20-06-2007, 19:02
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: malloc?

There's gotta be a moderately automated way to do this.

One way I can think of to make the array-resizing a bit easier would be to do something like this:

Code:
enum ReadReasons (CAMERA_X,CAMERA_Y,CAMERA_Z,CAMERA_SPEED,TURN_SPEED,REASON_COUNT);
bool myArray[REASON_COUNT];

int main()
{
  Read(CAMERA_X);
  Read(CAMERA_Y); // the read function would perform behavior appropriate to whatever was passed in
  Read(CAMERA_Z);
}
void Read(ReadReason blah)
{
  switch(blah)
  {
    case CAMERA_X:
      if(myArray[CAMERA_X])
        Terminal_Print("Enter camera pan mode: ");
      Terminal_Read();
  }
}
Now, so long as the programmer defines a new entry in that enum each time they make a new call to Read, the array will be resized appropriately at compile time. IMO, it slightly improves code readability because you have a reason for each read that MUST be defined in clear english.

I keep thinking you could also use the preprocessor to do this, because having to do dynamic allocation for something that you know the needed size of at compile-time just sets of alarm bells in my head.

Last edited by Bongle : 20-06-2007 at 19:06.