Strange code in lpc175x_6x lib USBCDC demo

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Strange code in lpc175x_6x lib USBCDC demo

337 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ivbor on Mon Apr 14 04:39:11 MST 2014
Folder:\lpc175x_6x\Examples\USBDEV\USBCDC\, file: cdcuser.c
lines 72 and 73

/*----------------------------------------------------------------------------
  read data from CDC_OutBuf
 *---------------------------------------------------------------------------*/
int CDC_RdOutBuf (char *buffer, const int *length) {
  int bytesToRead, bytesRead;

  /* Read *length bytes, block if *bytes are not available*/
  bytesToRead = *length;
  bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length);
  bytesRead = bytesToRead;

In which case bytesToRead < (*length) will retun true? Is it possible? Thx
Labels (1)
0 Kudos
1 Reply

325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Mon Apr 21 14:37:07 MST 2014
That code looks absolutely wonderful. :)

length is a const int *, which means: it's not marked volatile, thus you can assume that all the *length will always return the same value.

So... Let's say length points to a value of 10.

bytesToRead = 10.
bytesToRead = (10 < 10) ? 10 : 10;
BytesRead = 10;

In other words, the above can be simplified to:

bytesRead = *length;

... I've also seen code (somewhere else) like this:
if(a == 10)
{
  a = 10;
}

...Anyway, the code you've mentioned is clearly not doing what the programmer intended it to do.
0 Kudos