1 / 14

Memoria estática versus dinámica con Estructuras

Memoria estática versus dinámica con Estructuras. Memoria dinámica. ¿Qué hacer cuando las variables declaradas son insuficientes o cuando no se conoce anticipadamente cuanto almacenamiento será requerido por el programa?. 1. Se crea una variable de tipo puntero.

rafael-may
Télécharger la présentation

Memoria estática versus dinámica con Estructuras

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memoria estática versus dinámicaconEstructuras

  2. Memoria dinámica ¿Qué hacer cuando las variables declaradas son insuficientes o cuando no se conoce anticipadamente cuanto almacenamiento será requerido por el programa? 1. Se crea una variable de tipo puntero. 2. Se le pide al sistema operativo que asigne más memoria. 3. El sistema operativo retorna la dirección a la memoria asignada. 4. Si la memoria asignada, no se usará más, se puede devolver al sistema operativo.

  3. a x p Memoria estática/dinámica 1 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; } Número de bytes que usa una variable de este tipo b NULL

  4. a x p Memoria estática/dinámica 2 struct str { int x; struct str *p; }; main(){ // se crea la variable estructurada estatica, "a" que tiene memoria asignada, // la relacion entre el nombre "a" y la memoria asignada es permanente: struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; } b NULL

  5. b NULL Memoria estática/dinámica 3 struct str { int x; struct str *p; }; main(){ struct str a; // se crea la variable de tipo puntero "b" para apuntar a variables estructuradas, // no hay memoria asignada de tipo estructurada, "b" apunta a nada: struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; } a x p

  6. Memoria estática/dinámica 4 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; // se puede modificar u obtener el contenido de los campos de la variable "a", // para ello se debe usar "." seguida del nombre del campo: a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; } b NULL a 55 x p

  7. x p Memoria estática/dinámica 5 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; // se pide, al sistema operativo, memoria para una variable estructurada, // la variable puntero "b" es usada para almacenar la direccion de la nueva // memoria devuelta, esto significa que “b” queda apuntando a esta nueva // variable estructurada: b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; } b NULL a 55 x p

  8. x p Memoria estática/dinámica 6 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; // se pide, al sistema operativo, memoria para una variable estructurada, // la variable puntero "b" es usada para almacenar la direccion de la nueva // memoria devuelta, esto significa que “b” queda apuntando a esta nueva // variable estructurada: b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; } b NULL a 55 x p

  9. Memoria estática/dinámica 7 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); // para acceder a los campos de la variable estructurada se debe usar "->" // en vez de “.”: b->x=66; a.x=a.x+b->x; } b NULL a 55 66 x p x p

  10. Memoria estática/dinámica 8 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); // para acceder a los campos de la variable estructurada se debe usar "->" // en vez de “.”: b->x=66; a.x=a.x+b->x; } b NULL a 121 66 x p x p

  11. Memoria estática/dinámica 9 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; // si la variable de tipo puntero se hace apuntar a otra variable estructurada // por ejemplo a "a", la variable estructurada, pedida en forma dinamica, // queda sin la posibilidad de ser usada por el programa: b=&a; } b NULL a 121 66 x p x p

  12. NULL 66 x p Memoria estática/dinámica 10 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; // la forma correcta es devolver dinamicamente la memoria: free(b); b=&a; } b a 55 x p

  13. Memoria estática/dinámica 11 struct str { int x; struct str *p; }; main(){ struct str a; struct str *b=NULL; a.x=55; b=(struct str *)malloc(sizeof(struct str)); b->x=66; a.x=a.x+b->x; free(b); b=&a; // NO es necesario declarar una variable estructurada como “a” para formar una lista } b a 55 x p

  14. Memoria dinámica 1 struct str { int x; struct str *p; }; main(){ struct str *b=NULL; ………… // BASTA con declarar una variable tipo puntero a estructur como “b” y usar malloc y free // si el puntero tiene asignado el valor NULL, se dice que apunta a NULL (“nada”) // si el valor del puntero es NULL, se dice que la lista está vacía } b NULL

More Related