Question regarding scope and lifetime of variable

S

somenath

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
 
I

Ian Collins

somenath said:
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.
 
S

somenath

somenath said:
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 ?
 
H

Harald van Dijk

somenath said:
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.
 
R

Richard Bos

somenath said:
#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
 
E

Eric Sosman

Ian said:
somenath said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top