大家好,我是新人,以前用Delphi写过小程序,现在想花时间系统的学习一下,看了一个别人写的代码,首先提个问题,好简单,大家别笑话我啊:
var
  Size: Integer;
  Buffer: PChar;
begin
    Size := FileSize(F);
    GetMem(Buffer, Size);
    try
        //
    finally
      FreeMem(Buffer);
    end;
end;
我在想:
如果GetMem(Buffer, Size);这句话出异常了怎么办?是不是应该对这句话做保护呢?是否应该写成:
var
  Size: Integer;
  Buffer: PChar;
begin
    Size := FileSize(F);
    try
        GetMem(Buffer, Size);
        try
            //
        finally
          FreeMem(Buffer);
        end;
    end;
end;另外我想深入学习一下Windows编程,比如说Windows程序的结构,Delphi程序的编译过程,连接过程,VCL的消息流程等等,Delphi里面向对象是如何实现的,比如说虚拟函数指针表的介绍等等,请各位帮我推荐一本书好吗?小女子谢谢大家了。

解决方案 »

  1.   

    小女子谢谢大家了真的假的????推荐几本书
    1、DELPHI的帮助
    2、VCL源码
    3、Windows核心编程
    4、DELPHI5开发人员指南
      

  2.   

    try
      GetMem(Buffer, Size);
      FreeMem(Buffer);
    catch
       //
    end;
      

  3.   

    try
      GetMem(Buffer, Size);
    except
      //出错的处理
    finally
      FreeMem(Buffer);
    end;李维的《VCL架构剖析》
      

  4.   

    TO zhuminghua() 
    try
      GetMem(Buffer, Size);
    except
      //出错的处理
    finally
      FreeMem(Buffer);
    end;
    你的这段代码,好象是无法通过编译的,Delphi里好象没有java那样的catch和finally共存的吧?TO Cassava(车超)
    try
      GetMem(Buffer, Size);
      FreeMem(Buffer);
    catch
       //
    end;
    也就是说其实如果语句只是GetMem,try...catch结构足够了,因为如果分配不成功直接跳到异常处理里,而成功了的话在结束这段话之前是无论如何都会调用FreeMem的,没有必要再写finally了,是吗?但是如果还有其他的语句,比如先打开文件,再读文件到缓冲区里去,就象Delphi里的帮助:
    var
      F: file;
      Size: Integer;
      Buffer: PChar;
    begin
      AssignFile(F, 'test.txt');
      Reset(F, 1);
      try
        Size := FileSize(F);
        GetMem(Buffer, Size);
        try
          BlockRead(F, Buffer^, Size);
          ProcessFile(Buffer, Size);
        finally
          FreeMem(Buffer);
        end;
      finally
        CloseFile(F);
      end;
    end;
    针对这段代码我有几个疑问:
    1、我实验了一下,如果文件不存在,在Reset(F, 1)的时候就会出异常,为什么不加在异常保护代码段里?
    2、Size := FileSize(F)这句话是否有可能产生异常?平常编程的时候如何知道一句话是否有可能产生异常?
    3、就这段代码要做的事情而言,我大概的读了一下帮助文件,有几个问题问一下大家
    首先关于Reset的:
    (1)Delphi syntax:
    procedure Reset(var F [: File; RecSize: Word ] );
    RecSize is an optional expression, which can be specified only if F is an untyped file.
    这里的"untyped file"是什么意思?是指“F: file”这种没有指定文件类型的声明吗?
    type
      PhoneEntry = record
        FirstName, LastName: string[20];
        PhoneNumber: string[15];
        Listed: Boolean;
      end;
      PhoneList = file of PhoneEntry;
    PhoneList是不是应该属于typed file了呢?(2)If F is assigned an empty name, such as AssignFile(F, ''), then after the call to Reset, F refers to the standard input file.
    我不明白“standard input file”是什么意思?请各位帮忙解释一下,谢谢了。(3)If F is a text file, F becomes read-only.
    只指调用Reset以后,F 的 FileMode 变成 ReadOnly了吗?
    还是如果F是text File,那么 FileMode 在调用 Reset以前就默认是 readonly?(4)关于{$I+},是不是说如果我写了{$I+}就以异常的形式来处理IO错误,而写了{$I-}不会产生异常,要use IOResult to check for I/O errors?(5)关于FileSize的问题:
    Delphi syntax:
    function FileSize(var F): Integer;
    DescriptionIn Delphi code, call FileSize to determine the size of the file specified by the file variable F. The size is expressed as the number of records in a record file. Thus:If the file is declared as a file of byte, then the record size defaults to one byte, and FileSize returns the number of bytes in the file.
    The Reset procedure can set the record size (in bytes) when it opens the file. In this case, FileSize returns the number of records in the file.Note: If the file is declared as an untyped file and you don't specify a record size when you call Reset, then FileSize assumes a record size of 128. That is, FileSize gives the number of bytes divided by 128.我们经常写的F:file,这里的F应该是无类型的吧?f: file of Byte,这里的f就是file of byte文件吧?TO wudi_1982 是真的,呵呵。以前糊里糊涂的也编了一些程序,有windows的也有embed的,现在想整理一下思路,从头学起,有时候自己理解的东西不一定对,所以来这里向大家确认一下,请各位以后多多的帮助我,谢谢大家了。
      

  5.   

    Delphi5开发人员指南,去盒子上下载看看吧www.2ccc.com
      

  6.   

    <<Windows核心编程>>
    李维的《VCL架构剖析》
      

  7.   

    李维的《VCL架构剖析》
    刘艺《delphi面向对象编程思想》