Reputation: 99
I realized (the hard way, an hour of debugging) that my Delphi 2010 compiler with optimization off (and record alignment of 1 byte) does not allocate memory for unused variables. If they are defined like:
var x,y,z:longint; a1,a2,a3:whatever;
and y gets unused (actually, i removed a form from the project temporarily, which references that variable exclusively), then z is assigned the address of x+4. Nice except that i have a lot of code which will now mix z with a1. Can this be controlled by some switch? Thank you.
Upvotes: 0
Views: 382
Reputation: 43023
This won't work. The compiler will optimize all unused variables.
What you can do is to define a packed record, as such:
type
TMyData = packed record
x,y,z: longing;
a1,a2,a3: whatever;
end;
Then the allocated TMyData
will always be allocated as one, including all internal variables, even if they are not used in your code.
For temporary variables allocated on the stack, you may use:
function DoSomething();
var loc: packed record
x,y,z: longing;
a1,a2,a3: whatever;
end;
begin
loc.x := 10;
...
Upvotes: 4