Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Array, Access Violation 1

Status
Not open for further replies.

mrollings

Programmer
Oct 14, 2007
3
GB
I have an array which length is set dynamically, it all works fine the first time the program is loaded. But if a user changes the settings and it needs to be resized it just gives and access violation.



procedure TForm1.MakeSignalArray();
var
SignalArray : array of integer;
b, integer;
begin

setlength(SignalArray,totaltime);

for b:=0 to length(SignalArray)-1 do
begin
SignalArray[t]:=0;
t:=t+1;
end;

end;


If I keep calling this procedure with no change to totaltime, then there is no error untill the program is closed where i get "Invalid pointer operation"

and when totaltime is changed i get an access violation.. :(

any ideas what ive done wrong? thanks in advance

 
It looks like you might consider looking at what t is, and code it a little differently. Also, consider what value "totaltime" is when it enters the code you posted.

This should initialize the array properly.

Code:
setlength(SignalArray, totaltime);
for b:= Low(SignalArray) to High(SignalArray) do
  SignalArray[b]:=0;
 
First, ALWAYS copy and paste your actual code in here. There's a typo in the example you provided. The code as you provided it also doesn't do anything useful, so is a contrived example. Since your variable SignalArray is a local variable, it's not available outside the procedure, and you do nothing inside the procedure other than initialize all elements to 0.

Second, put your code inside code tags.

One problem is that you're using t as an index when you haven't declared the variable or initialized it. There's also no way to tell what totaltime is or if there's a value assigned to it at all.

I'd write your code as this:

Code:
procedure TForm1.MakeSignalArray();
var
  SignalArray: array of integer;
  b: integer;
begin

  setlength(SignalArray, totaltime);

  for b:=0 to High(SignalArray) do
  begin
    SignalArray[b]:=0;
  end;

end;

Am I correct in guessing that this is a school assignment?

Lee
 
@ Glen9999

I didn't realise that you needed to set every value to zero. Is this only necessary if you are going to be making the array a different size and it can't delete no-zero values? I tried setting the array to nil after it was used and to be recreated but I think that destroyed everything.

@ Trollacious

Sorry about that, I didn't copy and paste the code because I thought it might have been easier for people to understand what I meant if I made it up quickly.

Here is the full code:
Code:
...

var
  Form1: TForm1;
  SignalArray : array of integer;
implementation

{$R *.dfm}

...

procedure TForm1.MakeSignalArray();
var
a,b,t, totaltime:integer;

begin

  totaltime:=(length(SignalEdit.text)*strtoint(PulseTimeEdit.Text))+(length(signaledit.Text)+1)*strtoint(TimeGapEdit.text);

  t:=0;

  setlength(SignalArray,totaltime);

  // Time Gap on graphs
  for b:=0 to strtoint(TimeGapEdit.text) do
  begin
  SignalArray[t]:=0;
  t:=t+1;
  end;

  // All binary numbers in signal edit box
  for a:=0 to length(SignalEdit.Text)-1 do
  begin

  if signaledit.text[a+1]='1' then
  begin
      for b:=0 to strtoint(PulseTimeEdit.text) do
      begin
        SignalArray[t]:=100;
        t:=t+1;
      end;
  end
  else
  begin
    for b:=0 to strtoint(PulseTimeEdit.text) do
      begin
        SignalArray[t]:=0;
        t:=t+1;
      end;
  end;

  // Time Gap on graphs
  for b:=0 to strtoint(TimeGapEdit.text) do
  begin
  SignalArray[t]:=0;
  t:=t+1;
  end;

  end;

end;

I'm afraid its not a school assignment, I'm actually a 3rd year physics student and I'm writing a program for my final year project. We're taught fortran on our course, which I think is a bit backwards, and I had a placement over the summer where I learnt Delphi.
 
At what point is the access violation occurring? Have you set break points in the code and stepped through it to find where this happens?

Lee

 
you are not using your loop variable :

Code:
for b:=0 to strtoint(TimeGapEdit.text) do
  begin
  SignalArray[t]:=0;
  t:=t+1;
  end;

change into :

Code:
for b:=0 to strtoint(TimeGapEdit.text) do
  begin
   SignalArray[b]:=0;
  end;

obvious bug...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I didn't realise that you needed to set every value to zero.

That's what the code does that you posted, as far as I could tell.

As for the "fuller version" of the code you posted:

Use proper formatting, indenting, etc. The compiler doesn't care, but it will help you and anyone else that happens to read your code along in understanding it. Seeking to simplify the code will help too.

As for the access violations, you always need to validate where appropriate. At almost every point in the code there's potential for an access violation. As Trollacious says, have you found out where the access violation occurs?

Some validation issues that come to mind in this code
1. Totaltime can not be zero or less.
2. T must be less than or equal to totaltime.

Since you do not use any limit relating to "totaltime", but use an uncontrolled, untested variable (t), it would pay for you to rethink this code.

The code formatted out as I understood it (as an example), and changed a little bit. The change is untested.
Code:
var
  a,b,t, totaltime, savalue:integer;
begin
  totaltime:=(length(SignalEdit.text) * strtoint(PulseTimeEdit.Text))
           + (length(signaledit.Text)+1) * strtoint(TimeGapEdit.text);

  setlength(SignalArray,totaltime);
  // Time Gap on graphs
  for b:=0 to strtoint(TimeGapEdit.text) do
    begin
      SignalArray[t]:=0;
      t:=t+1;
    end;

  // All binary numbers in signal edit box
  for a:=0 to length(SignalEdit.Text)-1 do
    begin
      if signaledit.text[a+1]='1' then
        savalue := 100
      else
        savalue := 0;
      for b:=0 to strtoint(PulseTimeEdit.text) do
        begin
          SignalArray[t]:=savalue;
          t:=t+1;
        end;
    end;

  // Time Gap on graphs
  for b:=0 to strtoint(TimeGapEdit.text) do
    begin
      SignalArray[t]:=0;
      t:=t+1;
    end;
 end;

end;
 
Oh and for floating-point calculations, you might want to be aware that Delphi is pretty backwards in terms of accuracy. If you insist on using it for that, you definitely want to be sure you are getting proper accurate results out of it in each and every case.
 
Glenn, I see that you repost the code with the same error?

he is using t as an index variable:
BUT t is NOT initialized so t COULD BE 0 but could also be anyhing, so the AV may already occur in the the first FOR-loop. or am I missing something here???

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Glenn, I see that you repost the code with the same error?

I noticed after I posted it, that "t:=0;" was missing on the second line. But no way to edit posts and I thought it was easily picked up on so I left it alone.

BUT t is NOT initialized so t COULD BE 0 but could also be anyhing, so the AV may already occur in the the first FOR-loop. or am I missing something here???

The repost was to provide an example of "proper indenting, etc", and wasn't intended to correct anything. I did change a section to illustrate my point about simplification. I don't really understand the application that was posted well enough to be able to try and correct the problem.

As I pointed out, though, T or totaltime (T would be my guess, actually) is the source of the OP's problem. When T equals totaltime, the OP probably needs to do something with SignalArray and set T to 0, but again not enough information was presented as to what the OP is doing with this code.

Don't have the full code, so I can't attempt a fix.
 
Thanks I've got the program working without access violations now! The code the Glen9999 posted sorted it out, so thanks very much :)
 
k, didn't see that.
mrollings, thank glenn with a star please!

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top