简体   繁体   English

在 Visual Studio Code 中代码不起作用,但过了一会儿它又起作用了,为什么?

[英]In Visual Studio Code code doesn't works but after a bit it again works why?

I was writing some code and every thing seemed alright.我正在编写一些代码,一切似乎都很好。 I carefully watched it run, but only to half way and then it stops, when it's time to give output from my given input.我仔细观察它运行,但只跑到一半然后它停止了,这时是时候从我给定的输入中给出 output 了。
But when I removes while loop and added do while loop it worked.但是当我删除 while 循环并添加 do while 循环时,它起作用了。 After that I again used the while loop, ie exactly the code that was not running before, not changing anything, same to same code as before.之后我再次使用了while循环,即之前没有运行的代码,没有改变任何东西,与之前的代码相同。 But now it works.但现在它起作用了。
I faced this problem 3-4 times.我遇到这个问题3-4次。
I fixed all and even though my code was correct, sometimes it runs and sometimes it doesn't.我修复了所有,即使我的代码是正确的,有时它会运行,有时它不会。
This wastes my time a lot.这非常浪费我的时间。 Why is this happening?为什么会这样?

My pc is i5 7200u @ 2.5 ghz, 4 gb ram.我的电脑是 i5 7200u @ 2.5 ghz,4 GB 内存。

I notice that in Visual Studio Code the text looked like they move from its place while i am scrolling through.我注意到在 Visual Studio Code 中,当我滚动浏览时,文本看起来像是从它的位置移动了。 my code is this我的代码是这个

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *next;
};
struct node *head;

void insert(int a);
void print();

int main (){
    head = NULL;
     printf("How many numbers?\n");
      int x,n;
    scanf("%d",&x);
    for(int i=0 ;i<x ;i++){
    printf("enter no\n");
    scanf("%d",&n);
    insert(n);
    }
    print();
    
}

void insert(int a){
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    (*temp).data=a;
    (*temp).next=head;
    head=temp;

}

void print(){
    struct node *temp=head;
/* i declearde this while i was doing do while loop*/ int i=0;
     printf("list is :");
    while(temp != NULL)  //only changed this with do while 
    {                    //and again replaced this same code i am 100% sure
        printf(" %d",(*temp).data);
        temp=(*temp).next;
    }
 /* do{
printf("list is :");
printf("%d",(*temp).data);
temp=(*temp).next;
i++
}while(i<3);//just to conform that I was right I set i and looped for 3 times 
//and then the code worked out and after this I tried while loop then also it 
worked out fine same code that was not working before, started working
*/
    printf("\n");
}

Two possible answers:两个可能的答案:

A) You are mistaken in "i again made it while loop,exactly the code that was not running before" and missed a tiny but relevant difference. A)您误认为“我再次在循环中创建了它,正是之前未运行的代码”并且错过了一个微小但相关的差异。 If you do not happen to have used a versioning system for your code, this is my bet.如果您碰巧没有为您的代码使用版本控制系统,我敢打赌。

B) If for any reason your are 100% sure about "i again made it while loop,exactly the code that was not running before", then you probably have undefined behaviour in your code due to a mistake you made. B)如果出于任何原因,您 100% 确定“我再次在循环中执行,确切地说是之前未运行的代码”,那么您的代码中可能由于您犯的错误而存在未定义的行为。 Without seeing your code this cannot be analysed in more detail.如果没有看到您的代码,就无法更详细地分析。
Undefined behaviour (see Undefined, unspecified and implementation-defined behavior ) could explain any strange behaviour, including the one you describe.未定义的行为(请参阅未定义、未指定和实现定义的行为)可以解释任何奇怪的行为,包括您描述的行为。
With the shown code (sadly only one version of it), I can see a vulnerability to wrong input in combination with non-initiliased variables.使用显示的代码(遗憾的是它只有一个版本),我可以看到错误输入与非初始化变量相结合的漏洞。 Ie you do not check the return value of scanf() , hence you do not notice when scanning fails.即您不检查scanf()的返回值,因此扫描失败时您不会注意到。 Then you use x befor it is initialised, whatever loop you use.然后在初始化之前使用x ,无论您使用什么循环。
Gotcha: Undefined Behaviour.问题:未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM