How to access array elements of pointers - c++

How can I access elements from my Node pointer array? The cout at the bottom returns an address "0xb7738ff4". Even if I make a constructor and set each element to NULL, I cannot modify any of them later on.
#include <iostream>
using namespace std;
class Node
{
public:
char character;
};
class Tree
{
public:
Node* nodes[26];
};
int main() {
Tree t;
//t.nodes[0]->character = 'a';
cout << "\"" << (t.nodes[0]) << "\"";
}
http://ideone.com/XvLme9

t.nodes[0] is returning a Node* pointer. You are passing that pointer as-is to cout, that is why it is printing a memory address (and a random one at that, because you are not initializing the array). If you want to print the character of a node, you have to dereference the Node* pointer, exactly like your commented out code is doing (which is the correct way to do it):
t.nodes[0]->character
You just have to make sure that nodes[0] returns a valid pointer to a real Node object to begin with:
Tree t;
t.nodes[0] = new Node; // <-- here
t.nodes[0]->character = 'a';
std::cout << "\"" << t.nodes[0]->character << "\"" << std::endl;
Don't forget to delete the node when you are done using it. Tree should have a destructor that frees the nodes it owns.
Try something more like this:
#include <iostream>
#include <stdexcept>
class Node
{
public:
char character;
Node(char c = 0);
};
class Tree
{
private:
Node* nodes[26];
int count;
public:
Tree();
~Tree();
Node* add(char c);
Node* get(int idx);
};
Node::Node(char c)
: character(c)
{
}
Tree::Tree()
: count(0)
{
for (int i = 0; i < 26; ++i)
nodes[i] = NULL;
}
Tree::~Tree()
{
for (int i = 0; i < count; ++i)
delete nodes[i];
}
Node* Tree::add(char c)
{
if (count == 26)
throw std::runtime_error("nodes array is at its max capacity");
Node *node = new Node(c);
nodes[count++] = node;
return node;
}
Node* Tree::get(int idx)
{
if ((idx < 0) || (idx >= count))
throw std::out_of_range("invalid index");
return nodes[idx];
}
int main()
{
Tree t;
t.add('a');
std::cout << "\"" << t.get(0)->character << "\"" << std::endl;
}
With that said, you should use std::list or std::forward_list instead of writing your own tree class:
#include <list>
int main()
{
std::list<char> l;
l.push_back('a');
std::cout << "\"" << l.front() << "\"" << std::endl;
}
Or:
#include <list>
class Node
{
public:
char character;
// other things here...
Node(char c = 0);
Node(const Node &src);
Node& operator=(const Node &rhs);
};
Node::Node(char c)
: character(c)
{
}
Node::Node(const Node &src)
: character(src.character)
{
}
Node& Node::operator=(const Node &rhs)
{
character = src.character;
return *this;
}
int main()
{
std::list<Node> l;
l.push_back('a');
std::cout << "\"" << l.front().character << "\"" << std::endl;
}

Related

Destructors called without matching constructor

I am trying to figure out how C++ resource management works, especially in relation to constructors/destructors. This is my test code;
ylist.h
#pragma once
template <class T>
struct ListNode
{
T elem;
struct ListNode* next;
ListNode(T elem):elem(elem),next(nullptr) {}
};
template <class T>
class List
{
ListNode<T> *head;
public:
List() : head(nullptr)
{
}
~List()
{
ListNode<T>* cursor = head;
while (cursor != nullptr)
{
ListNode<T>* next = cursor->next;
delete cursor;
cursor = next;
}
}
void append(T item)
{
ListNode<T>* n = new ListNode<T>(item);
if (head == nullptr)
{
head = n;
}
else
{
ListNode<T>* cursor = head;
while (cursor->next != nullptr)
cursor = cursor->next;
cursor->next = n;
}
}
};
main.cpp
#include <iostream>
#include "ylist.h"
using namespace std;
class myObj
{
int val;
public:
myObj(int val):val(val)
{
cout << "myObj#" << this << "(" << val << ")" << endl;
}
~myObj()
{
cout << "~myObj#" << this << "(" << val << ")" << endl;
}
};
int main()
{
List<myObj> myList;
for (int i = 0; i < 3; i++)
{
myList.append(myObj(i));
}
}
And here is the output;
myObj#00000039614FFAC0(0)
~myObj#00000039614FFAD0(0)
~myObj#00000039614FFAC8(0)
~myObj#00000039614FFAC0(0)
myObj#00000039614FFAC0(1)
~myObj#00000039614FFAD0(1)
~myObj#00000039614FFAC8(1)
~myObj#00000039614FFAC0(1)
myObj#00000039614FFAC0(2)
~myObj#00000039614FFAD0(2)
~myObj#00000039614FFAC8(2)
~myObj#00000039614FFAC0(2)
~myObj#0000019878DF6100(0)
~myObj#0000019878DF5F20(1)
~myObj#0000019878DF6200(2)
According to above output, constructor called for each object once, but destructors called four times.
I was expecting to see multiple temporary copies being constructed/destructed but why is number of constructors/destructos is not matching.
If, for example, I was opening files or db connections on constructors and closing them on destructors, would I get in trouble in above scenario?
Add this copy constructor to your myObj function in order to see how the additional myObj objects are being constructed. Once you have this, you'll see that the number of constructor-calls matches the number of destructor-calls.
myObj(const myObj & rhs):val(rhs.val)
{
cout << "COPY CTOR myObj#" << this << "(" << val << ")" << endl;
}

A reference of type "Bucket&" (not const qualified) cannot be initialized with a value of type "SortedList." How can this error be fixed?

The error occurs in my SortedList class in function method sortList. I have tried to rename, remove, move, and manipulate certain things to no avail. I will show you a good amount of code so that you can see where I'm coming from.
void SortedList::sortList(const vector <double>& list) {
BucketSort bucketA;
bucketA.insert(list);
bucketA.createSortedList(*this); // <--- Problem here, on this one line.
// The * is highlighted in red in my IDE.
}
Error: A reference of type "Bucket&" (not const qualified) cannot be initialized with a value of type "SortedList"
For reference, if you need to see to get a context of my error:
SortedList.h:
#ifndef SORTEDLIST_H
#define SORTEDLIST_H
#include "bucketsort.h"
#include <vector>
using namespace std;
class SortedList
{
public:
SortedList();
void sortList(const vector <double>& list);
~SortedList();
private:
};
#endif
Bucketsort.h:
#ifndef BUCKETSORT_H
#define BUCKETSORT_H
#include "bucket.h"
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
const int DEFAULTCAPACITY = 10;
class BucketSort
{
public:
// Constructors:
BucketSort();
// Functions:
void print() const;
void insert(const vector <double>& v) const;
void createSortedList(Bucket& a);
//
~BucketSort();
private:
Bucket ** a;
};
#endif
Bucketsort.cpp:
#include "bucketsort.h"
BucketSort::BucketSort() {
a = new Bucket*[DEFAULTCAPACITY]();
}
void BucketSort::print() const {
for (int i = 0; i < DEFAULTCAPACITY; ++i) {
if (a[i] != nullptr) {
a[i]->print();
}
}
}
void BucketSort::insert(const vector <double>& v) const {
int index;
for (int i = 0; i < v.size(); ++i) {
index = v[i] * 10;
if (a[index] == nullptr) {
Bucket* newBucket = new Bucket;
a[index] = newBucket;
}
a[index]->insert(v[i]);
}
}
void BucketSort::createSortedList(Bucket& a){
}
BucketSort::~BucketSort() {
for (int i = 0; i < DEFAULTCAPACITY; ++i) {
if (a[i] != nullptr) {
a[i]->deleteBucket();
}
}
delete a;
a = nullptr;
}
Bucket.h:
#ifndef BUCKET_H
#define BUCKET_H
#include <iostream>
using namespace std;
class Node
{
public:
Node() : item(0.0), link(nullptr) {}
Node(double newItem, Node *newLink) : item(newItem), link(newLink) {}
Node* getLink() const { return link; }
double getItem() const { return item; }
void setItem(double newItem) { item = newItem; }
void setLink(Node *newLink) { link = newLink; }
~Node() {}
private:
double item;
Node *link;
};
class Bucket
{
public:
Bucket();
void insert(double value);
void testPrint() const;
void print() const;
int getNumberOfElements() const;
void deleteBucket();
~Bucket();
private:
Node * ptrToFirst;
Node *ptrToLast;
int numberOfElements;
};
#endif
Bucket.cpp:
#include "bucket.h"
Bucket::Bucket() {
ptrToFirst = nullptr;
ptrToLast = nullptr;
numberOfElements = 0;
}
void Bucket::insert(double value) {
if (numberOfElements != 0) {
Node *newNode = new Node(value, nullptr);
if (value < ptrToFirst->getItem()) {
newNode->setLink(ptrToFirst);
ptrToFirst = newNode;
}
else if (value > ptrToLast->getItem()) {
ptrToLast->setLink(newNode);
ptrToLast = newNode;
}
else if (value != ptrToFirst->getItem()) {
Node *current = ptrToFirst;
while (value > current->getLink()->getItem()) {
current = current->getLink();
}
if (current->getLink()->getItem() != value) {
newNode->setLink(current->getLink());
current->setLink(newNode);
}
}
}
else {
ptrToFirst = new Node(value, ptrToLast);
ptrToLast = ptrToFirst;
}
++numberOfElements;
}
void Bucket::testPrint() const {
cout << "Pointer to first: " << ptrToFirst << endl;
cout << "Pointer to last: " << ptrToLast << endl;
if (ptrToFirst != nullptr && ptrToLast != nullptr) {
cout << "Value of ptrToFirst: " << ptrToFirst->getItem() << endl;
cout << "Value of ptrToLast: " << ptrToLast->getItem() << endl;
}
cout << "Number of elements: " << numberOfElements << endl;
cout << "Contents of bucket: " << endl;
Node *current = ptrToFirst;
while (current != nullptr) {
cout << current->getItem() << " ";
current = current->getLink();
}
cout << endl;
}
void Bucket::print() const {
Node *current = ptrToFirst;
while (current != nullptr) {
cout << current->getItem() << " ";
current = current->getLink();
}
}
int Bucket::getNumberOfElements() const {
return numberOfElements;
}
void Bucket::deleteBucket() {
Node *trailingCurrent;
while (ptrToFirst != nullptr) {
trailingCurrent = ptrToFirst;
ptrToFirst = ptrToFirst->getLink();
delete trailingCurrent;
trailingCurrent = nullptr;
}
ptrToLast = nullptr;
numberOfElements = 0;
}
Bucket::~Bucket() {
deleteBucket();
}
Help?
void SortedList::sortList(const vector <double>& list) {
[...]
bucketA.createSortedList(*this); // <--- Problem here
In the above line, this is of type SortedList *, so *this is of type SortedList &.
... and yet, your createdSortedList method requires an argument of a different type, Bucket &:
void createSortedList(Bucket& a);
Since a SortedList and a Bucket are not the same type, and there is no way (that the compiler knows about) to convert a SortedList object into a Bucket object, the compiler is rightly flagging the call as an error.
To solve the problem, you either need to change createSortedList to take a SortedList & as its argument, instead of a Bucket &, or change your call to pass in a Bucket & instead of a SortedList &.

Trouble implementing a templated singly linked list

I'm trying to implement a templated singly linked list and I'm fairly new to C++
#include <iostream>
#include <string>
#define NEWL "\n"
#define PRINT(s) std::cout << s
#define PRINTL(s) std::cout << s << NEWL
#define PRINTERR(e) std::cerr << e << NEWL
////// Class for a Node
template<class Data> class Node {
Node<Data>* next_ptr;
Data data;
public:
Node(Node<Data>* nxt_ptr) :next_ptr(nxt_ptr) {};
Node(Data d, Node<Data>* nxt_ptr) :data(d), next_ptr(nxt_ptr) {};
Node<Data>* get_next() { return next_ptr; }
Data& get_data() { return data; }
friend std::ostream& operator<<(std::ostream& out, const Node<Data>& node) {
out << node.data;
return out;
};
};
////// Class for a SinglyLinkedList
template<class Data> class SLinkedList {
Node<Data>* head_ptr;
int max_size;
public:
SLinkedList() : head_ptr(nullptr) {};
bool is_empty() {
return head_ptr == nullptr;
};
bool is_full() {
return get_size() == max_size;
};
int get_size() {
if (is_empty()) {
return 0;
}
int count = 0;
for (Node<Data>* it_ptr = head_ptr; it_ptr != nullptr; it_ptr = it_ptr->get_next()) {
count++;
}
return count;
};
void add(Data d) {
if (is_full()) {
throw std::exception("List is full!");
}
Node<Data> new_node(d, head_ptr);
head_ptr = &new_node;
};
void print_content() {
int count = 1;
PRINTL("This list contains:");
for (Node<Data>* it_ptr = head_ptr; it_ptr != nullptr; it_ptr = it_ptr->get_next()) {
PRINTL("\t["<< count << "]" << " at " << it_ptr << " : " << *it_ptr);
count++;
}
}
};
////// Main function
int main()
{
SLinkedList<int> sll;
sll.add(42);
sll.print_content();
}
I can't get this to work. Somehow iterating the list with for-loops does not work. It always results in an Reading Access Violation Exception about a pointer to 0xCCCCCCD0 and I have no idea how to fix this.
Your add function is incorrect
Node<Data> new_node(d, head_ptr);
creates a new function local Node in add. You then set head to the address of that local variable. When the function ends all local variables are destroyed so now head points to an object that no longer exists.
To fix that you need to use the new keyword to create a dynamic object that will live on after the function ends.
Node<Data>* new_node = new Node(d, head_ptr);
head_ptr = new_node;
The down side with this is you need to remember to call delete on all of the nodes you created in the list destructor.
You also have some other bugs in your code. You never set max_size in your constructor so using it at all except to give it a value is undefined behavior as we have no idea what the value of it is going to be. You also never increase the size of the list when you add nodes into the list.

Binary search tree destructor: free() invalid pointer

For my current programming assignment I have to construct a binary search tree and use it to insert words from a file in alphabetical order, to make a concordance list. The program is complete and outputs correctly; the main bulk of the program works perfectly. But when the program finishes and destructors are called, it crashes with error in ./concordancebst : free(): invalid pointer. I'm at a loss as to what the problem is, my destructor looks like it would work and looking at code online it seems as though it should work as well. I could probably get around this by using a std::unique_ptr but it seems a bit overkill in the sense of the scope of the program (also overkill in the sense that we haven't covered smart pointers, everything we have done in the class involving dynamic memory has been handled through manual allocating/deallocating).
BST.h
#ifndef BST_H
#define BST_H
#include <string>
class BST {
public:
BST() { root = nullptr; }
~BST();
void insert(const std::string word);
int getCount(const std::string word);
int length();
friend std::ostream& operator << (std::ostream &out_s, BST b);
private:
struct Node {
std::string word;
int count;
Node *left;
Node *right;
Node() { word = ""; count = 0; left = nullptr; right = nullptr;}
~Node();
};
Node *root;
void RInsert(Node* &t, std::string word);
int countNodes(Node *p);
void print(Node *p, std::ostream &out_s);
};
#endif
BST.cpp
#include "BST.h"
#include <string>
#include <iostream>
#include <iomanip>
BST::Node::~Node() {
delete left;
delete right;
}
BST::~BST() {
delete root;
}
int BST::countNodes(Node *p) {
if(p == nullptr)
return 0;
else
return countNodes(p->left) + countNodes(p->right) + 1;
}
int BST::getCount(const std::string word) {
Node *p = root;
while(true) {
if(p == nullptr)
return 0;
else if(word.compare(p->word) < 0)
p = p->left;
else if(word.compare(p->word) == 0)
return p->count;
else
p = p->right;
}
}
int BST::length() {
return countNodes(root);
}
void BST::RInsert(Node* &t, std::string word) {
if(t == nullptr) {
t = new Node;
t->word = word;
t->left = nullptr;
t->right = nullptr;
t->count++;
}
else if(word.compare(t->word) == 0)
t->count++;
else if(word.compare(t->word) < 0)
RInsert(t->left, word);
else //word.compare(t->word > 0)
RInsert(t->right, word);
}
void BST::insert(const std::string word) {
RInsert(root, word);
}
void BST::print(Node *p, std::ostream &out_s) {
if(p != nullptr) {
print(p->left, out_s);
out_s << std::setw(13) << p->word << std::setw(10) << p->count << std::endl;
print(p->right, out_s);
}
}
std::ostream &operator << (std::ostream &out_s, BST b) {
out_s << std::setw(13) << "Word" << std::setw(10) << "Count" << std::endl;
out_s << "---------------------------------------------" << std::endl;
b.print(b.root, out_s);
out_s << "---------------------------------------------" << std::endl;
out_s << "The file contains " << b.length() << " distinct words." << std::endl;
return out_s;
}
std::ostream &operator << (std::ostream &out_s, BST b)
Pass by value^. b is copied.
Since BST has no copy constructor, the default copy constructor is invoked and does not perform a deep copy. b contains copies of the source BST's pointers, and when b is destroyed on return from the function, it takes all of the source's Nodes with it to the grave.
Fixes are twofold:
Most directly, pass by reference.
std::ostream &operator << (std::ostream &out_s, BST & b)
Indirectly, this code violates the Rule of Three. BST and BST::Node need copy constructors and assignment operators to be used safely.
Edit
A minimal test case would be something like:
#include <iostream>
#include "BST.h"
int main()
{
BST t;
t.insert("A");
std::cout << t << std::endl;
return 0;
}

Postincrementation operator in linked list

I had to write a program that handle this main code:(not allowed to change it)
list<int> iv;
iv["john"] = 23;
int ia = iv["john"]++;
int ib = iv["john"];
cout << ia << " " << ib << endl; // prints 23 24
try{
cout << iv["jack"] << endl; // should throw an exception
}catch(list<int>::Uninitialized&)
{
cout << "Uninitialized map element!" << endl;
};
Here is my code:
#ifndef EXAM_H
#define EXAM_H
#include <iostream>
#include <string>
using namespace std;
template <class TYPE>
class list
{
private:
struct node
{
TYPE value;
string index;
bool isInit;
node *next;
};
node *head;
node *current;
public:
class Cref
{
friend class list;
list& s;
string position;
Cref (list& ss, string pos): s(ss), position(pos) {};
public:
operator TYPE() const
{
return s.read(position);
}
Cref& operator = (TYPE val)
{
s.write(position,val);
return *this;
};
Cref& operator = (const Cref& ref)
{
return operator= ((TYPE)ref);
};
};
class Uninitialized{};
list ()
{
cout << "constructor\n";
head = NULL;
current = NULL;
}
~list ()
{
while (head)
{
node *t = head->next;
delete head;
head = t;
};
}
TYPE read (string ind) const
{
cout << "read\n";
node *t = head;
while(t)
{
if(t->index == ind && t->isInit == true) return t->value;
else t = t->next;
}
throw Uninitialized();
}
void write (string ind, TYPE value_)
{
cout << "write\n";
node *t = new node;
t->next = head;
head = t;
head->value = value_;
head->index = ind;
head->isInit = true;
}
TYPE operator[] (string ind) const
{
cout << "read\n";
node *t = head;
while(t)
{
if(t->index == ind && t->isInit == true) return t->value;
else t = t->next;
}
throw Uninitialized();
}
Cref operator[] (string ind)
{
return Cref(*this, ind);
}
};
#endif
Everything works great, but only when I comment out postincrementation operation in main program
int ia = iv["john"]++;
As you can see I have a struct node where I put all variables and I want to increment value by one in node where the key is "john". Is there any way to implement operator++ for this code ?
I am not allowed to use std::map.
The usual approach to your problem is defining the array subscript operators as
const TYPE& operator[](string ind) const;
TYPE& operator[](string ind);
In this way, you do not have to bother a single bit about the operator++: Since iv["John"] returns a reference to int, iv["John"]++ will call the int post-increment operator which is built-in.
Yes, I have already tried this solution, but compiler do not distinguish between reading and writing and still using non-const version. So I had to build proxy class Cref that helps to distinguish.
I have also already find a solution to operator++ problem.
This operation had to be from Cref level. I created
Cref& operator++ (int val)
{
s.increment(position,val);
return *this;
};
And increment function in main class body as follows:
void increment (string ind, int value_)
{
cout << "increment\n";
node *t = head;
while(t)
{
if(t->index == ind && t->isInit == true) t->value = t->value + 1;
t = t->next;
}
}
That fully solved my problem.