Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6219

Re: Persisting a structure: Valgrind errors

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Persisting a structure: Valgrind errors
Date Sun, 05 Jun 2011 10:59:49 +1200
Lines 83
Message-ID <94vrn5FivcU11@mid.individual.net> (permalink)
References <ise7gc$6k2$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net dRaWv5VWCQ1ejI1jkK8LawqVSQprC222AfQdAQHJXrtT9g71UE
Cancel-Lock sha1:gaqNUy4czDdTDsV0SZ6ME7EnwPk=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <ise7gc$6k2$1@dont-email.me>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:6219

Show key headers only | View raw


On 06/ 5/11 09:19 AM, Ramon wrote:
> Hello,
>
> Though I have a good amount of experience in ANSI C, Java, C#...etc, I
> am fairly new with C++.
>
> What I am trying to do is to persist a struct to a binary file.  The
> structure is being saved correctly yet when I am trying to load it from
> disk to memory Valgrind is popping up some errors (Invalid write and
> Invalid free) from this line of code:
>
> file.read((char*) i, sizeof(struct record));
>
> Attached with this message is the code of my program.  Can anyone point
> out what am I doing wrong?  Mind you, the program runs great, just
> Valgrind is showing some warnings.

I'm not surprised!

> #include<iostream>
> #include<fstream>
> #include<string>
>
> using namespace std;
>
> #define NAME "test_file.bin"

Yuck, use a constant!

> struct record
> {
> 	string	name;
> 	string	surname;
> };
>
>
> void save(struct record *i)

This and all subsequent uses of "struct" are superfluous.

> {
> 	fstream file;
> 	
> 	file.open(NAME, fstream::trunc | fstream::binary | fstream::out);
> 	
> 	
> 	if (!file.is_open())
> 		throw string("Unable to open file");
> 	
> 	file.write((char*)i, sizeof(struct record));

This will write what ever the static part of the strings are, but 
probably not the actual data (the two may be the same for small strings 
if the small value optimisations are used, but who knows?).

> 	file.close();
> }
>
>
>
>
> void load(struct record *i)
> {
> 	fstream 		file;
> 	
> 	file.open(NAME, fstream::binary | fstream::in);
> 	
> 	
> 	if (!file.is_open())
> 		throw string("Unable to open file");
> 	
> 	
>    	file.read((char*) i, sizeof(struct record));	// error

This will write some random garbage over the strings, causing all sorts 
of errors when they are deleted.

If you are going to serialise a struct, you have to serialise each 
member.  What you are doing could work for a POD struct, but it would be 
unsafe even for those.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Persisting a structure:  Valgrind errors Ramon <ramif_47.invalid@yahoo.co.uk> - 2011-06-04 23:19 +0200
  Re: Persisting a structure:  Valgrind errors Ian Collins <ian-news@hotmail.com> - 2011-06-05 10:59 +1200
    Re: Persisting a structure: Valgrind errors Ebenezer <woodbrian77@gmail.com> - 2011-06-04 18:11 -0700
      Re: Persisting a structure: Valgrind errors Öö Tiib <ootiib@hot.ee> - 2011-06-04 18:56 -0700
  Re: Persisting a structure:  Valgrind errors Juha Nieminen <nospam@thanks.invalid> - 2011-06-05 05:57 +0000
    Re: Persisting a structure:  Valgrind errors "io_x" <a@b.c.invalid> - 2011-06-05 08:14 +0200
      Re: Persisting a structure:  Valgrind errors Ramon <ramif_47.invalid@yahoo.co.uk> - 2011-06-05 11:40 +0200
        Re: Persisting a structure:  Valgrind errors "io_x" <a@b.c.invalid> - 2011-06-05 12:17 +0200
          Re: Persisting a structure:  Valgrind errors Ramon <ramif_47.invalid@yahoo.co.uk> - 2011-06-05 18:15 +0200
            Re: Persisting a structure:  Valgrind errors Asger-P <junk@asger-p.dk> - 2011-06-05 18:44 +0200
            Re: Persisting a structure:  Valgrind errors Juha Nieminen <nospam@thanks.invalid> - 2011-06-07 06:20 +0000
              Re: Persisting a structure: Valgrind errors Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-07 00:24 -0700
                Re: Persisting a structure: Valgrind errors Juha Nieminen <nospam@thanks.invalid> - 2011-06-07 08:40 +0000
                Re: Persisting a structure: Valgrind errors Miles Bader <miles@gnu.org> - 2011-06-08 14:43 +0900
                Re: Persisting a structure: Valgrind errors Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-07 23:37 -0700
                Re: Persisting a structure: Valgrind errors Miles Bader <miles@gnu.org> - 2011-06-08 17:18 +0900
        Re: Persisting a structure:  Valgrind errors Nobody <nobody@nowhere.com> - 2011-06-05 18:42 +0100
    Re: Persisting a structure: Valgrind errors Ebenezer <woodbrian77@gmail.com> - 2011-06-05 13:22 -0700
  Re: Persisting a structure:  Valgrind errors Asger-P <junk@asger-p.dk> - 2011-06-05 12:04 +0200

csiph-web