TPixelQueueItem = record
PrevPoint: TImagePoint;
NextPoint: TImagePoint;
end; TPixelQueue = array of array of TPixelQueueItem;
var
PixelQueue: TPixelQueue;
begin
SetLength(PixelQueue,ImageHeight,ImageWidth);
FillChar(PixelQueue,ImageWidth * ImageHeight * SizeOf(TPixelQueueItem),0);{这句不行!}
end;如果循环清零,效率太慢:
for I := 0 to High(PixelQueue) do
FillChar(PixelQueue[I],ImageWidth * SizeOf(TPixelQueueItem),0);
PrevPoint: TImagePoint;
NextPoint: TImagePoint;
end; TPixelQueue = array of array of TPixelQueueItem;
var
PixelQueue: TPixelQueue;
begin
SetLength(PixelQueue,ImageHeight,ImageWidth);
FillChar(PixelQueue,ImageWidth * ImageHeight * SizeOf(TPixelQueueItem),0);{这句不行!}
end;如果循环清零,效率太慢:
for I := 0 to High(PixelQueue) do
FillChar(PixelQueue[I],ImageWidth * SizeOf(TPixelQueueItem),0);
解决方案 »
- delphi如何连sqlserver?
- 大家帮看一下,这个是用什么控件做的?
- 谁有使用TreeView的例子,给我粘一段,谢谢!来者有分!
- 为什么出现乱码?
- 急!我中病毒了!屏幕上出现好多绿色的小色点!开机自检内存的时候也有!CMOS也进不去!请问这是什么病毒啊!能不能杀掉啊!
- delphi里的指针和c里地指针有什么区别呢?
- 有关用DELPHI调用C++编写的DLL的简单问题
- 一道真正的难题困惑着Delphi的每位同仁!
- 在DELPHI的图象处理中,怎样将GIG转成BMP或BMP转成GIF,各位大虾帮帮忙!
- 发放两个管理软件的源代码,机房管理系统和仪器管理系统,要者可以交换,给分也行//牛虻
- 怎样让delphi自动添加价格
- 南昌之行遭遇黑公司,切身利益遭到侵犯!鬱悶!>>续集
动态数组很怪,FillChar和Move似乎都不支持帮你顶,也许会有高手知道
PixelQueue: TPixelQueue;
begin
SetLength(PixelQueue,100,100);
.... SetLength(PixelQueue,0,0);
end;
用 zeroMemory也是
SetLength(PixelQueue,0,0);
这个 不是等则于
赋值NIl吗。应该不可以的啦,实在是对于里面的机制不了解呀。
我主要是想在使用这个二维数组前,把内容清零,查看帮助
SetLength(PixelQueue,100,100);并不自动把内存清零。
SetLength(PixelQueue,0,0);相当于把数组释放了,我还没有使用哩,不能用它!
只是把指向数组的指针数组清0了。当然会出现你那个问题。
var a:array of array of integer;
p:pinteger;
begin
setlength(a,10,10);
a[3,3]:=3;
p:=@a[3,3];
setlength(a,0,0);
showmessage(inttostr(p^));
end;显示他的内存仍然还是存储它原先的直
program Project1;
{$APPTYPE CONSOLE}
uses windows;
type
TPixelQueueItem = record
PrevPoint: TPoint;
NextPoint: TPoint;
end; TPixelQueue = array of array of TPixelQueueItem;
var
PixelQueue: TPixelQueue;
i,j:Integer;
begin
SetLength(PixelQueue,100,100);
FillChar(PixelQueue[0,0],100* 100 * SizeOf(TPixelQueueItem),0);{这句不行!}
for i:=0 to 99 do
begin
writeln('');
for j:=0 to 99 do
begin
write(PixelQueue[i,j].PrevPoint.X);
end;
end;end.
SetLength(PixelQueue,100,100);
FillChar(PixelQueue[0,0],100* 100 * SizeOf(TPixelQueueItem),0);{这句不行!}
for i:=0 to 99 do
begin
writeln('');
for j:=0 to 99 do
begin
write(PixelQueue[i,j].PrevPoint.X);
end;
不行的那句是因为越界了
program Project1;
{$APPTYPE CONSOLE}
uses windows;
type
TPixelQueueItem = record
PrevPoint: TPoint;
NextPoint: TPoint;
end;
TPixelQueue = array of array of TPixelQueueItem;
var
PixelQueue: TPixelQueue;
i,j:Integer;
begin
SetLength(PixelQueue,100,100);
FillChar(PixelQueue[0,0],100* 100 * SizeOf(TPixelQueueItem),0);{这句行!}
for i:=0 to 99 do
begin
writeln('');
for j:=0 to 99 do
begin
write(PixelQueue[i,j].PrevPoint.X);
end;
end;
end.
SetLength(PixelQueue,100,100);
FillChar(PixelQueue[0,0],100* 100 * SizeOf(TPixelQueueItem),0);{这句行!}
for i:=0 to 99 do
begin
writeln('');
for j:=0 to 99 do
begin
write(PixelQueue[i,j].PrevPoint.X);
end;
end;
PixelQueue[55,55].PrevPoint.x:=1;
write(PixelQueue[i,j].PrevPoint.X);
readln; end;
不要直接运行,用cmd 到命令提示符下运行,比如:e:\project1.exe 会发现runtime error
$01010101的十进制结果。
而如果他带了n个班,不管他的n个半在教学楼里是否挨在一起,他都得每次都进入教室,宣布事情除非他有本事把学校已经排好的班按他自己的方法重新排...
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TPixelQueueItem = packed record
PrevPoint: TPoint;
NextPoint: TPoint;
end;
TPixelQueue = array of array of TPixelQueueItem;var
Form1: TForm1;
PixelQueue: TPixelQueue;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
// SetLength(PixelQueue,100,100);
GetMem(PixelQUeue,100*100 * SizeOf(TPixelQueueItem));
ShowMessage(IntToStr(PixelQueue[0,0].PrevPoint.X));
FillChar(PixelQueue,99* 99 * SizeOf(TPixelQueueItem),0);{这句行!}
end;procedure TForm1.Button1Click(Sender: TObject);
var
i,j:Integer;
begin
StringGrid1.RowCount:=100;
StringGrid1.ColCount:=100;
for i:=1 to 99 do
for j:=1 to 99 do
StringGrid1.Cells[i,j]:=IntToStr(PixelQueue[i,j].PrevPoint.X);end;end.
//Sorry,可能Delphi中的动态数组有特殊的机制。
SetLength(PixelQueue, 10);
SetLength(PixelQueue[0], 0);
SetLength(PixelQueue[1], 1);
SetLength(PixelQueue[2], 2);
SetLength(PixelQueue[3], 3);
SetLength(PixelQueue[4], 4);
SetLength(PixelQueue[5], 5);
SetLength(PixelQueue[6], 6);
SetLength(PixelQueue[7], 7);
SetLength(PixelQueue[8], 8);
SetLength(PixelQueue[9], 9);
//就是说有可能出现不是矩形的二维数组(二级下标信息因此也必须保存)~~
//即使是从0位置开始清空也会把二级下标信息给清掉~~
//这样就要选一个折中的办法,循环+FillChar()~~type
TImagePoint = TPoint;
TPixelQueueItem = record
PrevPoint: TImagePoint;
NextPoint: TImagePoint;
end;
TPixelQueue = array of array of TPixelQueueItem;
var
PixelQueue: TPixelQueue;
ImageHeight, ImageWidth: Integer;
I: Integer;
begin
ImageHeight := 10;
ImageWidth := 10;
SetLength(PixelQueue, ImageHeight, ImageWidth);
for I := Low(PixelQueue) to High(PixelQueue) do
if Length(PixelQueue) > 0 then
FillChar(PixelQueue[I][Low(PixelQueue[I])],
Length(PixelQueue[I]) * SizeOf(TPixelQueueItem), $FF);
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TPixelQueueItem = packed record
PrevPoint: TPoint;
NextPoint: TPoint;
end;
TPixelQueue = array of array of TPixelQueueItem;var
Form1: TForm1;
PixelQueue: TPixelQueue;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
SetLength(PixelQueue,100,100);
end;procedure TForm1.Button1Click(Sender: TObject);
var
i,j:Integer;
begin
StringGrid1.RowCount:=100;
StringGrid1.ColCount:=100;
for i:=0 to 99 do
for j:=0 to 99 do
StringGrid1.Cells[i,j]:=IntToStr(PixelQueue[i,j].PrevPoint.X);end;end.
for i := 0 to 2 do
begin
for j := 0 to 2 do
begin
PixelQueue[i][j].PrevPoint.X := 111;
PixelQueue[i][j].PrevPoint.Y := 222; PixelQueue[i][j].NextPoint.X := 333;
PixelQueue[i][j].NextPoint.Y := 444;
end; end; for i := 0 to 2 do
begin
FillChar(PixelQueue[i][0], 3*sizeof(TPixelQueueItem), 0);
end;
uses Windows;begin
PInteger(nil)^ := 100;
end.//就算有异常,控制台程序也不会出对话框啊,就算try-except也没用~~
TImagePoint = record
X,Y: Word;
end; TImageRect = record
Left,Top,Right,Bottom: Word;
end; TPixelQueueItem = record
PrevPoint: TImagePoint;
NextPoint: TImagePoint;
end; TPixelQueue = array of array of TPixelQueueItem;
看看SetLength是否自动清零:
For a long-string or dynamic-array variable, SetLength reallocates the string or array referenced by S to the given length. Existing characters in the string or elements in the array are preserved, but the content of newly allocated space is undefined. The one exception is when increasing the length of a dynamic array in which the elements are types that must be initialized (strings, Variants, Variant arrays, or records that contain such types). When S is a dynamic array of types that must be initialized, newly allocated space is set to 0 or nil.帮助里说的明白,只有Strings, Variants, Variant arrays, or records that contain such types才自动清零。
我的结构体是由word构成的,所以SetLength不会自动清零,如果是初始为0,很可能是内存块中的随机值,不敢用啊!!
PrevPoint: integer;
NextPoint: integer;
end; TPixelQueue = array of array of TPixelQueueItem;var
PixelQueue: TPixelQueue;
i,j:integer;
begin
SetLength(PixelQueue,3000,3000);
for i := 0 to 2999 do
begin
for j := 0 to 2999 do
begin
PixelQueue[i][j].PrevPoint := 111;
PixelQueue[i][j].PrevPoint := 222; PixelQueue[i][j].NextPoint := 333;
PixelQueue[i][j].NextPoint := 444;
end; end;
PixelQueue := nil;
SetLength(PixelQueue,3000,3000);
for i := 0 to 2999 do
begin
for j := 0 to 2999 do
begin
PixelQueue[i][j].PrevPoint := 111;
PixelQueue[i][j].PrevPoint := 222; PixelQueue[i][j].NextPoint := 333;
PixelQueue[i][j].NextPoint := 444;
end; end; end;经测试,没有出现内存泄漏, 并且能正常清零,赋值!
var
PixelQueue: TPixelQueue;
i,j:integer;
begin
SetLength(PixelQueue,3000,3000);
for i := 0 to 2999 do
begin
for j := 0 to 2999 do
begin
PixelQueue[i][j] := 111;
end; end;
//PixelQueue := nil;(为了测试将这句暂时注一下,实际中一定加上)
SetLength(PixelQueue,3000,3000);
for i := 0 to 2999 do
begin
for j := 0 to 2999 do
begin
PixelQueue[i][j] := 222; end; end; end;
The one exception is when increasing the length of a dynamic array in which the elements are types that must be initialized (strings, Variants, Variant arrays, or records that contain such types). 从上下文看,SetLength仅仅对(strings, Variants, Variant arrays, or records that contain such types)清零。
1. 如果SetLenth中的S作为动态数组其中的元素是strings, Variants, Variant arrays, or records that contain such types)那么,执行SetLenth后,增加的空间内容清0。其它的情况,比如说S就是一个String类型的变量,那么SetLenth后,其空间内的值是不定的。2.给动态数组清0, 只需将动态数组变量的赋nil,然后再次SetLenth即可!
把nil赋值给动态数组:
SA:=nil; //释放S A