Reputation: 1927
I am trying to test c++ performance when object locality is low, thus I am trying to allocate a large amount of memory that has many "dead objects". I am going to benchmark the "live objects" when there are many "dead ones" between them.
For that purpose I defined A simple LinkedList:
#include "LinkedList.hpp"
#include <iostream>
#include <string>
LinkedList::LinkedList() {
this->first = NULL;
this->last = NULL;
this->size = 0;
}
void LinkedList::add(node_t *node) {
if (!last) {
first = node;
last = first;
size++;
return;
}
last->next = node;
last = last->next;
size++;
}
void LinkedList::deleteFirst() {
if (first == NULL || size <= 0) {
std::cout << "Cannot Delete from empty list" << std::endl;
return;
}
node_t* oldfirst = first;
first = first->next;
delete oldfirst;
size--;
}
Header File:
#ifndef LINKEDLIST_HPP
#define LINKEDLIST_HPP
class node_t {
public:
node_t *next;
};
class LinkedList {
public:
LinkedList();
void add(node_t*);
void deleteFirst();
node_t *first;
int size;
private:
node_t *last;
};
#endif
While trying to experiment, I ve noticed that valgrind shows that I have some memory leaks. I am pretty sure that I am deleting every allocated object though. Here is my main:
#include "LinkedList.hpp"
#include <ctime>
#include <cstdlib>
bool doConnect() {
int r;
r = rand();
return ((r % 2) == 1);
}
int main() {
srand(time(NULL));
int size = 100000;
int i = 0;
LinkedList *node_list = new LinkedList();
LinkedList *dead_node_list = new LinkedList();
for (i=0; i < size; i++) {
node_t *new_node = new node_t();
if (doConnect()) {
node_list->add(new_node);
}
else {
dead_node_list->add(new_node);
}
}
for (i=0; i < dead_node_list->getSize(); i++)
dead_node_list->deleteFirst();
for (i=0; i < node_list->getSize(); i++)
node_list->deleteFirst();
delete node_list;
delete dead_node_list;
return 0;
}
And here is valgrind's output:
==15291== Memcheck, a memory error detector
==15291== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==15291== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==15291== Command: ./main
==15291==
==15291==
==15291== HEAP SUMMARY:
==15291== in use at exit: 199,720 bytes in 24,965 blocks
==15291== total heap usage: 100,002 allocs, 75,037 frees, 800,048 bytes allocated
==15291==
==15291== LEAK SUMMARY:
==15291== definitely lost: 16 bytes in 2 blocks
==15291== indirectly lost: 199,704 bytes in 24,963 blocks
==15291== possibly lost: 0 bytes in 0 blocks
==15291== still reachable: 0 bytes in 0 blocks
==15291== suppressed: 0 bytes in 0 blocks
==15291== Rerun with --leak-check=full to see details of leaked memory
==15291==
==15291== For counts of detected and suppressed errors, rerun with: -v
==15291== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Am I missing something obvious?
I compile my code with: g++ -Wall -g LinkedList.cpp main.cpp -o main
Upvotes: 0
Views: 224
Reputation: 5311
What did you think the variable i
is doing in this code?
for (i=0; i < dead_node_list->getSize(); i++)
dead_node_list->deleteFirst();
The size should be going down, so with i
going up, you only delete half the original nodes.
You didn't show all the code, so I have to assume the missing parts are ordinary. But with that assumption, the above code should have been:
while ( dead_node_list->getSize())
dead_node_list->deleteFirst();
and similar for the other list.
Upvotes: 2