Recent

Author Topic: Pointer and nodes - help  (Read 2448 times)

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Pointer and nodes - help
« on: September 22, 2014, 07:30:15 pm »
Hi,

I'm having difficulty understanding pointers and nodes. I've tried after learning some theory to use this in pascal. Although it's giving me the usual "SIGSEGV" error whenever I input data:


This is my code:
Code: [Select]
Program PointerAndNodes;

Uses
    SysUtils, Classes , Crt;

Type
  NewPtr = ^Integer;

Var
  NodeOne, NodeTwo : NewPtr;
Begin
  New(NodeOne);
  Writeln('Assign a number to NodeOne: ');
  Readln(NodeOne^);

  Writeln('Now I''l link NodeOne to NodeTwo!');
  NodeOne := NodeTwo;

  Writeln('Value of NodeTwo = ',NodeTwo^);
  Writeln(NodeTwo^);
  Dispose(NodeOne);

 Readln;

End.
ShowMessage('Yes');

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Pointer and nodes - help
« Reply #1 on: September 22, 2014, 07:39:22 pm »
where is new(NodeTwo)? erm and dispose(nodeOne) after you have replaced the pointer with an empty one?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Legolas

  • Full Member
  • ***
  • Posts: 117
    • http://itaprogaming.free.fr
Re: Pointer and nodes - help
« Reply #2 on: September 22, 2014, 07:41:10 pm »
What taazz said, and  NodeTwo := NodeOne too :)

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Pointer and nodes - help
« Reply #3 on: September 22, 2014, 07:41:18 pm »
Try:
NodeTwo := NodeOne;

<<edit>>
... 8 seconds later...  :D
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Re: Pointer and nodes - help
« Reply #4 on: September 22, 2014, 07:47:22 pm »
Thank you for stating my stupidity :P. Although, whenever I try to print out the data that I have assigned to the NodeTwo pointers, it gives me a random number. OMG, DO I HATE THIS CONCEPT.
ShowMessage('Yes');

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Pointer and nodes - help
« Reply #5 on: September 22, 2014, 07:52:28 pm »
did you really reversed the assignement from NodeOne := NodeTwo; to NodeTwo := NodeOne; ? If yes then there shouldn't be any random numbers printed (by the way do not call new(nodetwo) if you reverse the assignment) if not and you simple added the new(nodeTwo) as I mentioned earlier then its expected to see random numbers.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Pointer and nodes - help
« Reply #6 on: September 22, 2014, 08:07:11 pm »
So... are we going to see your post with code disappear again like in the other topic you wrote?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018