follwing code gives error:
1 #include<iostre am.h>
2 int main()
3 {
4 int a=5,b;
5 switch(a){
6 case 1:
7 {b=5;
8 break;
9 }
10 case 2:
11 b=7;
12 int c=4;
13 c=3;
14 break;
15 case 3:
16 c=2;
17 break;
18 }
19 return 0;
20 }
"1.cpp", line 15: Error: This case bypasses initialization of a local
variable.
If i put statements under case 2: in {}, i get error:
"1.cpp", line 18: Error: c is not defined.
So exactly what is happening?
i have read that
A problem occurs , when a variable is declared AND INITIALIZED, in a
location where the flow-of-control is undetermined relative to the
location of the declaration (after the declaration has been moved to the
beginning of the scope block).
Or perhaps a better way of saying it, is the compiler recognizes the
declaration and initialization as two different things:
the declaration is moved to a "static" location at the beginning of the
scope block
while the initialization is really an assignment statement whose execution
is "dynamic", that is, it depends on the flow of control through the
program
Can someone one explain reason in a more common way. I am not able to
understand it.Can you put code, as seen by compiler. 7 25136
gyan wrote: follwing code gives error: 1 #include<iostre am.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 } 10 case 2: 11 b=7; 12 int c=4; 13 c=3; 14 break; 15 case 3: 16 c=2;
'c' is local to this 'switch'. However, if 'a' is 3, then the declaration
of 'c' (and its initialisation) does not get "executed". That's forbidden.
17 break; 18 } 19 return 0; 20 }
"1.cpp", line 15: Error: This case bypasses initialization of a local variable.
If i put statements under case 2: in {}, i get error: "1.cpp", line 18: Error: c is not defined.
So exactly what is happening?
What's happening is simple: you're trying to declare/define/initialise
a variable in one place and use it later, but the control flow _allows_
the delcaration/definition/initialisation to be skipped. Similar to
goto blah;
int c = 5; // this is skipped by the goto.
blah:
c = 2;
i have read that A problem occurs , when a variable is declared AND INITIALIZED, in a location where the flow-of-control is undetermined relative to the location of the declaration (after the declaration has been moved to the beginning of the scope block).
I am not sure I understand that (my coffee hasn't kicked in yet).
Or perhaps a better way of saying it, is the compiler recognizes the declaration and initialization as two different things:
the declaration is moved to a "static" location at the beginning of the scope block while the initialization is really an assignment statement whose execution is "dynamic", that is, it depends on the flow of control through the program
Can someone one explain reason in a more common way. I am not able to understand it.Can you put code, as seen by compiler.
The relevant part of your switch statement is semantically equivalent
to this:
if (a == 3) goto case3;
case2:
int c = 4;
c = 3;
case3:
c = 2;
If 'a' is indeed 3, the code that declares/defines/initialises 'c' is
stepped over.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
gyan napisa³(a): follwing code gives error: 1 #include<iostre am.h>
there is no such standard header, use <iostream> instead
2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 } 10 case 2: 11 b=7; 12 int c=4; 13 c=3; 14 break; 15 case 3: 16 c=2; 17 break; 18 } 19 return 0; 20 }
please, do not include line numbers like that, it prevents us from
copy-pasting code form newsreader in order to compile it
instead try indicating interesting lines in a comment:
int c=4;
c=3;
break;
case 3: //line 15
c=2;
break;
}
"1.cpp", line 15: Error: This case bypasses initialization of a local variable.
If i put statements under case 2: in {}, i get error: "1.cpp", line 18: Error: c is not defined.
So exactly what is happening?
its pretty strightforward, 'c' begins its life at line 12, it is however
visible in 'case 3' becouse its in the same scope,
when you enter 'case 3' you try to use variable 'c', but you ommit its
birth place/time which result in your first error message
when you intorduce a scope in 'case 2' 'c' is not visible in 'case 3'
thus second error message
Victor Bazarov <v.********@com acast.net> wrote: The relevant part of your switch statement is semantically equivalent to this:
if (a == 3) goto case3; case2: int c = 4; c = 3; case3: c = 2;
If it is not provable that a is always 3, must a conforming compiler
accept this code (possibly with helpful warnings)?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
So basically all of you saying that i am declaring/defining/initializing c
in case:2 and using it in case 3. Now it may possible that case 2 block
didn't got executed and in case 3: we are using variable c without
defining it.
But look if i put concern part of code as below:
int a=3;
switch(a){
case 2:
int c;
break;
case 3:
c=5;
cout<<c<<end;
}
I got it compiled and executed successfully.Wh y?
Hi All,
I will like to make my question more straight forward. See code as
#include<iostre am.h>
int main()
{
int a=1;
switch(a){
case 1:
int b=3;
break;
case 2:
break;
}
return 0;
}
When compiling, i get error:
"1.cpp", line 9: Error: This case bypasses initialization of a local
variable.
Christopher Benson-Manica wrote: Victor Bazarov <v.********@com acast.net> wrote:
The relevant part of your switch statement is semantically equivalent to this:
if (a == 3) goto case3; case2: int c = 4; c = 3; case3: c = 2;
If it is not provable that a is always 3, must a conforming compiler accept this code (possibly with helpful warnings)?
Well, a conforming compiler is allowed to accept any code even if it's
ill-formed (see clause 1, subclause 1.4, for example).
6.7/3 allows to jump over a declaration of a local object if it is of POD
type and doesn't contain an initialiser. The code above is ill-formed.
A diagnostic is required; I conclude that based on the fact that the text
of the Standard does not contain "no diagnostic required" in 6.7/3.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
gyan wrote: I will like to make my question more straight forward.
There is no question anywhere in this post. Care to ask it?
See code as #include<iostre am.h>
You've been told already, IIRC that <iostream.h> is a non-standard header.
Please stop using it, it's for your own good. Besides, in this program
there is no need to include it at all.
int main() { int a=1; switch(a){ case 1: int b=3; break; case 2: break;
} return 0; }
When compiling, i get error: "1.cpp", line 9: Error: This case bypasses initialization of a local variable.
Yes, according to the rules of the language. There are no requirements
for a compiler to be as sophisticated as to prove that 'a' in your code
above cannot have any other value than 1. You can do it yourself, if you
need to. Some compilers can do it. Many don't bother.
The bottomline is, don't write such programs. And if the compiler tells
you that your code contains an error, fix it.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: NS Develop |
last post by:
What's wrong with the following:
void SomeClass::someMethod(void)
{
A *ptrA = NULL;
ptrA = &A();
....
}
|
by: Kench |
last post by:
I was curious and playing with pointers and references to see what's
different between them.
Other than the obvious ones involving C++ syntax & things like references
cannot be modified with addition & subtraction,
pointers can be modified, both are similar.
It stated me going what are the diferrence between a reference vairable and
local variable ?
Here are what I can think of
1. For a local variable constructor is called, not the case...
|
by: romerun |
last post by:
Hello,
This make me very confusing:
----------------------------------
int &kkk() {
int a = 5;
return a;
}
|
by: Alzane |
last post by:
I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
|
by: John Smith |
last post by:
In the following (pseudo)code, why is it that C# returns an
"unassigned local variable" error for nVar? It seems I have to
declare the variable outside foo() for the error to dissapear.
void foo()
{
int nVar = 0;
SqlDataReader rdr = objCmd.ExecuteReader();
| |
by: Edward Yang |
last post by:
A few days ago I started a thread "I think C# is forcing us to write
more (redundant) code" and got many replies (more than what I had
expected). But after reading all the replies I think my question about
local variable initialization is still not solved. And some of the
replies forked into talking about out parameters. And the thread is
becoming way too deep.
So I open a new thread here. My question in the previous thead has
turned...
|
by: silverburgh.meryl |
last post by:
Can you please tell me what is the meaning this error in general?
UnboundLocalError: local variable 'colorIndex' referenced before
assignment
In my python script,
I have a variable define and init to 0, like this
colorIndex = 0
and in one of my functions, I increment it by 1
|
by: Laura T. |
last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used
before assigned.
as far as I can see, definite assignment is made.
If I add
finally
{
ret = true;
a3 = "b3";
|
by: Jess |
last post by:
Hello,
I understand that if a function "f" has a local variable "a", and
after it returns, "a" vanishes. If "f" returns "a" as a result, then I
noticed the following:
1. if the return type is "a&", then compiler complains reference to
the local variable "a".
2. if the return type is "a", then everything works fine.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |