I was reading some text and I came across the following snippet
switch('5')
{
int x = 123;
case '5':
printf("The value of x %d\n", x);
break;
}
Now according to the text the memory would be allocated for the varible x but
the value of x would be an intermediate one. Can anybody expain the rationale
for this I ran this snippet [ with the obvious additions] through a C++
compiler and it gave me an error for this the following definition
int x = 123;
Is this sort of behavior of any use?
I could have better done through
{
int x = 123;
switch('5') {
....
}
}
But I was just wondering as to even declaring a variable in the switch level
scope should be allowed?
--
FUCK SCO!
Imanpreet Singh Arora
isingh AT acm DOT org
Even if you are on the right track you are going to get run over if you just
keep sitting there . - Will Rogers
Nov 13 '05
18 3465
On Fri, 14 Nov 2003 10:34:04 +0530, sahukar praveen wrote: The contents below the header "6.8#3" seems to be an extract from some standard document. Could you please provide me more details about the document and the location where I can get it.
Ben already gave you the long answer. The short answer is http://webstore.ansi.org/ansidocstor...EC+9899%2D1999
Sheldon Simms <sh**********@y ahoo.com> wrote in message news:<pa******* *************** ******@yahoo.co m>... On Thu, 13 Nov 2003 12:26:59 -0800, Minti wrote:
I was reading some text and I came across the following snippet
switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
Now according to the text the memory would be allocated for the varible x but the value of x would be an intermediate one. Can anybody expain the rationale
The assignment of 123 to x never happens The code is equivalent to:
goto foo; { int x = 123; foo: printf("The value of x %d\n", x); }
Thanks Sheldon, but could you tell me when is the memory for x
allocated. If it has to be allocated when we get into the block after
the statement
goto foo;
Then obviously we can't use x at any place. Or is it the case that
that space would be reserved for x, or any other variable within the
immediate scope of the switch block but it's value would not be
initialized for the given initialization value.
--
Imanpreet Singh Arora
isingh AT acm DOT org
On Fri, 14 Nov 2003 12:10:06 -0800, Minti wrote: Sheldon Simms <sh**********@y ahoo.com> wrote in message news:<pa******* *************** ******@yahoo.co m>...
goto foo; { int x = 123; foo: printf("The value of x %d\n", x); }
Thanks Sheldon, but could you tell me when is the memory for x allocated. If it has to be allocated when we get into the block after the statement
goto foo;
Then obviously we can't use x at any place. Or is it the case that that space would be reserved for x, or any other variable within the immediate scope of the switch block but it's value would not be initialized for the given initialization value.
We don't know or care when space for the object named x is reserved,
but we know that x is in scope and accessible from the point of its
declaration to the end of the block in which it is declared. The
C Standard puts it like this:
6.2.1
2 For each different entity that an identifier designates, the
identifier is visible (i.e., can be used) only within a region
of program text called its scope.
4 If the declarator or type specifier that declares the identifier
appears inside a block ... the identifier has block scope, which
terminates at the end of the associated block.
This means that we can use x anywhere inside the block after it is
declared, regardless of whether or not it has been initialized. It
does not mean that our use of x has defined behavior.
-Sheldon
Dan Pop wrote: CBFalconer <cb********@yah oo.com> writes:Alan Balmer wrote: ... snip ... Is this not a declaration with initialization, rather than an assignment?
Contrary to external appearances, there is no such declaration for automatic variables.
Of course there is:
int x;
at block scope.
Code must always be generated to perform the initialization.
Nonsense. The compiler merely has to allocate space for x on a stack-like data structure, without touching this space. The program can assign something to x at any time before trying to access its value for the first time. If this something is not available at the time the variable is defined, there is no point in providing an initialiser.
We are not even disagreeing. Your example is a declaration with
no initialization statement. I was talking about declarations
with initialization statements, and whether they generate code.
Because they do, there is really no such thing.
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Eric Sosman <Er*********@su n.com> wrote in message news:<3F******* ********@sun.co m>... Minti wrote [reformatted for better line length and spacing]: I was reading some text and I came across the following snippet
switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
Now according to the text the memory would be allocated for the varible x but the value of x would be an intermediate one. Can anybody expain the rationale for this
Which "this" do you mean? Why `x' is indeterminate, or why the code was written this way?
The value of `x' is indeterminate because the `switch' statement jumps directly to the selected `case' or `default'. The executable code that sets `x' equal to 123 cannot be reached, and thus is not executed.
The programmer's reason for attempting this construct is a mystery I am unable to explain. Perhaps his or her grasp of C was not very firm.
I could have better done through
{ int x = 123; switch('5') { .... } }
That is fine; you have cured the indeterminacy.
But I was just wondering as to even declaring a variable in the switch level scope should be allowed?
Variable declarations are permitted at the start of *any* {}-enclosed executable block. There is nothing special about `switch' in this regard.
Is the technique useful? Yes, it can be. If a block is the only user of some particular variable, it may be a good idea to limit the variable's scope to the block. This can emphasize to the human reader that the variable has no significance outside the block, and may also help the compiler discover when the variable has "died" so it can re-use the registers or other resources that held it.
That is why I suggested a little alternative to to just the part of
declaring variable[s] within a {} that encloses the switch block, my
main reason for asking the question was since
int x = 32;
is a declaration with initialization I was confused why the
declaration would only be 'executed' i.e. why only the memory for x
would be allocated but not initialized even though they form one
single statement.
<snip>
--
Imanpreet Singh Arora
isingh AT acm DOT org
In message <pa************ *************** *@yahoo.com>
Sheldon Simms <sh**********@y ahoo.com> wrote: On Fri, 14 Nov 2003 12:10:06 -0800, Minti wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote in message news:<pa******* *************** ******@yahoo.co m>...
goto foo; { int x = 123; foo: printf("The value of x %d\n", x); }
Thanks Sheldon, but could you tell me when is the memory for x allocated. If it has to be allocated when we get into the block after the statement
goto foo;
Then obviously we can't use x at any place. Or is it the case that that space would be reserved for x, or any other variable within the immediate scope of the switch block but it's value would not be initialized for the given initialization value.
We don't know or care when space for the object named x is reserved, but we know that x is in scope and accessible from the point of its declaration to the end of the block in which it is declared.
I think we do care, although not in this example. Minti's question is
answered by distinguishing the "lifetime" from the "scope".
x's lifetime extends from the start of its enclosing block to the end. Thus
in the following example:
int *p = NULL;
{
label:
if (p) printf("x = %d\n", *p);
int x = 5;
printf("x = %d\n", x);
x = 10;
if (!p)
{
p = &x;
goto label;
}
}
You will get the output
x = 5
x = 10
x = 5
Although x isn't in scope at the first printf, it still exists, and you
can reference it through the pointer p.
So, from the abstract machine's point of view, x is "allocated" , but
uninitialised when the { } block is entered and "deallocate d" when the block
is exited. It is initialised (or re-initialised) whenever execution reaches
its declaration/initialiser.
An actual implementation does not have to actually "allocate" or "deallocate "
x there - it may reserve space for it on function entry, for example,
regardless of whether the block is entered. But that's not the C programmer's
business. For you, the scope extends from the declaration to the end of the
block, and the lifetime from block entry to block exit.
PS - never write code like that
PPS - the example is C99
PPPS - a worse example is in the C99 rationale
--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Kevin Bracey wrote: An actual implementation does not have to actually "allocate" or "deallocate " x there - it may reserve space for it on function entry, for example, regardless of whether the block is entered. But that's not the C programmer's business. For you, the scope extends from the declaration to the end of the block, and the lifetime from block entry to block exit.
I don't follow this; if 'x' was not in scope in the first line of the
block (because the declaration was two lines later), then references to
'x' at that point would have to resolve to some _other_ declaration of
'x' that _is_ in scope, not the one in this block. The example you gave
showed that both the lifetime _and_ the scope of x are the entire
enclosing block.
In message <V7Nub.18630$vJ 6.6629@fed1read 05>
"Kevin P. Fleming" <kp*******@cox. net> wrote: Kevin Bracey wrote:
An actual implementation does not have to actually "allocate" or "deallocate " x there - it may reserve space for it on function entry, for example, regardless of whether the block is entered. But that's not the C programmer's business. For you, the scope extends from the declaration to the end of the block, and the lifetime from block entry to block exit. I don't follow this; if 'x' was not in scope in the first line of the block (because the declaration was two lines later), then references to 'x' at that point would have to resolve to some _other_ declaration of 'x' that _is_ in scope, not the one in this block.
Indeed, they would.
The example you gave showed that both the lifetime _and_ the scope of x are the entire enclosing block.
I don't think so. No, the first printf accessed x through a pointer
which was in scope:
if (p) printf("x = %d\n", *p);
If I'd just written
printf("x = %d\n", x);
that would have been an error, or the wrong x would have been used.
--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Kevin Bracey wrote: I don't think so. No, the first printf accessed x through a pointer which was in scope:
if (p) printf("x = %d\n", *p);
If I'd just written
printf("x = %d\n", x);
that would have been an error, or the wrong x would have been used.
OK, yeah, I missed that subtlety. Well, actually I saw it, but my brain
passed it off as irrelevant. It wasn't :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Teddy |
last post by:
Hello all
consider the code fragment below:
case 1:
{
int i = 1;
break;
}
VC give this error:error C2360: initialization of 'i' is skipped by
|
by: Mark Yudkin |
last post by:
The documentation is unclear (at least to me) on the permissibility of
accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003,
with separate transactions scope, from separate threads of a multithreaded
program using embedded SQL. Since the threads do not need to share
transaction scopes, the sqleAttachToCtx family of APIs do not seem to be
necessary.
<quote>
In the default implementation of threaded applications against...
|
by: NotYetaNurd |
last post by:
for(int i=0;i<6;i++)
{
//
}
wont i go out of scope after the for loop ...?
|
by: Meya-awe |
last post by:
Hi, i am new to C#. I am using some code similar to the one below. I am
declaring an ArrayList in one case block but the compiler complains
about it already being used in the previous case block. Is this normal
in C#? Assume that i follow this and in the first case block, i allocate
the memory using the "ArrayList mylist=new ArrayList()", and in the
second block, i just use the variable mylist like
"mylist.Add(something)". Now if the...
|
by: Andrew Ducker |
last post by:
And no, this isn't a complaint about break - I'm very happy to make
things explicit.
However, why isn't the format something like:
switch(myVariable)
{
case 1:
{
//Do Something
| |
by: apandapion |
last post by:
The following code snippet fails to compile with "a local variable
named n is already defined in this scope." This makes me
uncomfortable, but I could not tell you exactly why. My instincts are
telling me that each case should be a seperate block, I guess. Any
comments?
public class MyClass
{
public static void Main()
{
|
by: Evan Camilleri |
last post by:
I am going from VB.NET to c#. How can I do the following?
switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
|
by: easy |
last post by:
The following code gives me compiler errors galore telling me that I'm
crossing the initalization of the vectors and that case label 2 and
default are "within scope of cleanup or variable array". I suspect its
the destructors for the vectors that is causing the problem but why are
they being called at all? Shouldn't they go out of scope at the
closing bracket after default?
Putting brackets around the contents of case1 solves my...
|
by: somenath |
last post by:
Hi All ,
I have one question regarding scope and lifetime of variable.
#include <stdio.h>
int main(int argc, char *argv)
{
int *intp = NULL;
char *sptr = NULL;
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |