Recent

Author Topic: [SOLVED] array within class errors with SIGSEGV  (Read 2598 times)

aileron

  • Newbie
  • Posts: 6
[SOLVED] array within class errors with SIGSEGV
« on: July 23, 2015, 02:16:13 am »
I'm new to pascal so bear with me. I've been learning pascal for a little over a month and have had lots of growing pains as I've never been an OOP programmer.  Which is where all my frustrations are coming from.  :-[

So I'm trying to make a stack class of my own for learning purposes and also because I need something that allows me to drop off stuff at the end of the stack. Its one of many brain teasers for my feeble mind.

When the code gets to line 46 it fails while trying to assign 0 to the first element in the array. If I'm not mistaken at this moment in time I haven't changed the size of the array so that's all that should exist.  Of course it could be that the array is non existent at this point and the constructor is trying to write to something that's not there.

I can't get past setting the stackArr[0] to zero.

Code: [Select]
program StackQueue;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

Type
  { TmyStack }
  TmyStack = class(Tobject)
    public
      element: integer;
      indexNum: integer;
      stackArr: array of integer;

      constructor Create;
      procedure setSize(itsNewLength: integer);
      procedure setIndex(IndexNumber: integer);
      procedure push(IndexNumber: integer);

      function pop(IndexNumber: integer): integer;
end;

constructor TmyStack.Create;
begin
  inherited Create;

  // Set the initial index number to 0
  indexNum := 0;      // Probably don't need to do this, but doing it anyways.
  setIndex(indexNum);

end;

{ TmyStack }
procedure TmyStack.setSize(itsNewLength: integer);
begin
  setLength(stackArr, itsNewLength);
end;

procedure TmyStack.setIndex(IndexNumber: integer);
begin
  stackArr[0] := IndexNumber;
end;

procedure TmyStack.push(IndexNumber: integer);
begin

end;

function TmyStack.pop(IndexNumber: integer): integer;
begin

end;

var
  myStack: TmyStack;


begin
  myStack := TmyStack.Create;

  try
    // Set the lengh of the array
    myStack.setSize(25);

  finally
    mystack.Free;

  end;
  writeln('Hit any key to end.');
  readln;

end.

I'm having all kinds of brain farts as to how I can be accessing things within the class from other procedures and functions and all so I apologize if I've really messed it up.

But the question is why can't I assign to index 0 a value? What am I doing wrong?
« Last Edit: July 23, 2015, 08:17:58 pm by aileron »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: array within class errors with SIGSEGV
« Reply #1 on: July 23, 2015, 02:46:28 am »
Code: [Select]
procedure TmyStack.setIndex(IndexNumber: integer);
begin
  if (length(stackArr) < 1) then setlength(StackArr, 1);
  stackArr[0] := IndexNumber;
end;
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

aileron

  • Newbie
  • Posts: 6
Re: array within class errors with SIGSEGV
« Reply #2 on: July 23, 2015, 03:00:02 am »
Figures that was the problem. LOL.

Thank you very much.

 

TinyPortal © 2005-2018