Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6215
| From | Ramon <ramif_47.invalid@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Persisting a structure: Valgrind errors |
| Date | 2011-06-04 23:19 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <ise7gc$6k2$1@dont-email.me> (permalink) |
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.
Thanks.
//
// code
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define NAME "test_file.bin"
struct record
{
string name;
string surname;
};
void save(struct record *i)
{
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));
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
file.close();
}
int main()
{
struct record *r = new struct record;
struct record *test = new struct record;
r->name = "name";
r->surname = "surname";
save(r);
load(test);
cout << test->name << endl;
cout << test->surname << endl;
delete r;
delete test;
return 0;
}
Back to comp.lang.c++ | Previous | Next — 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