Article View: es.comp.os.linux.programacion
Article #6192(Grandes) dudas sobre el funcionamiento de malloc() y realloc()
From: Han Solo
Date: Thu, 17 Jul 2003 09:22
Date: Thu, 17 Jul 2003 09:22
90 lines
2964 bytes
2964 bytes
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hola a todos.
Pues lo dicho, no acabo de entender c�mo demonios funcionan esas
funciones, sobre todo realloc. Os pongo un programita:
- ----------------------------
#include <stdio.h>
#include <stdlib.h>
#define ELEM 3
float *p,*q;
int i;
main(){
p=(float *)malloc(ELEM*sizeof(float));
p[0]=5.6;
p[1]=66.6;
p[2]=166.6;
p[3]=266.6; /* �no deber�a dar un segfault? */
while (*p!='\0'){
printf("VValor %f\n",*p);
*p++;
}
*p=0; /* �es realmente necesario poner a 0 este puntero? */
p=(float *)realloc(p, ELEM*4*sizeof(float));
p[4]=15.6;
p[5]=166.6;
p[6]=166.6;
*p=0;
printf ("Aqu� se queda\n");
while (*p!='\0'){
printf("Valor de p (segundo) %f\n",*p);
*p++;
}
}
- ----------------------------
Y el resultado es:
VValor 5.600000
VValor 66.599998
VValor 166.600006
VValor 266.600006
Violaci�n de segmento
- ----------------------------
En principio, malloc() reserva memoria para 3 float, por eso no
entiendo por qu� demonios al asignar un valor a p[3], no se sale fuera
de su memoria.
El segfault que aparece viene del realloc(), que tampoco entiendo,
porque en varios manuales que he visto aparece as�.
Por otra parte, si comento la l�nea del realloc(), luego no entra en
el segundo while(), que es exactamente igual que el primero.
�alguna sugerencia?
Gracias a todos por adelantado
Un Saludo
- --
Han Solo
The Rebel Alliance
Emacs is not on every system
So what? [...] Do you tell your administrative people to stick with
notepad.exe? Do you tell your fat kids they can only have the crummy
games that come with their video games or plain dress that comes with
Barbie?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>
iD8DBQE/Fmrh4FjpJaPEp20RApDVAKDKtLfrZatt59E+QYKsI00VxGaW+wCg34q/
0/mo4ZZNFSaIUtxlVARwgww=
=pPeo
-----END PGP SIGNATURE-----
Technical Details
Message-ID:
<86r84po6c8.fsf@pc147.ietcc.csic.es>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!pc147.ietcc.csic.ES!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,87b737dc79fbe16,start X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2003-07-17 02:30:41 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!pc147.ietcc.csic.ES!not-for-mail From: Han Solo <hsolo@escomposlinux.org> Newsgroups: es.comp.os.linux.programacion Subject: (Grandes) dudas sobre el funcionamiento de malloc() y realloc() Date: 17 Jul 2003 11:22:47 +0200 Lines: 90 Message-ID: <86r84po6c8.fsf@pc147.ietcc.csic.es> NNTP-Posting-Host: pc147.ietcc.csic.es (161.111.230.147) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 1058434240 11823206 161.111.230.147 (16 [63837]) X-Orig-Path: ID-63837.user.dfncis.de!news User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7