Home > Back-end >  Questions about the function return value
Questions about the function return value

Time:11-16

Type
Tdata=https://bbs.csdn.net/topics/array [0.. 3] of byte;

Pdata=https://bbs.csdn.net/topics/^ Tdata;

.

The function Myfunc: pdata;
Var
Data: Pdata;
The begin
New (data);
Result:=data;
end;

The function Myfunc: pdata;
Var
Data: Tdata;
The begin
Result:=@ data;
end;

Excuse me, two kinds of writing this return value is the difference? What for?

CodePudding user response:

 type 
Tdata=https://bbs.csdn.net/topics/array [0.. 3] of byte;
pdata=https://bbs.csdn.net/topics/^ Tdata;//pointer to Tdata type


Type
TForm1=class (TForm)
For: TButton;
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;
A: pdata;


Implementation

{$R *. DFM}


The function Myfunc1: pdata;
Var data: Pdata;
The begin
New (data);
Result:=data;//return pointer type
end;

The function Myfunc2: pdata;
Var data: Tdata;
The begin
Result:=@ data;//return a pointer to the data
end;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
A:=Myfunc1;
A:=myfunc2;
end;


Both can, returns are pdata,

CodePudding user response:

The second is clearly wrong, the address of the local variable do return a value. Leave the function, local variables are recycled. Memory if it can be read, if yao random or keep the original value.
May also be other code reuse.

CodePudding user response:

Suggest using 1

1 standard of writing, version compatibility good

Version 2 writing for D7, will to XE to DEBUG your head exploding

CodePudding user response:

Writing 2 neither of which version of Delphi is unreasonable, because the data is a local variable, it occupied will be released within function exits, the memory at any time are likely to be themselves program or other redistribution,

CodePudding user response:

2 write is wrong, the reason is life cycle is only temporary variables within the function

1 of the writing is right, but the actual is equivalent to the new, it is not necessary to set up a temporary variable
 
The function Myfunc1: pdata;
The begin
New (Result);
end;

CodePudding user response:

2 is a typical errors, return to a local variable reference or pointer,
Because of Delphi function allows any return type, actually don't need to return a pointer to the complex type, the latter is a typical way of C, can be directly:
The function Myfunc: Tdata;
The begin
The Result [0] :=1;
end;

CodePudding user response:

So say, most people learn DEPHI of pointer involved is not much, but the DELPHI support for Pointers and C the same, even we can open a piece of memory making artificial an array

CodePudding user response:

Procedure SetAryHeader (var RevBuffer: Pointer);
Var
AryLen, MemSize: integer;
The begin
If PHeader (RevBuffer) ^. X> 0 then
The begin
AryLen:=PHeader (RevBuffer) ^. X.
MemSize:=PHeader (RevBuffer) ^. Size;//Windows VirtualAlloc ()
MoveMemory (Pointer (DWORD (RevBuffer) + 2 * S_INTEGER), Pointer (DWORD (RevBuffer) + S_HEADER), MemSize - S_HEADER);
ReallocMem (RevBuffer, MemSize S_HEADER + 2 * S_INTEGER);
PDWORD (Pointer (DWORD (RevBuffer) + S_INTEGER)) ^ :=AryLen;//set the dynamic array length,
RevBuffer:=Pointer (DWORD (RevBuffer) + 2 * S_INTEGER);
//Dec (PDWORD (RevBuffer), 1);
//PDWORD (RevBuffer) ^ :=0;//the reference count is greater than 0
//Inc (PDWORD (RevBuffer), 2);
End
The else
The begin
FreeMem (RevBuffer);
RevBuffer:=nil;
end;
end;
  • Related