Delphi - ADO 查询和 FillChar 生成错误

发布于 2024-11-06 14:53:44 字数 454 浏览 6 评论 0原文

我有以下代码:

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-11-13 14:53:44

麻烦的代码很可能是

FillChar(wSpaces,cSpacesAfter,' ');

我假设 wSpaces 是字符串类型的代码。字符串变量实际上只不过是指向保存字符串的数据结构的指针。您不需要使用指针语法,因为编译器会为您处理这些问题。

因此,这段代码的作用是用 4 个空格字符覆盖保存该指针的变量,然后在变量后面的内容顶部再写入 1031 个空格。简而言之,你将彻底破坏你的记忆。这可以解释为什么 FillChar 可以工作,但下一行代码却痛苦而戏剧性地死去。

如果您的字符串确实有 1035 个字符的空间,您可以改为编写:

FillChar(wSpaces[1], cSpacesAfter, ' ');

但是,如果编写可能更惯用:

wSpaces := StringOfChar(' ', cSpacesAfter);

The troublesome code is most likely this one

FillChar(wSpaces,cSpacesAfter,' ');

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:

FillChar(wSpaces[1], cSpacesAfter, ' ');

However, if may be more idiomatic to write:

wSpaces := StringOfChar(' ', cSpacesAfter);
孤蝉 2024-11-13 14:53:44

FillChar 过程使用相同的字节或字符 FillValue FillCount 次填充存储区 Buffer 次。

它主要用于初始化数字数组。它可用于初始化记录和字符串,但应小心避免覆盖长度字段。 StringOfChar 最适合将字符串填充为同一字符。

您确定 wSpaces 的大小足以容纳写入后的所有 cSpaces 吗?

FillChar procedure fills out a section of storage Buffer with the same byte or character FillValue FillCount times.

It is principally used to initialise arrays of numbers. It can be used to initialise records and strings, but care should be used to avoid overwriting length fields. StringOfChar is best for filling out strings to the same character.

Are you sure wSpaces has the size enough to fit all of cSpacesAfter you write to it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文