Maybe I should read the members var from the file to a tmp variable and use this to iterate. But the problem must be somewhere else because that was my previous implementation which didn't work...
43 Replies - 1902 Views - Last Post: 05 August 2012 - 03:10 AM
#32
Re: Reading and writing a linked list from a file
Posted 31 July 2012 - 08:51 AM
Yes, using the temp variable will work.
Edit after: I tried grabbing the load() code from post #11 with the temp variable, and I'm seeing two nodes loaded and printed out. What kind of failure were you seeing?
Edit after: I tried grabbing the load() code from post #11 with the temp variable, and I'm seeing two nodes loaded and printed out. What kind of failure were you seeing?
This post has been edited by Skydiver: 31 July 2012 - 08:55 AM
#33
Re: Reading and writing a linked list from a file
Posted 31 July 2012 - 10:25 AM
#34
Re: Reading and writing a linked list from a file
Posted 31 July 2012 - 10:33 AM
You can't even go back in this thread to look up your own post #11 ?
As I said, I just grabbed the code you had there for load() that used the temporary variable, and used it to replace load() that you had in post #25.
As I said, I just grabbed the code you had there for load() that used the temporary variable, and used it to replace load() that you had in post #25.
#35
Re: Reading and writing a linked list from a file
Posted 31 July 2012 - 11:13 AM
Skydiver, on 31 July 2012 - 10:33 AM, said:
You can't even go back in this thread to look up your own post #11 ?
As I said, I just grabbed the code you had there for load() that used the temporary variable, and used it to replace load() that you had in post #25.
As I said, I just grabbed the code you had there for load() that used the temporary variable, and used it to replace load() that you had in post #25.
It works for me too... But why didn't my other implementation work?
#36
Re: Reading and writing a linked list from a file
Posted 31 July 2012 - 11:32 AM
I don't know. All you said was that it didn't work. You didn't describe the behavior that you were seeing.
Anyway, you still have huge memory leak to fix.
Anyway, you still have huge memory leak to fix.
#37
Re: Reading and writing a linked list from a file
Posted 02 August 2012 - 04:57 AM
I made a destructor for List, but according to valgrind there is e memory leak in main:26, where the call of List::load() is.
#include "list.h"
#define FILE_NAME "file.dat"
List::List()
{
list = NULL;
members = 0;
}
List::~List() {
int i;
Node *current, *next;
for(i = 0, current = list; i < members; i++, current = next) {
next = current->next;
delete current;
}
}
int List::printcon() {
int i;
Node *dummy;
if(list == NULL)
return -1;
for(i = 0, dummy = list; i < members; i++, dummy = dummy->next)
printf("%d %lf %s\n", dummy->i, dummy->d, dummy->s);
return (i + 1);
}
int List::add(int i, double d, char *s) {
if(list == NULL) {
list = new Node;
list->i = i;
list->d = d;
strcpy(list->s, s);
members++;
return 0;
}
Node *dummy;
int counter;
for(dummy = list, counter = 0; counter < members - 1; dummy = dummy->next, counter++)
;
dummy->next = new Node;
dummy->next->i = i;
dummy->next->d = d;
strcpy(dummy->next->s, s);
members++;
return 0;
}
int List::backup() {
FILE *fp;
if( (fp = fopen(FILE_NAME, "wb") ) == NULL )
return 1;
int i;
Node *dummy;
fwrite(&members, sizeof(members), 1, fp);
for(i = 0, dummy = list; i < members; i++, dummy = dummy->next)
fwrite(dummy, sizeof(Node), 1, fp);
fclose(fp);
return 0;
}
int List::load() {
FILE *fp;
if( (fp = fopen(FILE_NAME, "rb")) == NULL )
return 1;
Node new_node;
int tmp;
fread(&tmp, sizeof(tmp), 1, fp);
for(int i = 0; i < tmp; i++) {
fread(&new_node, sizeof(Node), 1, fp);
this->add(new_node.i, new_node.d, new_node.s);
}
/*
int i;
Node *dummy;
fread(&members, sizeof(members), 1, fp);
dummy = new Node;
fread(dummy, sizeof(Node), 1, fp);
list = dummy;
for(i = 1, dummy = dummy->next; i < members; i++, dummy = dummy->next) {
dummy = new Node;
fread(dummy, sizeof(Node), 1, fp);
}
*/
return 0;
}
#38
Re: Reading and writing a linked list from a file
Posted 02 August 2012 - 12:43 PM
You'll have to show your new code for main() because the last main() that you posted back in post #3 has list.add() on line 26.
#39
Re: Reading and writing a linked list from a file
Posted 04 August 2012 - 03:09 AM
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include "node.h"
#include "list.h"
using namespace std;
#define LOAD
int main()
{
List list;
#ifdef BACKUP
list.add(12, 8.234, "Bla Bla");
list.add(6, 3.33333, "Bourou Bourou");
list.printcon();
list.backup();
printf("Backup was succesfull!\n");
#endif
#ifdef LOAD
list.load();
list.printcon();
#endif
return 0;
}
#40
Re: Reading and writing a linked list from a file
Posted 04 August 2012 - 10:31 AM
I tried a quick test just to do cout's in the Node class to show when Node's are constructed and destroyed and I'm not seeing a leak. Since I don't have valgrind, I can't actually run valgrind on my build of your code.
You'll have to post the detailed error list that valgrind prints out because that will have the details of where the memory is being allocated and where it thinks it is being lost. You'll want to use the --leak-check=yes and --track-origins=yes flags.
You'll have to post the detailed error list that valgrind prints out because that will have the details of where the memory is being allocated and where it thinks it is being lost. You'll want to use the --leak-check=yes and --track-origins=yes flags.
#41
Re: Reading and writing a linked list from a file
Posted 04 August 2012 - 10:47 AM
==4033== Memcheck, a memory error detector ==4033== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==4033== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==4033== Command: ./a.out ==4033== 12 8.234000 Bla Bla 6 3.333330 Bourou Bourou ==4033== ==4033== HEAP SUMMARY: ==4033== in use at exit: 352 bytes in 1 blocks ==4033== total heap usage: 3 allocs, 2 frees, 624 bytes allocated ==4033== ==4033== LEAK SUMMARY: ==4033== definitely lost: 0 bytes in 0 blocks ==4033== indirectly lost: 0 bytes in 0 blocks ==4033== possibly lost: 0 bytes in 0 blocks ==4033== still reachable: 352 bytes in 1 blocks ==4033== suppressed: 0 bytes in 0 blocks ==4033== Reachable blocks (those to which a pointer was found) are not shown. ==4033== To see them, rerun with: --leak-check=full --show-reachable=yes ==4033== ==4033== For counts of detected and suppressed errors, rerun with: -v ==4033== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
#42
Re: Reading and writing a linked list from a file
Posted 04 August 2012 - 11:30 AM
I'm not seeing any leaks.
With regards to the still reachable, read here: http://valgrind.org/...tml#faq.reports
With regards to the still reachable, read here: http://valgrind.org/...tml#faq.reports
#43
Re: Reading and writing a linked list from a file
Posted 05 August 2012 - 03:01 AM
Skydiver, on 04 August 2012 - 11:30 AM, said:
I'm not seeing any leaks.
With regards to the still reachable, read here: http://valgrind.org/...tml#faq.reports
With regards to the still reachable, read here: http://valgrind.org/...tml#faq.reports
When there are more allocs than frees, ins't there a leak?
#44
Re: Reading and writing a linked list from a file
Posted 05 August 2012 - 03:10 AM
I think those belong to the C++ library. You only had two calls to new: the two times you called add. Somebody else called new. Additionally, if you look at your Node class/struct, it is nowhere close to the 352 bytes that were allocated. Since the Node class is the only thing you ever allocate directly, the conclusion is that the leak is not yours.
The sure way to find out is follow the instructions on the output there with those sets of flags.
The sure way to find out is follow the instructions on the output there with those sets of flags.
|
|

New Topic/Question
Reply




MultiQuote


|