山东省第七届ACM省赛------Memory Leak

时间:2022-06-03 10:30:44

Memory Leak

Time Limit: 2000MS Memory limit: 131072K

题目描述

Memory Leak is a well-known kind of bug in C/C++. When a string is longer than expected, it will visit the memory of next array which will cause the issue and leak some information. You can see a simple example bellow:

山东省第七届ACM省赛------Memory Leak

As we see, if the length of the input string is equal to or larger than the limit length of array, the extra part won’t be stored and the information of next array will be leaked out while outputting. The output will stop until a ‘\0’ character (the symbol
of end of a string) is found. In this problem, there will never be unexpected end of the program and the last array won’t leak other information.

Source code given as follow:

山东省第七届ACM省赛------Memory Leak

Now a simpler source code will be given. What will the output be?

输入

 Multiple test cases, the first line is an integer T (T <= 20), indicating the number of test cases.

The first line of each test case contains a non-empty string, the definition of strings, formatted as “char s1[s1_len], s2[s2_len]...;”. “char ” is the type of the array which will never change. s1, s2... is the name of the array. s1_len, s2_len... is the
length limit. If nothing goes wrong, the array should be able to store the input string and a ‘\0’. The definitions of different arrays will be separated by a comma and a space. The length limits are positive and the Sum of length limit will be less than 10000.

Then, there will be several lines of string which consists two or three parts.

The first part is “gets” or “cout”, the second part will a string s, indicates the name of the array. s will contains only lower case letters and number digits and start with letters. s will be different in one case. If the first part is “gets”, then there
will be the third part, a string which should be input into array s, the length of input string will be less than 1000 and contains only visible ASCII characters. “gets” operation will rewrite the array no matter what was in the array before and add a ‘\0’
after the string. Different parts are separated by a space.

Case ends with “return 0;”

The whole input file is less than 10MB.

输出

 For each “cout”, you should output a line contains what will actually output, which means you should consider the issue of memory leak. If the requested array is empty, you should output an empty line.

示例输入

3
char a[5], b[5], c[5];
gets a C++
gets b Hello, world!
gets c guys
cout a
cout b
cout c
return 0;
char a[5];
cout a
gets a 233
gets a 2333
cout a
gets a 12345
cout a
return 0;
char str1[7], str2[2], str3[5];
gets str1 welcome
gets str2 to
gets str3 Shandong
cout str1
return 0;

示例输出

C++
Helloguys
guys
2333
12345
welcometoShand

来源

  “浪潮杯”山东省第七届ACM大学生程序设计竞赛

题意

一道不算很难的模拟题吧!输入的是字符串,模拟操作,定义数组,输入到数组,然后输出,其实呢!主要考察的是内存泄漏的问题,也就是一个数组的最后没有‘\0’,然后输出的时候会访问到下一段内存空间中!

AC代码:(别人的)
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int l;
    int r;
} e[11111];
int top;
char s[11111];
char c[11111][1111];
char op[11111];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(s,0,sizeof(s));
        int pos = 0;
        top = 0;
        scanf("%s",op);
        while(true)
        {
            scanf("%s",op);
            int len = strlen(op);
            int num = 0,i = 0;
            for(i = 0; i < len; i++)
            {
                if(op[i] != '[')c[top][i] = op[i];
                else break;
            }
            c[top][i] = '\0';
            for(i++ ; i < len; i++)
            {
                if(op[i] == ']') break;
                num = num * 10 + op[i] -'0';
            }
            e[top].l = pos;
            e[top].r = pos+num;
            pos+=num;
            top++;
            char ss = getchar();
            if(ss == '\n') break;
        }
        while(1)
        {
            scanf("%s",op);
            if(op[0] == 'r')
            {
                scanf("%s",op);
                break;
            }
            if(op[0] == 'c')
            {
                scanf("%s", op);
                for(int i = 0; i < top; i++)
                {
                    if(strcmp(op,c[i]) == 0)
                    {
                        for(int j = e[i].l ; j < pos; j++)
                        {
                            if(s[j] == '\0') break;
                            printf("%c",s[j]);
                        }
                        printf("\n");
                        break;
                    }
                }
            }
            else if(op[0] == 'g')
            {
                scanf("%s",op);
                for(int i = 0 ; i < top; i++)
                {
                    if(strcmp(op,c[i]) == 0)
                    {
                        gets(op);
                        int len = strlen(op);
                        int k = 1,j;
                        for( j = e[i].l ; j < e[i].r && k < len; j++, k++)
                            s[j] = op[k];
                        if(j < e[i].r)
                            s[j] = '\0';
                        break;
                    }
                }
            }
        }
    }
    return 0;
}

TLE代码:(自己的)
#include"stdio.h"
#include"string.h"
#include<iostream>
using namespace std;
#include<map>
char data[1000005];
map<string,int>k;
map<string,int>ss;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        k.clear();
        ss.clear();
        memset(data,0,sizeof(data));
        string start;
        int ci=0;
        while(cin>>start)
        {
            if(start=="char")
            {
                char c[1005]= {0};
                int size=0;
                char jud=0;
                while(~scanf("%*[ ]%[^[][%d]%c",c,&size,&jud))
                {
                    k[c]=ci;
                    ss[c]=size;
                    ci+=size;
                    if(jud==';')break;
                }
            }
            else if(start=="gets")
            {
                string value;
                cin>>value;
                scanf("%*[ ]");
                gets(data+k[value]);
                data[k[value]+ss[value]]=0;
                memset(data+k[value]+ss[value],0,sizeof(data+k[value]+ss[value]));
                for(int i=k[value]+ss[value]; i<=1000005; i++)
                    data[i]=0;
//                cout<<"/////////////////////////////////////"<<endl;
//                for(int i=0;i<20;i++)
//                    printf(i!=19?"%c":"%c\n",data[i]);
            }
            else if(start=="cout")
            {
                string va;
                cin>>va;
                if(ss[va])puts(data+k[va]);
//                for(int i=k[va];i<k[va]+(int)strlen(data+k[va])&&i<ci;i++)
//                    putchar(data[i]);
//                printf("\n");
            }
            else if(start=="return")
            {
                fflush(stdin);
                break;
            }
        }
    }
    return 0;
}

伤心(;′⌒`)~

山东省第七届ACM省赛------Memory Leak的更多相关文章

  1. 山东省第七届ACM省赛------Reversed Words

    Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...

  2. 山东省第七届ACM省赛------Triple Nim

    Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...

  3. 山东省第七届ACM省赛------The Binding of Isaac

    The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...

  4. 山东省第七届ACM省赛------Fibonacci

    Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...

  5. 山东省第七届ACM省赛------Julyed

    Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...

  6. 山东省第七届ACM省赛

    ID Title Hint A Julyed 无 B Fibonacci 打表 C Proxy 最短路径 D Swiss-system tournament 归并排序 E The Binding of ...

  7. 山东省第十届ACM省赛参赛后的学期总结

    5.11,5.12两天的济南之旅结束了,我也参加了人生中第一次正式的acm比赛,虽然是以友情队的身份,但是我依旧十分兴奋. 其实一直想写博客来增加自己的能力的,但是一直拖到现在,正赶上老师要求写一份总 ...

  8. 山东理工大学第七届ACM校赛-LCM的个数 分类: 比赛 2015-06-26 10&colon;37 18人阅读 评论&lpar;0&rpar; 收藏

    LCM的个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了 ...

  9. 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10&colon;36 15人阅读 评论&lpar;0&rpar; 收藏

    完美素数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...

随机推荐

  1. 你不知道的Spring配置文件

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...

  2. My Demo Reels

    Some elementary algorithms about discrete differential geometry http://www.cnblogs.com/yaoyansi/p/56 ...

  3. &lt&semi;Error&gt&semi;&colon; CGContextRestoreGState&colon; invalid context 0x0&period; If you want to see the backtrace&comma; please set CG&lowbar;CONTEXT&lowbar;SHOW&lowbar;BACKTRACE environmental variable&period;

    转自这里   出 错原因:设置app的状态栏样式的使用使用了旧的方式,在info.plist里面设置了View controller- based status bar appearance为NO,默 ...

  4. &OpenCurlyQuote;char &ast;&&num;39&semi; differs in levels of indirection from &&num;39&semi;int&&num;39&semi;

    这个问题是有与和系统变量重名导致的,如 char* ans = (char*) malloc(10);系统变量是一个int.

  5. &lt&semi;Win32&lowbar;5&gt&semi;深入浅出Win32的计时器

    说起时间,对于我们搞IT的人来说,那是要多重要有多重要.我觉得有价值的时间是给有抱负和有才能的人准备的,因为他们会充分利用,不会让时间失望…… 呵呵,有点儿说远了,还是回归主题吧 Win32的计时器其 ...

  6. Git总结笔记1-搭建和使用30条简明笔记

    1.环境搭建: Linux:安装git安装包(yum install -y git) Windows:到官网下载安装包 安装完成后配置如下用户名和邮箱: git config --global use ...

  7. Linux下,根据FHS定义出来的每个目录的作用

    (下表摘自<鸟哥的Linux的私房菜>) 在Linux下,根据FHS定义出来的每个目录应该放置的档案内容为: 目录 应放置档案内容 / 根目录 root (/),一般建议在根目录底下只接目 ...

  8. window cmd-常用命令

    1.常用命令 dir 文件列表 cd 改变目录md 创建目录 rd 删除目录 type 显示文件内容 fc 比较目录 attrib 修改文件属性 copy 复制文件 del 删除文件 ren 文件改名 ...

  9. PHP 中如何创建和修改数组?

    PHP中使用array来创建一个数组:array( key=>value , key=>value …… )用方括号的语法来修改数组:$arr[] = value 例如:$arr = ar ...

  10. &lbrack;html&rsqb;Sublime Text添加插件

    今天想在Sublime Text(简称ST)内编写HTML后直接使用浏览器看效果,想添加View in Browser插件,然后遇到奇怪的问题添加插件直接报"找不到有用的插件" 一 ...