奇怪的错误

axiqia 2016-04-14 12:00:13
问题描述:
oj评测断点异常。
代码功能:
稀疏矩阵乘法。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;

typedef struct node
{
int r,l,v;
} edge;

int cmp(edge a,edge b)
{
return (a.r == b.r) ? (a.l < b.l) : (a.r < b.r);
}

int m,n,num[1000],maxa,maxb,ma;

void init(edge a[],int k)
{
scanf("%d%d",&m,&n);
//cout << " sad "<<endl;
for (int i = 0 ;; i++)
{
//scanf("%d%d%d",&a[i].r,&a[i].l,&a[i].v);
cin >> a[i].r >> a[i].l >> a[i].v;
if(a[i].l == 0 && a[i].r == 0 && a[i].v == 0)
break;
if(k)
{
num[a[i].r]++;
if(a[i].l > maxb)
maxb = a[i].l;
ma++;
}
else
maxa++;
}
}

int main()
{
edge a[1000],b[1000],c[1000];
//memset(a, 0, sizeof(a));
init(a,0);
init(b,1);

int cput[1000],k = 0;
cput[1] = 0;
for (int i = 2; i <= maxb; i++)
cput[i] = cput[i-1]+num[i-1];

for (int i = 0 ; i < maxa; i++)
{

for (int j = cput[a[i].l]; ; j++)
{
if(j < ma && a[i].l == b[j].r)
{
c[k].r = a[i].r;
c[k].l = b[j].l;
c[k++].v = a[i].v * b[j].v;
}
else
break;
}

}

sort(c,c+k,cmp);
int i = 0;

for (i = 0 ; i < k-1 ; i++)
{
if(c[i].r == c[i+1].r && c[i].l == c[i+1].l)
c[i+1].v += c[i].v;
else
printf("%d %d %d\n",c[i].r,c[i].l,c[i].v);
}
printf("%d %d %d\n",c[i].r,c[i].l,c[i].v);
//system("pause");
return 0;
}
...全文
351 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-04-15
  • 打赏
  • 举报
回复
请参考scanf的源代码: Visual C++ 2010 Express简体中文版http://pan.baidu.com/s/1bnwRVLt C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\input.c 的内容。
小灸舞 版主 2016-04-15
  • 打赏
  • 举报
回复
。。。。
qq_29273775 2016-04-15
  • 打赏
  • 举报
回复
竟然只让回复三次。。。。
查了一下ascii码表:

结论应该就是非法输入吧。
axiqia 2016-04-15
  • 打赏
  • 举报
回复
对数据做了简单的测试:
测试一:
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
int m, n, t;
printf("%d\n",scanf("%d%d",&m,&n));
printf(" %d\n", scanf("%d", &t));
printf("%d", t);
system("pause");
return 0;
}

输入为:3 3 
输出为: 

测试二:
#include <cstdio>
#include <cstring>
int main()
{
char str[1000];
gets(str);
for(int i = 0; i < strlen(str); i++)
printf("%d", str[i]);
printf("\n");
}

输出为:
axiqia 2016-04-15
  • 打赏
  • 举报
回复
接楼上

输出后奔溃
axiqia 2016-04-15
  • 打赏
  • 举报
回复
引用 3 楼 qq423399099 的回复:
楼主的数据是不是从文件里输入的? 两个文件的区别在于第二个文件后面多了一些空白符(空格,tab之类的吧,或者不可见的一些字符?) 报错的原因是因为楼主你自己发的那个图上,a[i].r的值是0xcccccccc Visual C++在Debug模式下,会把栈上的变量初始化用0XCC填充,是一个未初始化的值转成十进制就是-858993460 所以很明显a[i].r并没有读到值 num[-858993460]++;楼主说能不报错吗
printf("%d\n",scanf("%d%d",&m,&n));
我把输入改成这样,输出scanf的返回值,一次性输出两个2, 0,然后奔溃
axiqia 2016-04-14
  • 打赏
  • 举报
回复
目前尝试过利用memset初始化数据,也尝试过将变量都声明为全局变量,但是错误依旧。
axiqia 2016-04-14
  • 打赏
  • 举报
回复

同样的数据,第一个可以运行出来结果,第二个不行
版本1:
3 3  
1 1 1  
2 2 2  
2 3 4  
3 1 -4  
0 0 0  
3 3
1 3 -2  
2 3 -5  
3 1 8  
3 2 -6  
0 0 0

版本2
3 3  
1 1 1  
2 2 2  
2 3 4  
3 1 -4  
0 0 0  
3 3
1 3 -2  
2 3 -5  
3 1 8  
3 2 -6  
0 0 0
版本2的错误如下图:

赵4老师 2016-04-14
  • 打赏
  • 举报
回复
在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。
小灸舞 版主 2016-04-14
  • 打赏
  • 举报
回复
楼主的数据是不是从文件里输入的?
两个文件的区别在于第二个文件后面多了一些空白符(空格,tab之类的吧,或者不可见的一些字符?)
报错的原因是因为楼主你自己发的那个图上,a[i].r的值是0xcccccccc
Visual C++在Debug模式下,会把栈上的变量初始化用0XCC填充,是一个未初始化的值转成十进制就是-858993460
所以很明显a[i].r并没有读到值
num[-858993460]++;楼主说能不报错吗
axiqia 2016-04-14
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
Format Specification Fields: scanf and wscanf Functions A format specification has the following form: %
  • [width] [{h | l | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates. Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
赵老师,White-space characters: blank (' '); tab ('\t'); or newline ('\n').既然这样,读入不存储,为啥还会造成上面那种情况呢?
赵4老师 2016-04-14
  • 打赏
  • 举报
回复
Format Specification Fields: scanf and wscanf Functions A format specification has the following form: %
  • [width] [{h | l | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates. Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
axiqia 2016-04-14
  • 打赏
  • 举报
回复
引用 3 楼 qq423399099 的回复:
楼主的数据是不是从文件里输入的? 两个文件的区别在于第二个文件后面多了一些空白符(空格,tab之类的吧,或者不可见的一些字符?) 报错的原因是因为楼主你自己发的那个图上,a[i].r的值是0xcccccccc Visual C++在Debug模式下,会把栈上的变量初始化用0XCC填充,是一个未初始化的值转成十进制就是-858993460 所以很明显a[i].r并没有读到值 num[-858993460]++;楼主说能不报错吗
我复制粘贴的数据,应该也算是文件出入吧。为什么scanf或者cin没有过滤掉空格或者tab呢
cms-Cloud后台管理系统 cms-Cloud 基于Spring Boot、Spring Cloud & Alibaba、OAuth2的前后端分离的后台管理系统,内置模块如:部门管理、角色用户、菜单及按钮授权、数据权限、系统参数、日志管理、代码生成等。在线定时任务配置;支持集群,支持多数据源。核心技术前端技术栈 ES6、vue、vuex、vue-router、vue-cli、axios、element-ui后端技术栈 Spring Boot、Spring Cloud & Alibaba、OAuth2系统需求JDK >= 1.8MySQL >= 5.5Maven >= 3.0主要特性1. 完全响应式布局(支持电脑、平板、手机等所有主流设备)2. 强大的一键生成功能(包括控制器、模型、视图、菜单等)3. 支持多数据源,简单配置即可实现切换。4. 支持按钮及数据权限,可自定义部门数据权限。5. 对常用js插件进行二次封装,使js代码变得简洁,更加易维护6. 完善的XSS防范及脚本过滤,彻底杜绝XSS攻击7. Maven多项目依赖,模块及插件分项目,尽量松耦合,方便模块升级、增减模块。8. 国际化支持,服务端及客户端支持9. 完善的日志记录体系简单注解即可实现技术选型1、系统环境Java EE 8Servlet 3.0Apache Maven 32、主框架Spring Boot 2.2Spring Cloud Hoxton.SR3Spring Framework 5.1Spring Security 5.13、持久层Apache MyBatis 3.4Alibaba Druid 1.14、视图层Vue 2.6Axios 0.18Element UI 2.11内置功能(1) 用户管理:用户是系统操作者,该功能主要完成系统用户配置。(2) 部门管理:配置系统组织机构(公司、部门、小组),树结构展现支持数据权限。(3) 岗位管理:配置系统用户所属担任职务。(4) 菜单管理:配置系统菜单,操作权限,按钮权限标识等。(5) 角色管理:角色菜单权限分配、设置角色按机构进行数据范围权限划分。(6) 字典管理:对系统中经常使用的一些较为固定的数据进行维护。(7) 参数管理:对系统动态配置常用参数。(8) 通知公告:系统通知公告信息发布维护。(9) 操作日志:系统正常操作日志记录和查询;系统异常信息日志记录和查询。(10) 登录日志:系统登录日志记录查询包含登录异常。(11) 在线用户:当前系统中活跃用户状态监控。(12) 定时任务:在线(添加、修改、删除)任务调度包含执行结果日志。(13) 代码生成:前后端代码的生成(java、html、xml、sql)支持CRUD下载 。(14) 系统接口:根据业务代码自动生成相关的api接口文档。(15) 服务监控:监视当前系统CPU、内存、磁盘、堆栈等相关信息。(16) 在线构建器:拖动表单元素生成相应的HTML代码。(17) 连接池监视:监视当期系统数据库连接池状态,可进行分析SQL找出系统性能瓶颈。 环境部署准备工作JDK >= 1.8 (推荐1.8版本)Mysql >= 5.5.0 (推荐5.7版本)Redis >= 3.0Maven >= 3.0Node >= 10nacos >= 1.1.0sentinel >= 1.6.0运行系统因为本项目是前后端分离的,所以需要前后端都启动好,才能进行访问。后端运行1、导入cms到Eclipse,菜单 File -> Import,然后选择 Maven -> Existing Maven Projects,点击 Next> 按钮,选择工作目录,然后点击 Finish 按钮,即可成功导入Eclipse会自动加载Maven依赖包,初次加载会比较慢(根据自身网络情况而定)2、创建数据库cms-cloud并导入数据脚本cms.sql(必须),quartz.sql(可选)3、创建数据库cms-config并导入数据脚本cms_config.sql(必须)4、配置nacos持久化,修改conf/application.properties文件,增加支持mysql数据源配置。# db mysqlspring.datasource.platform=mysqldb.num=1db.url.0=jdbc:mysql://localhost:3306/cms-config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTCdb.user=rootdb.password=password5、打开运行基础模块(启动没有先后顺序)CmsGatewayApplication (网关模块 必须)CmsAuthApplication (认证模块 必须)CmsSystemApplication (系统模块 必须)CmsMonitorApplication (监控中心 可选)CmsGenApplication (代码生成 可选)CmsJobApplication (定时任务 可选)CmsFileApplication (文件服务 可选)前端运行# 进入项目目录cd cms-ui# 安装依赖npm install# 强烈建议不要用直接使用 cnpm 安装,会有各种诡异的 bug,可以通过重新指定 registry 来解决 npm 安装速度慢的问题。npm install --registry=https://registry.npm.taobao.org 本地开发 启动项目npm run dev打开浏览器,输入:http://localhost:80 (默认账户 admin/admin123)若能正确展示登录页面,并能成功登录,菜单及页面展示正常,则表明环境搭建成功提示前端安装完node后,最好设置下淘宝的镜像源,不建议使用cnpm(可能会出现奇怪的问题)部署系统因为本项目是前后端分离的,所以需要前后端都部署好,才能进行访问后端部署bin/package.bat 在项目的目录下执行然后会在项目下生成 target文件夹包含 war 或jar1、jar部署方式使用命令行执行:java –jar xxxx.jar 或者执行脚本:bin/run.bat2、war部署方式pom.xml packaging修改为war 放入tomcat服务器webapps提示SpringBoot去除内嵌tomcat前端部署当项目开发完毕,只需要运行一行命令就可以打包你的应用# 打包正式环境npm run build:prod# 打包预发布环境npm run build:stage构建打包成功之后,会在根目录生成 dist 文件夹,里面就是构建打包好的文件,通常是 ***.js 、***.css、index.html 等静态文件。通常情况下 dist 文件夹的静态文件发布到你的 nginx 或者静态服务器即可,其中的 index.html 是后台服务的入口页面。环境变量所有测试环境或者正式环境变量的配置都在 .env.development (opens new window)等 .env.xxxx文件中。它们都会通过 webpack.DefinePlugin 插件注入到全局。常见问题如果使用Mac 需要修改application.yml文件路径profile如果使用Linux 提示表不存在,设置大小写敏感配置在/etc/my.cnf 添加lower_case_table_names=1,重启MYSQL服务如果提示当前权限不足,无法写入文件请检查profile是否可读可写,或者无法访问此目录

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧