Delphi - ADO 查询和 FillChar 生成错误
我有以下代码:
var wqry:TAdoQuery;
...
FillChar(wSpaces,cSpacesAfter,' ');
try
wqry := TADOQuery.Create(nil);//here the error
wqry.Connection:=...
cSpacesAfter 是一个常量,值为 1035。 wSpaces 是一个本地字符串变量。问题是,创建 TAdoQuery 时我收到以下错误
即使它是法语,我相信你明白了......
如果我评论 FillChar 代码,一切正常。我有通常的编译器指令,没什么特别的。我正在使用 Delphi 7。
有人可以告诉我该代码有什么问题吗?
I have the following code:
var wqry:TAdoQuery;
...
FillChar(wSpaces,cSpacesAfter,' ');
try
wqry := TADOQuery.Create(nil);//here the error
wqry.Connection:=...
cSpacesAfter is a constant and has the value 1035. wSpaces is a local string variable. The problem is that I receive the following error when TAdoQuery is created
even it is in french, I believe you got the idea.....
If I comment the FillChar code, everything works ok. I have the usual compiler directives, nothing special. I'm using Delphi 7.
Can someone tell me what is wrong with that code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
麻烦的代码很可能是
我假设
wSpaces
是字符串类型的代码。字符串变量实际上只不过是指向保存字符串的数据结构的指针。您不需要使用指针语法,因为编译器会为您处理这些问题。因此,这段代码的作用是用 4 个空格字符覆盖保存该指针的变量,然后在变量后面的内容顶部再写入 1031 个空格。简而言之,你将彻底破坏你的记忆。这可以解释为什么
FillChar
可以工作,但下一行代码却痛苦而戏剧性地死去。如果您的字符串确实有 1035 个字符的空间,您可以改为编写:
但是,如果编写可能更惯用:
The troublesome code is most likely this one
I'm assuming that
wSpaces
is of string type. A string variable is in fact nothing more than a pointer to the data structure that holds the string. You don't need to use pointer syntax because the compiler takes care of that for you.So what this code does is overwrite the variable holding that pointer with 4 space characters and then write 1031 more spaces over the top of whatever follows the variable. In short you will completely corrupt your memory. That would explain why the
FillChar
works but the very next line of code dies a painful and dramatic death.If your string indeed had space for 1035 characters your could instead write:
However, if may be more idiomatic to write:
您确定 wSpaces 的大小足以容纳写入后的所有 cSpaces 吗?
Are you sure wSpaces has the size enough to fit all of cSpacesAfter you write to it?