Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Software code - spot the error


  • Please log in to reply
4 replies to this topic

#1 warrenja1

warrenja1

  • Members
  • 3 posts
  • OFFLINE
  •  
  • Local time:05:33 AM

Posted 11 July 2016 - 06:47 AM

Hi all,

Our company is in the process of hiring its first software engineer for embedded control systems. I have a lot of experience dealing with software engineers and designing the machine functionality, but I don't have much of a clue about software programming language itself. The control systems we use utilise C/C++.

 

The below piece of faulty code was given to me to use on test candidates. Can someone tell me what the mistake is here? Thanks.

 

 

*************************************************
extern unsigned int ExFunc(unsigned char V);

void MyFunction(unsigned int Value)
{
float Flt;
unsigned int Res;

for(Flt=0; Flt < 10; Flt++)
 Res++;
 ExFunc();
 

printf("The number is",RES)
return(Res);
}

**********************************************



BC AdBot (Login to Remove)

 


#2 BaconFarmer

BaconFarmer

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:06:33 AM

Posted 17 July 2016 - 11:37 AM

Hi warrenja1!

 

I am not 100% sure, but I guess it is faulty, because Res is not initialized, so whatever was in the given memory address is in Res now and is handled as an unsigned int meaning it can only hold positive integers from 0 up to ~4,000,000,000. So this code has an undefined behavior, because it greatly depends on what was in the memory address that is now allocated for Res.(Most likely the needed value in this case for Res is Value, which was sent to the function at the beginning, but there could be an arbitary big number in it, because it is not initialized.)

Also if I am not mistaken the printf statement is also wrong, because a %u would be needed at the end of the string:printf("The number is %u",Res); and the semicolon is missing too + c++ is not case insensitive so Res would be needed instead of RES. Oh and I forgot to mention that the value that MyFunction gets isn't even used. And the last two which I noticed is that Exfunc is called without a parameter, and Res is returned even though MyFunction is void.(if return value is needed void should be changed to unsigned int, and at the end return Res; should be left there without the () around it.)

One more found.There are no starting and ending braces({}) for the for loop, in this case only the next line is in the loop.

 

The corrected code(at least in my opinion):

*************************************************
extern unsigned int ExFunc(unsigned int V);

void MyFunction(unsigned int Value)
{
int Flt;
unsigned int Res = Value;

for(Flt=0; Flt < 10; Flt++)
{
  Res++;
  ExFunc(Res);
}
printf("The number is %u",Res);
}

**********************************************

 

You can notice I changed in ExFunc the parameter unsigned int instead of char, it is because unsigned int is larger than unsigned char so it could lead to unexpected results if left that way (There would be an implicit conversion from unsigned int to unsigned char).Also the Value which MyFunction gets could be the initialization value for Res.

(And for incrementing purposes instead of float, int should be used, because of the way how floating point numbers are represented in the computer.)

 

I hope this helped, and if you need any more explanation just tell me, and I will try to explain it to the best of my knowledge.

BaconFarmer


Edited by BaconFarmer, 17 July 2016 - 12:31 PM.


#3 warrenja1

warrenja1
  • Topic Starter

  • Members
  • 3 posts
  • OFFLINE
  •  
  • Local time:05:33 AM

Posted 20 July 2016 - 02:22 PM

Cheers for that BaconFarmer.

That helps a lot. And I though there was only 1 mistake!!

 

Thanks again.

 

 



#4 aleeb

aleeb

  • Members
  • 2 posts
  • OFFLINE
  •  
  • Local time:06:33 AM

Posted 13 August 2016 - 08:22 AM

Hi warrenja1!

 

I have another tip for you. You use the float variable Flt for counting from 0 up to 9.

I highly recommend using an integer variable (usually a plain old int should do the job) because if you perform calculations with floating point numbers there often occur rounding errors.

So for example if you increased a float counter variable by 0.1 (0.1 cannot be represented well in the binary system that the computer is using internally, so it would be the periodic number 0.0001100110011...) and the loop condition was Flt < 3 it could happen that Flt is 2.99999 and the loop is executed once again. This can be even more problematic if you check for equality of floating point numbers.

Thus it is advisable to avoid floating point numbers as counter variables.

In your case there is no reason for using float so I suggest using int.

 

I hope my explanation is understandable.

aleeb



#5 warrenja1

warrenja1
  • Topic Starter

  • Members
  • 3 posts
  • OFFLINE
  •  
  • Local time:05:33 AM

Posted 18 August 2016 - 12:01 PM

Thanks very much aleeb. That makes sense.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users