Recent

Author Topic: [SOLVED] Variable changing  (Read 5590 times)

poed6xDMzRp7

  • New Member
  • *
  • Posts: 23
[SOLVED] Variable changing
« on: March 20, 2015, 07:16:48 am »
I do not know how to explain this...:

I have this piece of code:
Code: [Select]
dplaces := $01 shl (4*dplaces)-1;

num1.p2 := num1.p2 and dplaces;
num2.p2 := num2.p2 and dplaces;

num1.p2 := num1.p2+(dplaces+1);
If I am not mistaken, if "dplaces = 2" the first line must result in 255. But when I am debugging the program it keeps changing its value, sometimes when I step into the code it results in 255 then add some code, recompile, debug and it is at different value (32767, 3, etc...).

What am I doing wrong?

Thank you!
« Last Edit: March 21, 2015, 08:49:02 pm by BlueKnight »

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Variable changing
« Reply #1 on: March 20, 2015, 07:26:34 am »
Do not use any optimization levels when debugging your code. Disable it by giving -O- as parameter to FPC compiler.

poed6xDMzRp7

  • New Member
  • *
  • Posts: 23
Re: Variable changing
« Reply #2 on: March 20, 2015, 07:32:04 am »
Do not use any optimization levels when debugging your code. Disable it by giving -O- as parameter to FPC compiler.
I already checked for that. Optimization is off, but to clear any doubts I inserted the -O- to the compiler command and the problem persists.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Variable changing
« Reply #3 on: March 20, 2015, 07:33:49 am »
Do you initialize dplaces to zero or some other value somewhere in your code?

poed6xDMzRp7

  • New Member
  • *
  • Posts: 23
Re: Variable changing
« Reply #4 on: March 20, 2015, 07:36:08 am »
Do you initialize dplaces to zero or some other value somewhere in your code?
It is passed as parameter of the "function". When debugging it is at 2 and after the line it is at an unexpected value that changes whenever I add or remove code from the source.

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Variable changing
« Reply #5 on: March 20, 2015, 08:32:33 am »
Better show us the full code that gives the unexpected result.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Eugene Loza

  • Hero Member
  • *****
  • Posts: 578
Re: Variable changing
« Reply #6 on: March 20, 2015, 09:21:31 am »
Range check error possible.
try compiling with {$Q+}{$R+}. I always leave those 'on' until the program is finished.
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

poed6xDMzRp7

  • New Member
  • *
  • Posts: 23
Re: Variable changing
« Reply #7 on: March 20, 2015, 03:43:42 pm »
Range check error possible.
try compiling with {$Q+}{$R+}. I always leave those 'on' until the program is finished.
The problem seems to have stopped after this.

But if I remove the directives... The problem comes back + if I change the variable type it simply crashes with a SIGSEGV. I continue not understanding...

Will I have to compile my final program with those directives?
« Last Edit: March 20, 2015, 04:20:27 pm by BlueKnight »

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Variable changing
« Reply #8 on: March 20, 2015, 05:27:21 pm »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

poed6xDMzRp7

  • New Member
  • *
  • Posts: 23
Re: Variable changing
« Reply #9 on: March 20, 2015, 05:45:55 pm »
Better show us the full code that gives the unexpected result.
It seems it was a problem somewhere else because I started a new project with the same code, saved to an empty folder and the problem seems to have vanished.

I consider it solved for now. Thank you!

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Variable changing
« Reply #10 on: March 20, 2015, 05:50:53 pm »
It seems it was a problem somewhere else
It (almost) always is; so showing the full source code is the only way to get to the real problem.
Good that it works now.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018