Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pointer  reallocate memory

Posted on 1999-07-17
15
Medium Priority
?
204 Views
Last Modified: 2010-04-06
my pointer points on records  after loading file from disk into memory using TMemoryStream . How may I add a new record into memory(not into file !!) so that I could address to this new record through PRec^[N - new added record]. I don't want work with disk, all operations must be only with memory . ReallocMem(NewSize)and then
PRec^[N - new record] := NewRecord; don't work!
 
 
 
   
 
0
Comment
Question by:Vadik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +1
15 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1389334
hehe, actually you don't have to do that :))

you simply use WriteBuffer()

procedure WriteBuffer(const Buffer; Count: Longint);

..-=ViKtOr=-..
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389335
hi vadik,

regarding your q
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10183184 
i have written a full featured app (insert, delete, modify, stepthrough)
but you've accepted the provided answer, therefore i've not posted it there.

if you can wait 24 Hours, then i can post it here, because i've the code not here.

meikl

0
 

Author Comment

by:Vadik
ID: 1389336
Hi! kretzschmar  Ok! I'll wait for you. I need very much this!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1389337
Hey, why did you reject my answer Vadik?
0
 

Expert Comment

by:Primeware
ID: 1389338
Type
  TMyRec = Record
    Name : String[255];
  End;

  TaRec = Array[1..1] of TMyRec;

  PaRec = ^TaRec;

Var
  PRec : PaRec;
  I: Integer;
begin
  Getmem(PRec,SizeOf(TMyRec));
  PRec^[1].Name:='1';
  Showmessage(PRec^[1].Name);
  ReallocMem(PRec,SizeOf(TMyRec)*2);
  I:=2;
  PRec^[I].Name:='2';
  Showmessage(PRec^[I].Name);
  Freemem(PRec);
end;
0
 

Author Comment

by:Vadik
ID: 1389339
Hi! Victornet your answer is very simple :-)
0
 

Author Comment

by:Vadik
ID: 1389340
Primeware i accept your answer
0
 

Accepted Solution

by:
Primeware earned 200 total points
ID: 1389341
Okay- thanx
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389342
what the hell?? what do you mean it's simple... simple answers are usually most accurate... you want me to write a whole page simply explaining the above statement?? I can do that... write all kinds of stuff..

anyway, Primeware, didn't give you what you waned at all.. that doesn't change the memory in the MemoryStrream at all.. that simply reallocates some crappy record... anyway, it's your question....
0
 

Expert Comment

by:Primeware
ID: 1389343
Viktornet. The question was as I recall a continue on another question posted by Vadik for a couple of days ago.

Therefore I made that answer... Sorry if you think that it is wrong.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389344
I apologize then.. sorry..
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389345
hi all,

as i said yesterday my version, a totally other way as provided by primeware.

No pointerhandling !


unit Mem_file_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    First: TButton;
    Previous: TButton;
    Next: TButton;
    Last: TButton;
    Add: TButton;
    Modify: TButton;
    Delete: TButton;
    Save: TButton;
    Cancel: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FirstClick(Sender: TObject);
    procedure PreviousClick(Sender: TObject);
    procedure NextClick(Sender: TObject);
    procedure LastClick(Sender: TObject);
    procedure AddClick(Sender: TObject);
    procedure ModifyClick(Sender: TObject);
    procedure SaveClick(Sender: TObject);
    procedure CancelClick(Sender: TObject);
    procedure DeleteClick(Sender: TObject);
  private
    procedure AdJustControlState;
    procedure ReadRecord(No : Integer);
    procedure DisplayFields;
    Procedure GetFields;
    procedure InitFields;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//The Record
Type TMyRec = Record
                Name     : String[30];
                BirthDay : TDateTime;
                eMail    : String[30];
              end;

//The File
Const FN = 'C:\MyFile.Dat';

Var
  TM : TMemoryStream;
  MyRec : TMyRec;
  InEdit : Boolean = False;
  RecNo : Integer = 0;

//Adjust Control State
procedure TForm1.AdJustControlState;
begin
  Cancel.Enabled := InEdit;
  Delete.Enabled := Not(InEdit) and (TM.Size > 0);
  Save.Enabled := InEdit;
  First.Enabled := (TM.Position > SizeOf(MyRec)) and not InEdit;
  Previous.Enabled := (TM.Position > SizeOf(MyRec)) and not InEdit;
  Next.Enabled := (TM.Position < TM.Size) and not InEdit;
  Last.Enabled := (TM.Position < TM.Size) and not InEdit;
  Modify.Enabled := Not(InEdit) and (TM.Size > 0);
  Add.Enabled := Not(InEdit);
  Edit1.Enabled := InEdit;
  Edit2.Enabled := InEdit;
  Edit3.Enabled := InEdit;
end;

//Get one Record
procedure TForm1.ReadRecord(No : Integer);
begin
  If No * SizeOf(MyRec) < TM.Size then
  begin
    TM.Position := No * SizeOf(MyRec);
    TM.ReadBuffer(MyRec,SizeOf(MyRec));
  end
  else
  begin
    InitFields;
    RecNo := 0;
    TM.Position := 0;
  end;
  DisplayFields;
end;

//Output
procedure TForm1.DisplayFields;
begin
  Edit1.Text := MyRec.Name;
  Edit2.Text := DateToStr(MyRec.BirthDay);
  Edit3.Text := MyRec.eMail;
  label1.Caption := IntToStr(Recno);
end;

//Init
procedure TForm1.InitFields;
begin
  MyRec.Name := '';
  MyRec.BirthDay := Now;
  MyRec.eMail := '';
end;

//Input
Procedure TForm1.GetFields;
begin
  MyRec.Name := Edit1.Text;
  MyRec.eMail := Edit3.Text;
  try
    MyRec.BirthDay := StrToDate(Edit2.Text);
  except
    Showmessage('Invalid Date! Reset to Today');
    MyRec.BirthDay := Now;
  end;
end;

//ReadFile
procedure TForm1.FormCreate(Sender: TObject);
var H : Integer;
begin
  TM := TMemoryStream.Create;
  if not FileExists(FN) then
  begin
    H := FileCreate(FN);
    if H > 0 then
      FileClose(H)
    else
    begin
      ShowMessage('Error during Opening the WorkFile');
      Halt;
    end;
  end;
  TM.LoadFromFile(FN);
  TM.Position := 0;
  ReadRecord(RecNo);
  AdJustControlState;
end;

//WriteFile
procedure TForm1.FormDestroy(Sender: TObject);
begin
  TM.SaveToFile(FN);
  TM.Free;
end;

//Get First Record
procedure TForm1.FirstClick(Sender: TObject);
begin
  Recno := 0;
  ReadRecord(Recno);
  AdJustControlState;
end;

//Get Previous Record
procedure TForm1.PreviousClick(Sender: TObject);
begin
  Dec(RecNo);
  ReadRecord(Recno);
  AdJustControlState;
end;

//Get Next Record
procedure TForm1.NextClick(Sender: TObject);
begin
  inc(RecNo);
  ReadRecord(Recno);
  AdJustControlState;
end;

//Get Last Record
procedure TForm1.LastClick(Sender: TObject);
begin
  RecNo := TM.Size div SizeOf(MyRec) - 1;
  ReadRecord(Recno);
  AdJustControlState;
end;

//Append one Record
procedure TForm1.AddClick(Sender: TObject);
begin
  TM.Position := TM.Size;
  InEdit := True;
  InitFields;
  DisplayFields;
  AdJustControlState;
end;

//Modify One Record
procedure TForm1.ModifyClick(Sender: TObject);
begin
  TM.Position := RecNo * SizeOf(MyRec);
  InEdit := True;
  AdJustControlState;
end;

//Save Record
procedure TForm1.SaveClick(Sender: TObject);
begin
  GetFields;
  TM.WriteBuffer(MyRec,SizeOf(MyRec));
  RecNo := TM.Position div SizeOf(MyRec) - 1;
  DisplayFields;
  InEdit := False;
  AdJustControlState;
end;

//Cancel Modify or Add
procedure TForm1.CancelClick(Sender: TObject);
begin
  ReadRecord(RecNo);
  InEdit := False;
  AdJustControlState;
end;

//Delete One Record
//copys the last to the record which should delete
//and truncates the stream
procedure TForm1.DeleteClick(Sender: TObject);
var OldPos : Integer;
begin
  if TM.Size = SizeOf(MyRec) then
  begin
    TM.Position := 0;
    RecNo := 0;
    ReadRecord(RecNo);
  end
  else
    If TM.Position = TM.Size then
    begin
      RecNo := RecNo - 1;
      ReadRecord(RecNo);
    end
    else
    begin
      OldPos := TM.Position;
      ReadRecord(TM.Size div SizeOf(MyRec) - 1);
      TM.Position := OldPos;
      Save.Click;
    end;
  TM.Size := TM.Size - SizeOf(MyRec);
  AdJustControlState;
end;

end.

have fun

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389346
oops,

there is a little bug in the delete option
the correction:

procedure TForm1.DeleteClick(Sender: TObject);
var OldPos : Integer;
begin
  if TM.Size = SizeOf(MyRec) then
  begin
    TM.Position := 0;
    RecNo := 0;
    ReadRecord(RecNo);
  end
  else
    If TM.Position = TM.Size then
    begin
      RecNo := RecNo - 1;
      ReadRecord(RecNo);
    end
    else
    begin
      OldPos := TM.Position - SizeOf(MyRec);            //in this line
      ReadRecord(TM.Size div SizeOf(MyRec) - 1);
      TM.Position := OldPos;
      Save.Click;
    end;
  TM.Size := TM.Size - SizeOf(MyRec);
  AdJustControlState;
end;

meikl
0
 

Author Comment

by:Vadik
ID: 1389347
Hi! kretzschmar Thank you very much for your full answer on my question , but I had a limited time and wrote my program using PrimeWare's timely help . I'm sorry , but I must give points to him. :-(
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389348
no prob vadik,

its just an alternate
and primewares solutions works also

good luck again

meikl ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question