Unassign a variable lazarus / 9-Editor ---------------------------------------------------------------------- LazaruX · 2007-11-30 16:46 Hi everybody, with this code: Code: [Select] var  number:integer we assign a variable, this means that a little part of the memory is reserved to be able to fill the maximal allowed integer size. My question is, how do I unassign this variable? How do I free this memory? ---------------------------------------------------------------------- antonio · 2007-11-30 19:55 Why do you need to do so? ---------------------------------------------------------------------- gerardus · 2007-12-02 10:37 Like this: Code: [Select]  { var  number:integer } :wink: Seriously, there is no way, unless you assign memory dynamically for all your variables. But I don't understand why you would have all that trouble just to save a few bytes. Regards, Gerard ---------------------------------------------------------------------- Zaher · 2007-12-03 05:44 Inside the function your variable will deallocated when when finish the function, but if you talk about a huge global variable you can use a memory functions. But inside the function when you finish using a variable, with optimize options for FPC the compiler automatic remove this variable from memory. Code: [Select] function foo(); var   x:Integer; begin ... ...x :=10; ..writeln(x);//after this line x will deallocated, if you compile with optimize mode. ... .. .. .. end; I hope i am aright. ---------------------------------------------------------------------- gerardus · 2007-12-03 09:05 Quote from: "zaher" But inside the function when you finish using a variable, with optimize options for FPC the compiler automatic remove this variable from memory. Interesting. I thought that space was allocated on the stack for local variables like integers. Does the compiler generate code to manipulate the stack after every variable isn't used anymore? Regards, Gerard. ---------------------------------------------------------------------- Marc · 2007-12-03 10:43 The compiler reserves stackspace on entrance of the procedure. Assigning a value doesn't allocate, the space is already there. On exit the stackpointer is restored ---------------------------------------------------------------------- gerardus · 2007-12-03 11:31 Quote from: "Marc"The compiler reserves stackspace on entrance of the procedure. Assigning a value doesn't allocate, the space is already there. On exit the stackpointer is restored But the stack pointer isn't moved during procedure execution by some magic optimizing trick, or is it? ---------------------------------------------------------------------- Marc · 2007-12-03 17:24 what would it optimize ? ---------------------------------------------------------------------- LazaruX · 2007-12-03 18:33 I already new about the variables in the procedure and functions, but when they are assigned globally? Last time I was talking to C programmers and they told me that in C its possible to unassign variables and in Pascal not. I also need this for some little applications. ---------------------------------------------------------------------- RudieD · 2007-12-03 18:59 Don't make it global if you don't want to use it global. ---------------------------------------------------------------------- Zaher · 2007-12-03 19:10 In functions when optimization enabled the compiler push in stack that variable when first time used, then pop it when last time used (not need it any mode). i test it in Delphi, in FPC not sure, but i think it is more powerful than Delphi. ---------------------------------------------------------------------- gerardus · 2007-12-03 20:40 Quote from: "Marc"what would it optimize ? Nothing. Un-optimize, maybe. ---------------------------------------------------------------------- Bart · 2007-12-03 22:51 Quote from: "BPsoftware"Last time I was talking to C programmers and they told me that in C its possible to unassign variables and in Pascal not. I also need this for some little applications. Use pointers for everything then and allocate from the heap. Real C style coders (also called NARG's) do this al the time  :lol: Beware though, this makes it real easy to shoot yourself in the foot. (No flame intended!!!) Bart ---------------------------------------------------------------------- Marc · 2007-12-04 09:45 Quote from: "zaher"In functions when optimization enabled the compiler push in stack that variable when first time used, then pop it when last time used (not need it any mode). i test it in Delphi, in FPC not sure, but i think it is more powerful than Delphi. Modifying the stackpointer for each variable (un)usage is way more inefficient than do it once on procedure entry/exit So I won't call this an optimalization. However, the compiler knows what the lifetime of a variable is, so it can use the same stack location for different variables. You don't have to unset anything, simply not using it is enough. IIRC florian is/was working on this to add this to the compiler.