El código parece que esta bien.

Quizás tenés el archivo abierto o tuvo algún error al crear el archivo.
Ponele un "if" para verificar si fopen. Tendrías que usar fclose si no
te devuelve NULL. 

Copy-pastié tu código y le corrí el valgrind.

Mira el detalle de errores corriendo valgrind con "-v".

Esta es la respuesta que valgrind me tiro a mi:

==13622== Memcheck, a memory error detector.
==13622== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et
al.
==13622== Using LibVEX rev 1732, a library for dynamic binary
translation.
==13622== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==13622== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation
framework.
==13622== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et
al.
==13622== For more details, rerun with: -v
==13622== 
==13622== 
==13622== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from
1)
==13622== malloc/free: in use at exit: 0 bytes in 0 blocks.
==13622== malloc/free: 1,387 allocs, 1,387 frees, 64,390 bytes
allocated.
==13622== For counts of detected errors, rerun with: -v
==13622== All heap blocks were freed -- no leaks are possible.




El mié, 19-03-2008 a las 10:33 -0300, Federico De Seta escribió:
> Hola!. Tengo el siguiente programa:
> 
> 
> #include <stdio.h>
> #include "stdlib.h"
> 
> typedef struct{
>     int nro;
>     char letra;
> }campo;
> 
> int main(int argc, char ** argv)
> {
>     campo *reg1 = (campo*)malloc (sizeof(campo));
>     reg1->nro = 5 ;
>     reg1->letra ='g';
>     FILE* p = fopen("lala.txt", "wb");
>     
>     fwrite((void*)reg1, sizeof(campo), 1,  p);
>     fclose(p);
>     free(reg1);
>     return (0);
> }
> 
> Cuando lo corro con el valgrind me tira lo siguiente:
> 
> 
> ==6248== Memcheck, a memory error detector.
> ==6248== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et
> al.
> ==6248== Using LibVEX rev 1804, a library for dynamic binary
> translation.
> ==6248== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
> ==6248== Using valgrind-3.3.0, a dynamic binary instrumentation
> framework.
> ==6248== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et
> al.
> ==6248== For more details, rerun with: -v
> ==6248==
> ==6248== Syscall param write(buf) points to uninitialised byte(s)
> ==6248==    at 0x40E95C3: __write_nocancel
> (in /lib/tls/i686/cmov/libc-2.3.6.so)==6248==    by 0x408CC8A:
> _IO_do_write@@GLIBC_2.1 (in /lib/tls/i686/cmov/libc-2.3.6.so)
> ==6248==    by 0x408C3D4: _IO_file_close_it@@GLIBC_2.1
> (in /lib/tls/i686/cmov/libc-2.3.6.so)
> ==6248==    by 0x408281A: fclose@@GLIBC_2.1
> (in /lib/tls/i686/cmov/libc-2.3.6.so)
> ==6248==    by 0x80484AB: main (main.c:21)
> ==6248==  Address 0x4021005 is not stack'd, malloc'd or (recently)
> free'd
> ==6248==
> ==6248== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from
> 1)
> ==6248== malloc/free: in use at exit: 0 bytes in 0 blocks.
> ==6248== malloc/free: 2 allocs, 2 frees, 360 bytes allocated.
> ==6248== For counts of detected errors, rerun with: -v
> ==6248== All heap blocks were freed -- no leaks are possible.
> 
> 
> La linea 21 del main es la del fclose. Alguien sabe que puede ser?
> 
> Saludos y gracias de antemano por responder.
> 
> Fd.
> 
> 
> 
> 
> ______________________________________________________________________
> 
> Tarjeta de crédito Yahoo! de Banco Supervielle. Solicitá tu nueva
> Tarjeta de crédito. De tu PC directo a tu casa. 
> Visitá www.tuprimeratarjeta.com.ar
> _______________________________________________
> Lista de correo Programacion.
> [email protected]
> http://listas.fi.uba.ar/mailman/listinfo/programacion

_______________________________________________
Lista de correo Programacion.
[email protected]
http://listas.fi.uba.ar/mailman/listinfo/programacion

Responder a