473,236 Members | 1,393 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,236 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 2200
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.