By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,669 Members | 2,203 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,669 IT Pros & Developers. It's quick & easy.

Question regarding scope and lifetime of variable

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.