473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bug in switch statement ?

Hi,

I have the following code:

Expand|Select|Wrap|Line Numbers
  1. func1()
  2. {
  3.   unsigned int i;
  4.   int j = 3;
  5.  
  6.   switch(j)
  7.   {
  8.      case 3:
  9.               i = -1UL;
  10.               break;
  11.       default:
  12.               /*some operation*/
  13.   }
  14.  
  15.   return(i);
  16. }
The above code looks ok, compiles well without error and povides the output.

However, we have a tool which will compile c programs to see if there is any error in the code, maybe to identify mem leaks, dangling pointers etc.
When i use this tool to compile the above code, it says that we are READ_DANGLING at the return statement in the above code.

When I move the return statement inside case 1: after "i = -1UL", it doesn't show this error.

Assume that moving return inside or outside the switch does same operation.

Now my question:
1) why is it saying that we are doing a READ_DANGLING?
2) is there any scope w.r.t to variable returning within and outside switch
3) When i assign any other value to i other that -1UL, this issue is not seen. So is there any error with the way i am assigning the value to variable "i"??

Please help me in understanding the above.

--Thanks
Jan 16 '09 #1
2 1603
newb16
687 512MB
@narasimp
Probably it interprets assigning negative number to unsigned as no-op, and is unable to propagate it (?) correctly out of switch statement.
Jan 16 '09 #2
donbock
2,426 Expert 2GB
I see two problems:

-1UL is appropriate for an unsigned long variable. It should be -1U for an unsigned int.

The value of i is returned. However, i is only initialized if the switch takes the case-3 path. Other paths are available. True, you've rigged your code so only the case-3 path is taken; but your static analyzer must be unimpressed.
Jan 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
37
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.