🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #2309

Re: [C++] fwrite + segmentation fault

#2309
From: MaG
Date: Fri, 16 Jul 2004 07:53
106 lines
4586 bytes
"Lorenzo Dieryckx" <dit.is.geen.email@adres.be> schreef in bericht
news:n3tdf018psossgbcc1ohrcgo8kbk3kastb@4ax.com...
> On Thu, 15 Jul 2004 17:35:24 GMT, Kristoffel <kdg1955@yahoo.com> wrote:
>
> >Lorenzo Dieryckx wrote:
> >> Hello,
> >>
> >> Ik zit met een raar probleempje, en ik geraak er echt niet aan uit
> >>
> >> ��n van mijn klassen heeft volgende memberfunctie:
> >>
> >> int FileByteDestination::WriteBytes(const void * buffer, int count)
> >> {
> >>   if (_file == 0)
> >>     return 0;
> >>
> >>   std::cout << "inside FileByteDestination::WriteBytes: _file = " <<
_file << std::endl;
> >>   std::cout << "number of chars to write = " << count << std::endl;
> >>   std::cout << "real number of chars in string: " << strlen((char
*)buffer) << std::endl;
> >>   std::cout << "string to write :" << std::endl << (char *)buffer <<
std::endl;
> >>   int retval = fwrite(buffer, sizeof(char), count, _file);
> >> //sizeof(char) = 1 byte
> >>   std::cout << "wrote " << retval << " bytes" << std::endl <<
std::endl;
> >>   return retval;
> >> }
> >>
> >> _file is een FILE* die ingevuld wordt in de constructor van
FileByteDestination (mbv fopen())
> >>
> >> als ze opgeroepen wordt met als parameters "ik ben lorenzo", 14
> >> produceert ze volgende output:
> >>
> >> inside FileByteDestination::WriteBytes: _file = 0x1e
> >> number of chars to write = 14
> >> real number of chars in string: 14
> >> string to write:
> >> ik ben lorenzo
> >> Segmentation fault
> >>
> >> als ik met gdb door mijn programma step,
> >> krijg ik de segv op de lijn "int retval = fwrite(buffer, sizeof(char),
count, _file)"
> >>
> >> Heeft iemand enig idee waardoor dit zou kunnen veroorzaakt zijn?
> >>
> >> --
> >> Lorenzo Dieryckx
> >> mail: lorenzopuntdieryckxatpandorapuntbe
> >
> >
> >1) is _file wel goed geopend, heeft er per ongeluk niemand een close
> >gedaan? Ben je zeker dat fopen geen errors gaf?
> >Als test plaats de open en de close eens in WriteBytes.
> >
> >2) Wees er zeker van dat buffer goed is gedeclareerd, daarom:
> >    std::cout << "sizeof (buffer): " << sizeof (buffer) << std::endl;
>
> Eerst en vooral hartelijk bedankt voor je hulp!
> Het probleem is nu opgelost, maar toch is er nog
> iets dat me dwars zit.
>
> Zo zag mijn constructor er vroeger uit:
> FileByteDestination::FileByteDestination(const String & uri, bool
binary_mode) :
>   IByteDestination(uri)
> {
>   if (binary_mode)
>     fopen(uri.c_str(), "wb");
>   else
>     fopen(uri.c_str(), "w");
>
>   if (_file == 0)
>     throw Exception("could not open file");
> }
>
> String is een wrapper rond de standaard char * strings,
> een beetje achtergemaakt van de STLstring, maar dan met
> een look en feel die ik liever heb.
>
> anyway, _file was dus toch niet goed geopend.
> Ik heb nu bij de initialisatie nog eens   _file(0)
> toegevoegd, en nu zie ik dus idd dat de file
> niet goed geopend werd.
> Ik dacht echter dat dit niet nodig was,
> gezien ik in de manpages van fopen() gelezen
> heb dat fopen() NULL returned als het openen
> van de file mislukt. Gezien NULL volgens mij
> nog altijd 0 is (en niet 0x1e), dacht ik
> dat _file sowieso op 0 zou staan als het openen
> van de file niet lukte.
>
> Blijkbaar niet dus :-S


Null is niet altijd gelijk aan 0, hangt van de implementatie af. Moet
eigenlijk (_file == (FILE *)0) zijn.

Of (_file == NULL), da's gemakkelijker.  :-)) t Is niet voor niets dat NULL
gedefinieerd wordt.

MaG
Technical Details
Message-ID: <JPLJc.68$oL.62133269@hestia.telenet-ops.be>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!aotearoa.belnet.be!news.belnet.be!ossa.telenet-ops.be!hestia.telenet-ops.be.POSTED!not-for-mail
References: <6euaf0th4h3ctr13l2lk2se4lmbu39ueu6@4ax.com> <wfzJc.186138$XZ1.8812637@phobos.telenet-ops.be> <n3tdf018psossgbcc1ohrcgo8kbk3kastb@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: fcea4,33ec3f0617e4db5
X-Google-Attributes: gidfcea4,public
X-Google-Language: DUTCH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!aotearoa.belnet.be!news.belnet.be!ossa.telenet-ops.be!hestia.telenet-ops.be.POSTED!not-for-mail
From: "MaG" <nomail@nowhere.com>
Newsgroups: be.comp.programming
References: <6euaf0th4h3ctr13l2lk2se4lmbu39ueu6@4ax.com> <wfzJc.186138$XZ1.8812637@phobos.telenet-ops.be> <n3tdf018psossgbcc1ohrcgo8kbk3kastb@4ax.com>
Subject: Re: [C++] fwrite + segmentation fault
Lines: 108
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <JPLJc.68$oL.62133269@hestia.telenet-ops.be>
Date: Fri, 16 Jul 2004 07:53:13 GMT
NNTP-Posting-Host: 81.164.156.83
X-Complaints-To: abuse@telenet.be
X-Trace: hestia.telenet-ops.be 1089964393 81.164.156.83 (Fri, 16 Jul 2004 09:53:13 CEST)
NNTP-Posting-Date: Fri, 16 Jul 2004 09:53:13 CEST
Organization: Telenet Internet