473,395 Members | 2,443 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,395 software developers and data experts.

Question regarding scope and lifetime of variable

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;
{
int i =5;
char *s = "hello";
intp = &i;

sptr = s;

}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);

return 0;
}
Out put of the program

value of i = 5
value of sptr = hello
The variable i, and s has block scope .But as s points to string
literal life time of the s is through out the program .
My question is as i has only block scope so once it goes out of scope
address of i becoming meaning less. So is it guaranteed that first
printf statement will always print 5 ?
Regards,
Somenath
Dec 24 '07 #1
5 2209
somenath wrote:
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;
{
int i =5;
char *s = "hello";
intp = &i;

sptr = s;

}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);

return 0;
}
Out put of the program

value of i = 5
value of sptr = hello
The variable i, and s has block scope .But as s points to string
literal life time of the s is through out the program .
My question is as i has only block scope so once it goes out of scope
address of i becoming meaning less. So is it guaranteed that first
printf statement will always print 5 ?
It prints whatever is at the location is points to contains. The result
is undefined if the pointer points to a variable that is out of scope.

Your example only works because there isn't any intervening code between
the end of the block scope and the printf.

--
Ian Collins.
Dec 24 '07 #2
On Dec 24, 10:54*am, Ian Collins <ian-n...@hotmail.comwrote:
somenath wrote:
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;
* * {
* *int i =5;
* *char *s = "hello";
* *intp = &i;
* *sptr *= s;
* * }
* * printf("value of i * * = %d\n",*intp);
* * printf("value of sptr *= %s \n",sptr);
* * return 0;
}
Out put of the program
value of i * * = 5
value of sptr *= hello
The variable *i, and s * has block scope .But *as s points to string
literal life time of the s is through out the program .
My question is as i has only block scope so *once it goes out of scope
address of i becoming meaning less. So is it guaranteed that *first
printf statement will always print 5 ?

It prints whatever is at the location is points to contains. *The result
is undefined if the pointer points to a variable that is out of scope.

Your example only works because there isn't any intervening code between
the end of the block scope and the printf.
I would like to get bit more clarity .Please look at the updated
program bellow.

#include <stdio.h>

int main(int argc, char *argv[])
{
int *intp = NULL;
char *sptr = NULL;
{
int i =5;
char *s = "hello";
intp = &i;

sptr = s;

}
{
int y= 20;
printf("\n Value of y = %d \n",y);
}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);
return 0;
}
So are you indicating that *intp will not print 5 ?
Dec 24 '07 #3
On Sun, 23 Dec 2007 22:06:52 -0800, somenath wrote:
On Dec 24, 10:54Â*am, Ian Collins <ian-n...@hotmail.comwrote:
>somenath wrote:
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;
Â* Â* {
Â* Â*int i =5;
Â* Â*char *s = "hello";
Â* Â*intp = &i;
Â* Â*sptr Â*= s;
Â* Â* }
Â* Â* printf("value of i Â* Â* = %d\n",*intp); printf("value of sptr Â*=
Â* Â* %s \n",sptr);
Â* Â* return 0;
}
Out put of the program
value of i Â* Â* = 5
value of sptr Â*= hello
The variable Â*i, and s Â* has block scope .But Â*as s points to string
literal life time of the s is through out the program . My question
is as i has only block scope so Â*once it goes out of scope address of
i becoming meaning less. So is it guaranteed that Â*first printf
statement will always print 5 ?

It prints whatever is at the location is points to contains. Â*The
result is undefined if the pointer points to a variable that is out of
scope.

Your example only works because there isn't any intervening code
between the end of the block scope and the printf.

I would like to get bit more clarity .Please look at the updated program
bellow.

[...]
So are you indicating that *intp will not print 5 ?
No, he's saying printing *intp may or may not print 5, in both the
original code, and your changed code. "The result is undefined". There's
an error in your program. This error can't usually be detected at compile
time, and for that reason doesn't cause your compiler to emit an error
message, but the code is still broken.
Dec 24 '07 #4
somenath <so*********@gmail.comwrote:
#include <stdio.h>

int main(int argc, char *argv[])
{
int *intp = NULL;
char *sptr = NULL;
{
int i =5;
char *s = "hello";
intp = &i;

sptr = s;

}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);

return 0;
}
Out put of the program

value of i = 5
value of sptr = hello
The variable i, and s has block scope .But as s points to string
literal life time of the s is through out the program .
Correct.
My question is as i has only block scope so once it goes out of scope
address of i becoming meaning less. So is it guaranteed that first
printf statement will always print 5 ?
No. It has undefined behaviour. If it _does_ print a value, the most
likely is obviously 5, but it would be risky to rely on that. For
example, it's also quite allowed to crash with a "stack top violation"
error.

Richard
Dec 24 '07 #5
Ian Collins wrote:
somenath wrote:
>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;
{
int i =5;
char *s = "hello";
intp = &i;

sptr = s;

}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);

return 0;
}
Out put of the program

value of i = 5
value of sptr = hello
The variable i, and s has block scope .But as s points to string
literal life time of the s is through out the program .
My question is as i has only block scope so once it goes out of scope
address of i becoming meaning less. So is it guaranteed that first
printf statement will always print 5 ?

It prints whatever is at the location is points to contains. The result
is undefined if the pointer points to a variable that is out of scope.
No, that's not the problem. It is perfectly legal to
use a pointer to an out-of-scope variable, and in fact it
is quite usual to do so. "Scope" refers to the visibility
of the variable's identifier, not to the variable's existence.

The undefined behavior arises when the pointed-to variable's
lifetime ("storage duration") has ended. In somenath's example
this occurs at the same point in the source where the identifier
goes out of scope, and the fact that the two things happen at
the same point seems to have led to some confusion about which
causes what. They are, however, distinct notions -- and since
the question was specifically about the two notions it is as
well to clear up whatever confusion arises.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Minti | last post by:
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; }
2
by: JJ | last post by:
Hi, I am trying to understand the lifetime or scope of a class in this project. Here is the code that I am talking about: private void PopulateCategoryCombo() { ListItem objListItem;
23
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
6
by: Neelesh Bodas | last post by:
Hello All, I was just listing down various ways in which variables can be created and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3) Putting the summary here for corrections, comments,...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
22
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
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...
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
marktang
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,...
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.