第七届山东省ACM省赛 H Memory Leak 模拟

本文通过模拟内存泄漏问题,探讨了变量声明与使用过程中可能出现的溢出现象及其两种处理方式。通过对输入字符串长度的判断,文章详细介绍了如何避免超出数组长度限制导致的数据泄露,并提供了一段示例代码。

模拟变量的声明与使用,存在一种溢出的情况,有两种处理方式,要么为每一个变量分配内存空间,要么分配一个大内存空间,记录每个变量在这个内存区间的左右区间,我使用了第一种方式,比较耗费内存。这题有一个坑点,就是输入的内容是在变量名称一个空格之后,也就是内容的头部也可能会有空格,实际上题目说了,但是容易忽略,另外针对第一种方式,需要记录每一个变量的存储空间是否溢出,但是重新赋值之后可以使原本溢出的状态变为未溢出。

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: 3567_1 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: 3567_2 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大学生程序设计竞赛
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;

struct Memory
{
    char name[10100][10100];
    int len[10100];
    char content[10100][10100];
    int num;
    bool over[10100];
    bool used[10100];
};

Memory mem;
map<string,int> n2p;

void init()
{
    mem.num=0;
    n2p.clear();
    memset(mem.used,false,sizeof(mem.used));
}
void show(int pos)
{
    printf("pos=%d len=%d over=%d content=%s\n",pos,mem.len[pos],mem.over[pos],mem.content[pos]);
}

void inputdef()
{
    char tmp[10100];
    int len,p;
    bool flag=true;
    scanf("%s",tmp);

    while(flag)
    {

        scanf("%s",tmp);
        len=strlen(tmp);
        if(tmp[len-1]==';')
            flag=false;
        p=0;
        while(tmp[p]!='[') p++;
        tmp[p]='\0';
        strcpy(mem.name[mem.num],tmp);
        sscanf(tmp+p+1,"%d",&mem.len[mem.num]);
        n2p[string(mem.name[mem.num])]=mem.num;
        mem.num++;

    }
}



void inputop()
{
    char tmp[10100];
    char op[20];
    char name[10100];
    char content[10100];
    int pos,len;
    while(~scanf("%s%s",op,name))
    {
        if(op[0]=='r')
            return;
        if(op[0]=='g')
        {
            gets(tmp);
            strcpy(content,tmp+1);
            pos=n2p[string(name)];
            strcpy(mem.content[pos],content);
            len=strlen(content);
            mem.over[pos]=false;
            if(len>=mem.len[pos])
            {
                mem.content[pos][mem.len[pos]]='\0';
                mem.over[pos]=true;
            }
            mem.used[pos]=true;
        }
        else
        {

            pos=n2p[string(name)];
            if(mem.used[pos]==false)
            {
                ;
            }
            else if(mem.over[pos]==true)
            {
                while(mem.over[pos]==true)
                {
                    printf("%s",mem.content[pos++]);
                }
                printf("%s",mem.content[pos]);
            }
            else
            {
                printf("%s",mem.content[pos]);
            }

            printf("\n");
        }
    }




}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        inputdef();
        inputop();
    }
    return 0;
}
 

查看原文: http://colorfulshark.cn/wordpress/memory-leak-1055.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值