| |

VerySource

 Forgot password?
 Register
Search
View: 3596|Reply: 13

A simple C language problem

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-9 14:00:02
| Show all posts |Read mode
for (int i=0;i<=10;i++)
int s=0;
In the second cycle, is the original space released in the first time?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-10-9 16:45:02
| Show all posts
Can you debug this thing? Look at the execution process and you will know it. Compilation environments such as VC or Dev-cpp have this function.
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-9 22:15:01
| Show all posts
for (int i=0;i<=10;i++)
int s=0;
-------------
More standardized writing
int i = 0;
int s = 0;

for(i = 0;i <= 10;i++)
{
   s = 0
}
i and s are local variables that apply for address space in the stack. The for loop is just a value-added operation and no space is released.
Reply

Use magic Report

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-10-10 02:00:01
| Show all posts
The one on the second floor doesn't seem to understand what I mean!
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-10 02:15:01
| Show all posts
Is S a local variable???
I think so?!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-10 03:00:01
| Show all posts
This is a non-standard way of writing. The C language does not support applying for space at any time. It is estimated that the compilation will not pass.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-10 08:30:01
| Show all posts
Or add static
Reply

Use magic Report

0

Threads

1

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-10 10:41:51
| Show all posts
Try it in Dev-cpp
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-10 11:45:01
| Show all posts
Will be released. The lifetime of a local variable starts at the declaration and ends at the end of its scope. Change the program to
for(int i=0;i<=10;i++)
{
if(i==0) int s=1;
    else s++;
}
Just see it.
Reply

Use magic Report

0

Threads

1

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-13 22:39:58
| Show all posts
#include <cstdio>
int main()
{
for (int i = 0; i <10; i++)
{
int s = 0;
s++;
printf("%d", s);
}
}
The result is 111111, indicating that it has been released
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list