Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6219
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Persisting a structure: Valgrind errors |
| Date | 2011-06-05 10:59 +1200 |
| Message-ID | <94vrn5FivcU11@mid.individual.net> (permalink) |
| References | <ise7gc$6k2$1@dont-email.me> |
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
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