【问题标题】:FPC exitcode 201 while using ansistring. String does not do that使用 ansistring 时 FPC 退出代码 201。字符串不这样做
【发布时间】:2015-01-08 04:23:44
【问题描述】:

我有一个问题。我编写了这个小程序,它运行良好,直到我将 ss2string 更改为 ansistring。我需要使用 ansistring,因为它会超过 255 个字符。感谢您的回复。

    {$H+}
program Test;
uses Crt;
var s,s2:string;
    konec,radek:boolean;
    i,a,z:integer;
begin
  ClrScr;
  s:='';
  s2:='';
  i:=0;
  a:=0;
  z:=1;
  konec:=false;
  radek:=false;
  repeat
    s2:='';
    readln(s2);
    s:=s+s2;
  until s2='';
  while konec=false do begin
    while radek=false do begin
      a:=a+1;
      if length(s)+1=a then begin
        radek:=true;
        s:='';
        if a<60 then writeln(s2);   
      end;
      if not (a=length(s)+1) then begin
      if s[a]=' ' then
        i:=a;
      s2:=s2+s[a];
      if not (s[a]=' ') then
      if a=60 then begin
        radek:=true;
        delete(s2,i,60-i+1);
        writeln(s2);
        s2:='';
        delete(s,z,i);
      end;
      if (s[a]=' ') and (a=60) then begin
        radek:=true;
    writeln(s2);
    s2:='';
    delete(s,z,i);
      end;
      end;
    end;
    radek:=false;
    a:=0;
    if (s='') then konec:=true;
  end;
  readkey;
end.

【问题讨论】:

    标签: string pascal ansistring


    【解决方案1】:

    要知道的核心是shortstring通常没有问题越界字符串访问(s[a]当a不在允许范围内(a>=1)和(a

    您在此代码中将 S 设置为 '':

     if length(s)+1=a then begin
        radek:=true;
        s:='';
        if a<60 then writeln(s2);   
      end;
    

    但你没有重置“a”。因此,以下条件为真,并且 s[a] 访问炸弹。

      if not (a=length(s)+1) then begin
      if s[a]=' ' then
        i:=a;
    

    如何解决这个问题留给读者作为练习。

    学习使用范围检查,你很快就会发现这样的问题(提示在命令行中添加 -Cr -gl)

    【讨论】:

      猜你喜欢
      • 2016-08-05
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多