我用的是delphi Enterprise 6.0 build6.163 + update 2 + Run-Time Lib Patch 2
win98third下安装 默认设置该程序用到一个memo1,一个button1,一个button2。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
var pch: pchar;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
Pc: PChar;
S: String;begin
S := 'helloworld';
memo1.Lines.Add(s + ' :S的内容');
Memo1.Lines.Add(IntToStr(Integer(@S)) + ' :S的地址');
Pc := PChar(S);
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
//Pc := 'kkkkkk';
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
S := '1234567890'; //Pc := 'FFFFFF';
Memo1.Lines.Add(pc^ + ' :Pc^的值');
Memo1.Lines.Add(pc + ' :Pc的值');
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
pc^ := 'C';
Memo1.Lines.Add(pc^ + ' :Pc^的值');
Memo1.Lines.Add(pc + ' :Pc的值');
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
memo1.Lines.Add(s + ' :S的内容');
Memo1.Lines.Add(IntToStr(Integer(@S)) + ' :S的地址');
pch := pchar(s);
memo1.Lines.Add(pch +' :pch的内容');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Lines.Add(pch +' :pch的内容');
end;end.这个程序在Delphi中运行时(F9),按button1,按button2都很正常,但是当直接双击执行已生成的Project1.exe并按button1时却报错:
“Access violation at address 00457751 in module 'project1.exe'. Write of address 00457894.”
按button2没事。
怪死了,为什么脱离Delphi就会报错?
win98third下安装 默认设置该程序用到一个memo1,一个button1,一个button2。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
var pch: pchar;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
Pc: PChar;
S: String;begin
S := 'helloworld';
memo1.Lines.Add(s + ' :S的内容');
Memo1.Lines.Add(IntToStr(Integer(@S)) + ' :S的地址');
Pc := PChar(S);
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
//Pc := 'kkkkkk';
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
S := '1234567890'; //Pc := 'FFFFFF';
Memo1.Lines.Add(pc^ + ' :Pc^的值');
Memo1.Lines.Add(pc + ' :Pc的值');
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
pc^ := 'C';
Memo1.Lines.Add(pc^ + ' :Pc^的值');
Memo1.Lines.Add(pc + ' :Pc的值');
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
memo1.Lines.Add(s + ' :S的内容');
Memo1.Lines.Add(IntToStr(Integer(@S)) + ' :S的地址');
pch := pchar(s);
memo1.Lines.Add(pch +' :pch的内容');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Lines.Add(pch +' :pch的内容');
end;end.这个程序在Delphi中运行时(F9),按button1,按button2都很正常,但是当直接双击执行已生成的Project1.exe并按button1时却报错:
“Access violation at address 00457751 in module 'project1.exe'. Write of address 00457894.”
按button2没事。
怪死了,为什么脱离Delphi就会报错?
解决方案 »
- 请教高手 delphi 访问WebService实现文件下载,错误:SOAP Response Packet: result element expected, received
- 求助:DELPHI调用VC++DLL问题
- 怎样获得本机已经设置成共享的目录名称?用什么方法可以删除共享?
- 求一随机数生成函数 这样问可能好点 :)
- clentdata中的afterscroll时间是什么时候触发的?
- 做项目郁闷,升四角高兴,散分了。
- 求救:一个非delphi问题。急!!!!
- 关于demo~~
- 怎么做这种工具栏啊?
- help!!!高分求一本delphi书籍!!!!!!!!!!!!!!!
- 只因为我的问题太菜,也许根本就没人在意我这个小菜鸟
- 大家看看
说的对,只在集成环境下测试看来是不保险的!
但是为何会错呢,对指针的动态变量赋值有错吗?
怎么试的
pc^ := 'C';
你能给一个常量赋值么?
另外,一般PChar类型的指针是以0为字符串的末尾的。因此如果你乱赋值的话就会出错了。因此可以说你的程序中致命的错误就是pc^:=C。内存乱套了。呵呵。
Memo1.Lines.Add(IntToStr(Integer(@S)) + ' :S的地址');
不能得到S的地址,应该是
Memo1.Lines.Add(IntToStr(Integer(@S[1])) + ' :S的地址');因为本质上,S也是一个指针,@S是S这个变量的地址。其实,pc就是指向S[1]的。然后,我发现一个很有趣的事实:
一旦引用了s[n]这样的语句,S指向的地址发生了变化!
请比较这两段程序:
S := 'helloworld';
memo1.Lines.Add(s + ' :S的内容');
Memo1.Lines.Add(IntToStr(Integer(@S[1])) + ' :S的地址');
Pc := PChar(S);
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');
和
S := 'helloworld';
memo1.Lines.Add(s + ' :S的内容');
Pc := PChar(S);
Memo1.Lines.Add(IntToStr(Integer(@S[1])) + ' :S的地址');
memo1.Lines.Add(inttostr(integer(@pc^)) + ' :Pc的地址');仅仅是顺序不同,S和PC的地址就不一样!如果使用第一种方式,在以后的语句中,pc^:='C'就没有错。第二种方式就有错。所以我有以下的猜想:
S在没有被s[n]的方式(可能还有其他方式)引用前,是指向一个常量的空间,一旦被引用后,Delphi把常量空间中的字符串拷贝到变量空间中,并且S指向它。所以,之所以楼主的程序 pc^:='C'出错,是因为此时pc指向的是常量空间,不可写。加入在此语句前加入 s[1]:='a' 这样的语句,就不会出错了!
当然 PC^:= 'C'出错。
我觉得你说的不对。