delphi 7上的错误指针操作错误 [英] The error invalid Pointer Operation on delphi 7

查看:217
本文介绍了delphi 7上的错误指针操作错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

procedure searchAndReceipt;
var
  amt, counter, check: integer;
  gtinStore, qtyStore: array of integer;
  totalCost: real;
begin
  check     := 0;
  totalCost := 0.0;

  write('Enter how many products you are purchasing: ');
  repeat
    readln(amt);
    if (amt > 11) and (amt <= 0) then
      writeln ('Please re-enter how many products are you purchasing with a value between 1-10')
    else
      check:= 1;
  until check = 1;

  SetLength(gtinStore, amt);
  SetLength(qtyStore, amt);
  SetLength(receiptArray, amt);

  for counter:=1 to amt do
  begin
    write('Enter a GTIN code: ');
    repeat
      readln(gtinStore[counter]);
      if (gtinStore[counter] >= 99999999) and (gtinStore[counter] <= 1000000) then
        writeln ('Please re-enter the Gtin Code with a value of 8 digits')
      else
        check:= 1;
    until check = 1;

    check := 0;
    write('Enter the Quantity: ');
    repeat
      readln(qtyStore[counter]);
      if (qtyStore[counter] >= 11) and (qtyStore[counter] <= 0) then
        writeln ('Please re-enter the quantity with a value between 1-10')
      else
        check:= 1;
    until check = 1;
  end;

  assign(stockFile,'stockFile.dat');
  Reset(stockFile);
  counter:=1;
  while not EOF(stockFile) do
  begin
    receiptArray[counter].productName := ('Product Not Found');
    receiptArray[counter].productGTIN := 0;
    receiptArray[counter].productPrice := 0.0;
    inc(counter);
  end;
  read (stockFile, Stock);
  for counter:=1 to amt+1 do
  begin
    while not EOF(stockFile) do
    begin
      read (stockFile, Stock);
      if Stock.productGTIN = gtinStore[counter] then
        receiptArray[counter].productGTIN:= Stock.productGTIN;
      receiptArray[counter].productName:= Stock.productName;
      receiptArray[counter].productPrice:= Stock.productPrice;
    end;
  end;

  assign(receiptFile, 'receipt.txt');
  rewrite(receiptFile);
  for counter:= 1 to amt+1 do
  begin
    if receiptArray[counter].productName = 'Product Not Found' then
    begin
      writeln(receiptFile, 'GTIN: ', gtinStore[counter]);
      writeln(receiptFile, receiptArray[counter].productName);
      writeln(receiptFile, '');
    end
    else
    begin
      writeln(receiptFile, 'GTIN: ',gtinStore[counter]);
      writeln(receiptFile, 'Name: ',receiptArray[counter].productName);
      writeln(receiptFile, 'Quantity: ', qtyStore[counter]);
      writeln(receiptFile, 'Price: £',receiptArray[counter].productPrice*qtyStore[counter]:4:2);
      writeln(receiptFile, '');
      totalCost := ((receiptArray[counter].productPrice * qtyStore[counter]) + totalCost);
    end;
  end;
  choices:=1;
end;

begin
  choices:= 1;
  while choices <> 3 do
  begin;
    writeln('Press 1 to create the stock file');
    writeln('Press 2 to search for an item and print a receipt');
    writeln('Press 3 to exit');
    write('Choice: ');
    readln(choices);
    writeln;
    case choices of
      1: createStock;
      2: searchAndReceipt;
    end;
  end;
end.

我运行此过程(在此之前还有一个过程将库存放入一个文件),这是什么应该做的是把这个库存放在一个文本文件...然而,当我输入了GTIN号码和我的程序产生这个错误的项目数量$ /

I run this procedure (there's another procedure before this that places stock into a file), what this is supposed to do is to take that stock out and place it into a text file... however after I've entered the GTIN number and the quantities of the items my program produces this error


在00002550模块Task_2.exe中的异常EAccessViolation
模块Task_2.exe中的地址00402550处的访问冲突。在shell中读取地址03491DD4。

Exception EAccessViolation in module Task_2.exe at 00002550. Access violation at address 00402550 in module 'Task_2.exe'. Read of address 03491DD4.

,并显示一条消息框,弹出

within the shell, and a message box pops up saying


项目Task_2.exe引发异常类EInvalidPointer与消息'无效指针操作'。进程停止

Project Task_2.exe raised exception class EInvalidPointer with message 'invalid Pointer Operation'. Process Stopped

提前感谢

推荐答案

动态数组是基于0的,但您的代码假定基于1的索引。因此您可以将数组的结尾进行索引,从而导致运行时错误。使用基于0的索引修复代码。这是循环从0到N-1而不是从1到N。

Dynamic arrays are 0-based, but your code assumes 1-based indexing. Hence you index off the end of the array, and hence the runtime errors. Fix the code by using 0-based indices. That is loop from 0 to N-1 rather than from 1 to N.

即使你修复了,你有循环从1运行到N + 1,所以你甚至没有为您的数组分配足够的空间。

Even what you fix that, you have loops that run from 1 to N+1 so you aren't even allocating enough space for your arrays.

您应该在编译器选项中启用范围检查,以便编译器可以发出诊断代码,为您提供更好的错误消息。

You should enable range checking in the compiler options so that the compiler can emit diagnostics code to give you better error messages.

这篇关于delphi 7上的错误指针操作错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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