这个简单的程序在visual studio中引发了异常 [英] This simple program throws an exception in visual studio

查看:133
本文介绍了这个简单的程序在visual studio中引发了异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Visual Studio 2015中编译以下简单的C程序(计算5个五个科目的总和及其总百分比),但它抛出异常,任何人都可以帮助我,我是新手使用visual studio。 />
关闭控制台应用程序窗口(32位)并弹出一个窗口,说明



ConsoleApplication3中的0x54ACB5F2(ucrtbased.dll)异常抛出.exe:0xC0000005:访问冲突写入位置0xCCCCCCCC。



如果有此异常的处理程序,程序可以安全地继续。



我尝试了什么:



I tried compiling this following simple C program(calculating sum of 5 five subjects and its aggregate percentage) in visual studio 2015,but it throws an exception, can anyone help me, I'm new to use visual studio.
It closes the console application window (32bit) and pops up a window saying that

"Exception thrown at 0x54ACB5F2 (ucrtbased.dll) in ConsoleApplication3.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

If there is a handler for this exception, the program may be safely continued."

What I have tried:

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>


int main()
{
	int marks=0,sub[4],i=1;
	while (i <= 5)
	{
		A:printf("\nEnter Subject %d Marks: ", i);
		scanf_s("%d", sub[i]);
		if(sub[i]<0)
		{
			printf("\nINVALID VALUE!!");
			goto A;
		}
		else
		marks = marks + sub[i];
	}
	printf("\nTotal Marks : %d out of 500, Aggregate percentage is %f", marks, marks / 5);
    return 0;
}

推荐答案

C中的数组以索引0开头,因此如果声明一个包含4个整数的数组,则只能访问元素0,1,2和3 - 您的代码允许1,2,3,4和5这将导致问题。



并删除 goto 从你的记忆中直到你拥有大约五年的开发经验 - 到那时你会明白什么时候使用它是个好主意。这不是一个好时机,所以不要使用它 - 它有助于创建难以理解,维护和编写的代码。

我多年没有使用goto,可能是几十年!
Arrays in C start with index zero, so if you declare an array of 4 integers, you can only access elements 0, 1, 2, and 3 - your code allows 1, 2, 3, 4, and 5 which will cause problems.

And remove goto from your memory until you have around five years of development experience behind you - by then you will understand when it is a good idea to use it. This is not a good time, so don't use it - it helps to create code that is difficult to understand, maintain, and write.
I haven't used a goto in years, possibly decades!


正如您所知,错误来自于太小的数组。

但如果你仔细观察,你会看到实际上你根本不需要一个数组,一个变量就足够了。

看你做什么:你读取一个值,存储在数组上,用它做一些事情,然后循环下一个。

你永远不会再使用你在数组中保存的值。

数组的所有兴趣都是你可以在填充数组后重用它的值。
< br $> b $ b

要摆脱 goto ,请研究 do ... while 循环。



调试器是一个专门用于跟踪错误的工具,习惯它。



当你不明白你的代码在做什么或为什么它做它做的时候,答案我s 调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
As you have been told, the error comes from the array that is too small.
But if you look carefully, you will see that in fact you don't need an array at all, a variable is enough.
See what you do: you read a value, store on array, do something with it, then loop for next one.
And you never use again the values you saved in array.
All the interest of array is that you can reuse its values after filling the array.


To get rid of the goto, study the do ... while loop.

The debugger is a tool designed to gelp you tracking bugs, get used to it.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于这个简单的程序在visual studio中引发了异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆