2.1 Manejo de Memoria Estitica 9HFWRUHV $UUHJORV

Anuncio
$SXQWHVGH(VWUXFWXUDGH'DWRV
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
¢&yPRVHGLVWULEX\HHOHVSDFLRHQODPHPRULDFRQXQDGHFODUDFLyQFRPROD
0DQHMRGH0HPRULD(VWiWLFD
VLJXLHQWH"
9HFWRUHV
var x,y:integer:
a:array[1..6] of char;
j:longint;
¢&yPRHVODPHPRULD"
YDULDEOH[
ƒ
/XJDUHVFRQVHFXWLYRV
JXDUGDUVRORXQE\WH
ƒ
ƒ
FDGDXQRGHHOORV
&DGDE\WHWLHQHXQD
DUUHJORD
GLUHFFLyQ
6HDFRVWXPEUD
HVFULELUODGLUHFFLyQ
YDULDEOH\
FRQFDSDFLGDGSDUD
/* Si lo desea puede probar con
este programa C++ */
#include <iostream>
using namespace std;
int main()
{
short int x,y;
char a[6];
// 0..5
int j;
cout << "Direcciones\n";
cout << "de Memoria\n";
cout << "x " << &x << endl;
cout << "y " << &y << endl;
cout << "a " << &a << endl;
cout << "j " << &j << endl;
system("pause");
return 0;
}
$
HQKH[DGHFLPDO
%
$
YDULDEOHM
&
'
ƒ
QHFHVDULRSDUDFDGD
)
YDULDEOHGHDFXHUGRD
)))
ƒ
)))
)))$
VXWLSR
6HDVLJQDHOHVSDFLRGH
DFXHUGRDORUGHQGHODV
)))%
'XUDQWHODFRPSLODFLyQ
VHUHVHUYDHOHVSDFLR
(
YDULDEOHVGHQWURGHOD
GHFODUDFLyQYDU
)))&
)))'
'XUDQWH HO SURFHVR GH SURGXFFLyQ GHO FyGLJR PiTXLQD HO FRPSLODGRU FUHD XQD
)))(
WDEODGHVtPERORV
))))
1RPEUH'LUHFFLyQ
GHODYDULDEOH7LSRLQLFLDO
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TXLHUHSDUDHOHMHUFLFLRHVXQD07,FRPRODVLJXLHQWH
GDGRV VHUi GHVFHQGHQWH HV GHFLU HPSLH]D HO MXJDGRU Q~PHUR \ WHUPLQD HO
Q~PHUR &DGD MXJDGRUODQ]DUi XQ SDU GH GDGRV WDQWDV YHFHV FRPR LQGLTXH HO
Q~PHUR TXH OH KD\D WRFDGR HO MXJDGRU ODQ]DUi GLH] YHFHV \ HO VROR XQD
YH] /RV UHVXOWDGRV GH FDGD ODQ]DPLHQWR GHEHUiQ FRQVHUYDUVH SDUD ILQHV
HVWDGtVWLFRVHQXQDPDWUL] HQ ODFXDO ORV UHQJORQHV UHSUHVHQWDQ D ORV MXJDGRUHV \
ODVFROXPQDVDFDGDODQ]DPLHQWRSDUWLFXODU
(OMXJDGRUTXHREWHQJDHOSURPHGLRPiVDOWRHVHOTXHJDQDUi
(VWH MXHJR QR VH DQDOL]DUi GHVGH HO SXQWR GH YLVWD GH SUREDELOLGDGHV GH JDQDU
VRORVHKDUiXQDVLPXODFLyQXVDQGRXQSURJUDPDGHFRPSXWDGRUD
(MHUFLFLR
(VFULED XQ SURJUDPD TXH KDJD XQD VLPXODFLyQ GH pVWH MXHJR LPSULPLHQGR DO
PHQRV ORV UHVXOWDGRV SURPHGLR GH WRGRV ORV MXJDGRUHV 8WLOLFH XQD PDWUL]
FRPSOHWDHQODTXHORVUHQJORQHVUHSUHVHQWHQDORVMXJDGRUHV\ODVFROXPQDVDORV
ODQ]DPLHQWRVGHORVGDGRV
•
8QD07,HVPDWUL]SRFRGHQVDSRUTXHHOQ~PHURGHHOHPHQWRVTXHFRQWLHQH
HVPX\SHTXHxRFRQUHVSHFWRDOWRWDOVLIXHUDFRPSOHWD
(MHUFLFLR
•
8QD 07, HV XQD PDWUL] UHJXODU SRUTXH VH SXHGH SUHGHFLU VX IRUPD VLQ
LPSRUWDUHOWDPDxR
0RGLILTXH HO SURJUDPD SDUD TXH VH KDJD XQD VLPXODFLyQ GH FXDOTXLHU Q~PH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RGLILFDUHOWDPDxRGHOYHFWRU
0RGLI7DPQXHYRWDP
‰
ORQJ←QXHYRWDP
$SXQWHVGH(VWUXFWXUDGH'DWRV
‰
‰
$SXQWDGRUHV
,QVHUWDUµ¶
0DQHMRGH0HPRULD'LQiPLFD
24
[PU]#
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
#include <iostream>
using namespace std;
int main()
{
int Dias[]={31,28,31,30,31,30,31,31,30,31,30,31};
char NomMeses[][11]={"Enero","Febrero","Marzo","Abril","Mayo",
"Junio","Julio","Agosto","Septiembre","Octubre",
"Noviembre","Diciembre"};
int mes;
cout << "Escriba un número entre 1 y 12: ";cin >> mes;
int pos = mes-1;
cout << "Número de días del mes de " << NomMeses[pos] << ": "<< Dias[pos] << endl;
system("pause");
return 0;
}
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
27
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
25
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
0DQHMRGH0HPRULD'LQiPLFD
$SXQWDGRUHVDWLSRVVLPSOHV
$SXQWDGRUHVXQFRQFHSWR
/DVYDULDEOHVL\M
#include
<iostream>
/DYDULDEOHS1232'5È*8$5'$5XQHQWHUR6,12/$',5(&&,Ï1GH
32'5È1*8$5'$5
using
namespace std;
XQOXJDUGRQGHVHSRGUiJXDUGDUXQHQWHUR(ODVWHULVFRHVXQRSHUDGRUGH
HQWHURV
int main()
,1',5(&&,Ï1/RTXHVHLQGLFDDOFRPSLODGRUHVTXHSHVGHWLSRLQW
{
short int i,j;
short int *p;
cout << "Dirección correspondiente a la variable \"p\": " << &p << endl;
cout << "Dirección correspondiente a la variable \"j\": " << &j << endl;
cout << "Dirección correspondiente a la variable \"i\": " << &i << endl;
p=&i;
6HJXDUGDHQODYDULDEOH
cout << "Dirección
guardada en \"p\": " << p << endl;
³L´XQGDWRFXDOTXLHUD
i=123;
cout << "Dato guardado en i: " << i << endl;
([SUHVLyQ
cout << "Dato guardado en *p: " << *p << endl;
YiOLGDOD
system("pause");
YDULDEOH³S´
return 0;
FRQWHQGUiOD
&
}
SVHOHHODGLUHFFLyQS
GLUHFFLyQ
/RGHPXHVWUDHVWHHMHPSOR
8VDPRVXQSDUGHDUUHJORVSDUD
VDEHUHOQ~PHURGHGtDV\HOQRPEUH
&DGDHOHPHQWRGHORVDUUHJORV
S
FRUUHVSRQGLHQWH
D³L´
$
&
M
0DQHMRGH0HPRULD'LQiPLFD
$SXQWDGRUHV
HTXLYDOHQWHVDFDGDPHVGHODxR
L
#include <iostream>
WLHQHXQDGLUHFFLyQ~QLFD
using namespace std;
YDORUHVGHVGHD
int main()
{
int Dias[]={31,28,31,30,31,30,31,31,30,31,30,31};
char NomMeses[][11]={"Enero","Febrero","Marzo","Abril","Mayo",
"Junio","Julio","Agosto","Septiembre","Octubre",
"Noviembre","Diciembre"};
int mes;
cout << "Escriba un número entre 1 y 12: ";cin >> mes;
int pos = mes-1;
cout << "Número de días del mes de " << NomMeses[pos] << ": "<< Dias[pos] << endl;
system("pause");
return 0; /DYDULDEOH³SRV´HVXQDSXQWDGRUHQHODPSOLR
}
VHQWLGRGHOFRQFHSWR$TXtVHXVDSDUDVHxDODUD
XQDSRVLFLyQGHORVDUUHJORVFRQRFLHQGRHOYDORU
GH³SRV´REWHQHPRVHOQ~PHURGHGtDV\HO
(
QRPEUHGHFLHUWRPHVHOTXHHOXVXDULRLQGLFy
28
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
0DQHMRGH0HPRULD'LQiPLFD
0DQHMRGH0HPRULD'LQiPLFD
$SXQWDGRUHVDWLSRVVLPSOHV
5HVHUYDU\/LEHUDU(VSDFLR
#include <iostream>
using namespace std;
int main()
{
int *i;
i = new int;
printf("Escribe un entero (se guardará en la dirección %x): ",i);
cin >> *i;
cout << "Dato guardado: " << *i << endl;
delete i;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
short int i,j;
short int *p;
cout << "Dirección correspondiente a la variable \"p\": " << &p << endl;
cout << "Dirección correspondiente a la variable \"j\": " << &j << endl;
cout << "Dirección correspondiente a la variable \"i\": " << &i << endl;
p=&i;
cout << "Dirección guardada en \"p\": " << p << endl;
i=123;
cout << "Dato guardado en i: " << i << endl;
cout << "Dato guardado en *p: " << *p << endl;
system("pause");
return 0;
}
26
31
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
0DQHMRGH0HPRULD'LQiPLFD
0DQHMRGH0HPRULD'LQiPLFD
5HVHUYDU\/LEHUDU(VSDFLR
$SXQWDGRUHVDPLHPEURVGHXQDHVWUXFWXUD
Operador ->
#include <iostream>
using namespace std;
SDGUHPDGUHKLMRQRVRQORV
struct Persona
{ char nombre[41]; int edad; }; OXJDUHVGRQGHJXDUGDUODVHVWUXFWXUDV
int main()
6RQODVYDULDEOHVTXH
{
FRQWHQGUiQODVGLUHFFLRQHV
Persona *padre, *madre, *hijo;
GHODVHVWUXFWXUDV
padre = new Persona;
madre = new Persona;
hijo = new Persona;
cout << "Nombre del Padre : ";gets(padre->nombre);
cout << "Nombre de la Madre: ";gets(madre->nombre);
cout << "Nombre del Hijo
: ";gets(hijo->nombre);
printf("Padre: %s\n",padre->nombre);
printf("Madre: %s\n",madre->nombre);
printf("Hijo : %s\n",hijo->nombre);
(TXLYDOHDKLMRQRPEUH
delete padre;
6LQHPEDUJR−!HVPiVVLPSOH\
delete madre;
DGHPiVHVXQVtPERORIiFLOGH
delete hijo;
UHFRUGDUSRUVXVLJQLILFDGR
system("pause");
return 0;
}
29
$SXQWHVGH(VWUXFWXUDGH'DWRV
QHZLQWUHVHUYDGXUDQWHODHMHFXFLyQ
HVSDFLRSDUDXQHQWHUR
iHVXQDYDULDEOHTXH
/DGLUHFFLyQFRUUHVSRQGLHQWHHVDVLJQDGDDOD
DSXQWDUiDXQHQWHUR
#include <iostream>
YDULDEOHL
using namespace std;
(O6LVWHPD2SHUDWLYRVHDVHJXUDTXHHVD
GLUHFFLyQQRHVWpRFXSDGDSRURWURGDWR
int main()
{
6HOHHXQHQWHUR\VH
int *i;
(V
JXDUGDHQODGLUHFFLyQi
i = new int;
LQGLVSHQVDEOH
printf("Escribe un entero (se guardará en la dirección %x): ",i);
OLEHUDUHO
HVSDFLR
cin >> *i;
UHVHUYDGR
cout << "Dato guardado: " << *i << endl;
3RUFDGD
delete i;
QHZKD\TXH
system("pause");
HMHFXWDU
return 0;
GHOHWHDQWHV
}
GHOILQGHO
SURJUDPD
3DUDTXHVHIDPLOLDULFHFRQORV
3DUDTXHVHIDPLOLDULFH
PHQVDMHVGHHUURUGHOFRPSLODGRU
FRQORVPHQVDMHVGH
HMHFXWHVLQHORSHUDGRUGH
HUURUGHHMHFXFLyQUHWLUH
LQGLUHFFLyQDVWHULVFR
QHZLQW
30
$SXQWHVGH(VWUXFWXUDGH'DWRV
$XWRU,QJ)HOLSH$ODQtV*RQ]iOH],QVWLWXWR7HFQROyJLFRGH'XUDQJR
0DQHMRGH0HPRULD'LQiPLFD
$SXQWDGRUHVDPLHPEURVGHXQDHVWUXFWXUD
Operador ->
#include <iostream>
using namespace std;
struct Persona
{ char nombre[41]; int edad; };
int main()
{
Persona *padre, *madre, *hijo;
padre = new Persona;
madre = new Persona;
hijo = new Persona;
cout << "Nombre del Padre : ";gets(padre->nombre);
cout << "Nombre de la Madre: ";gets(madre->nombre);
cout << "Nombre del Hijo
: ";gets(hijo->nombre);
printf("Padre: %s\n",padre->nombre);
printf("Madre: %s\n",madre->nombre);
printf("Hijo : %s\n",hijo->nombre);
delete padre;
delete madre;
delete hijo;
system("pause");
return 0;
}
Descargar