Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 26-08-2004
jplj jplj is offline
Miembro
 
Registrado: oct 2003
Posts: 189
Poder: 21
jplj Va por buen camino
Matrices dinámicas

La Guía del Lenguaje Object Pascal de Borland para Delphi 5 dice acerca de las matrices dinámicas (no obstante estoy trabajando con Delphi 7):
"Para crear una matriz en memoria, es necesario llamar a SetLength. ...

Para liberar la memoria asignada una matriz dinámica, basta tomar una variable que haga referencia a la matriz y asignarle el valor nil o pasarla a Finalize; ... "
Mi pregunta es si en el caso de llamar a SetLength para una matriz que ya tuviera asigana memoria, no libera primero la memoria asignada.

Es decir, el siguiente código -que funciona- ¿es correcto?:

Código Delphi [-]
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Matriz: array of Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   SetLength(Matriz, 5);
   (Sender as TButton).Caption:= IntToStr(High(Matriz));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   SetLength(Matriz, 10);
   (Sender as TButton).Caption:= IntToStr(High(Matriz));
end;

o bien debería ser así:

Código Delphi [-]
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Matriz: array of Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin

   Matriz:= nil;

   SetLength(Matriz, 5);
   (Sender as TButton).Caption:= IntToStr(High(Matriz));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

   Matriz:= nil;

   SetLength(Matriz, 10);
   (Sender as TButton).Caption:= IntToStr(High(Matriz));
end;
__________________
Sonríe. Mañana puede ser peor.
Responder Con Cita
  #2  
Antiguo 26-08-2004
Sirkius Sirkius is offline
Miembro
 
Registrado: jun 2004
Posts: 83
Poder: 20
Sirkius Va por buen camino
De la ayuda de Delphi, buscando SetLength:

Cita:
SetLength reallocates the string or array referenced by S to the given length
Y a mi entender, como dice que recoloca: pilla un hueco, copia lo existente y borra lo anterior.

Bye!
Responder Con Cita
  #3  
Antiguo 30-08-2004
jplj jplj is offline
Miembro
 
Registrado: oct 2003
Posts: 189
Poder: 21
jplj Va por buen camino
Aunque tarde, gracias por la respuesta.
__________________
Sonríe. Mañana puede ser peor.
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 15:16:29.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi