Why does this function give me a segmentation fault after its execution? - c++

Using addElemento() from the template class below gives me a segfault immediatly after its execution, and I can't understand why.
template <typename tipoT>
class Celula{
private:
tipoT elem;
Celula *prox;
public:
Celula() { prox = nullptr; }
friend class LinkedList<tipoT>;
};
template <typename tipoT>
class LinkedList{
private:
int tamanho;
Celula<tipoT> *cabeca;
Celula<tipoT> *ultima;
public:
LinkedList() : tamanho(0) { cabeca = new Celula<tipoT>(); ultima = cabeca; }
~LinkedList() { limpaLista(); delete cabeca; }
int getTamanho() { return tamanho; }
bool estaVazia() { return tamanho == 0; }
tipoT *getElemento(int);
void addElemento(tipoT);
void limpaLista();
};
template <typename tipoT>
void LinkedList<tipoT>::addElemento(tipoT novoElem){
Celula<tipoT> *nova = new Celula<tipoT>();
nova->elem = novoElem;
ultima->prox = nova;
ultima = nova;
tamanho++;
}
template <typename tipoT>
tipoT *LinkedList<tipoT>::getElemento(int id){
erroAssert(id > 0, "ID smaller or equal to zero");
erroAssert(id <= tamanho, "ID bigger than size");
Celula<tipoT> *aux = cabeca;
for(int i = 0; i < id; ++i){
erroAssert(aux != nullptr, "Null cell");
aux = aux->prox;
}
return &aux->elem;
}
My test main:
int main(){
LinkedList<int> *listaDeInts = new LinkedList<int>();
cout << "Lista criada." << endl;
listaDeInts->addElemento(20);
cout << *listaDeInts->getElemento(1) << endl;
return 0;
}
that when executed, prints in the console:
Lista criada.
Segmentation fault
Celula has no destructor, and getElemento() returns the item's address correctly. So why does the segfault happen? While debugging in gdb the execution stops as addElemento() ends, so I don't understand what's happening.

Related

Printing std::strings produces no output

I was building a small template stack class for a side project and it appeared to be working correctly. However, when I tried it with strings it doesn't appear to work. I have no compilation errors or warnings. I simply get no output. I'm a little rusty in C++ but I wasn't expecting to get blocked by a problem that seems this simple.
My main function (for testing):
#include <iostream>
#include <fstream>
#include <string>
#include "myStack.h"
int main()
{
bool repeat = true;
int option = -1;
std::cout << "Option (1 - String | 2 - Integer) : ";
std::cin >> option;
std::cout << "\n";
switch (option)
{
case 1:
{
myStack<std::string> stringStack;
stringStack.push("!");
stringStack.push("there");
stringStack.push("Hello");
stringStack.show();
break;
}
case 2:
{
myStack<int> intStack;
intStack.push(3);
intStack.push(2);
intStack.push(1);
intStack.show();
break;
}
default:
break;
}
std::cout << "\n";
return 0;
}
Relevant parts of my stack class:
#pragma once
template <typename T>
class myStack
{
private:
T *elements;
size_t capacity;
public:
myStack();
T top();
size_t size();
void push(T pushed);
void pop();
bool isEmpty();
void show(std::ostream &out = std::cout);
};
template <typename T>
myStack<T>::myStack()
{
this->elements = NULL;
this->capacity = 0;
}
template <typename T>
void myStack<T>::push(T pushed)
{
this->elements = (T *)realloc(this->elements, (this->capacity + 1) * sizeof(T));
this->elements[this->capacity] = pushed;
this->capacity++;
}
template<typename T>
void myStack<T>::show(std::ostream &out)
{
for (int i = this->capacity - 1; i >= 0; i--)
{
out << this->elements[i] << std::endl;
}
}
Use of
this->elements = (T *)realloc(this->elements, (this->capacity + 1) * sizeof(T));
this->elements[this->capacity] = pushed;
to manage an array of std::strings is not right. Use of realloc to allocate memory is ok. However, it does not initialize the object properly. The second line is cause for undefined behavior since the object was not initialized properly.
If you are allowed to use std::vector, use it.
template <typename T>
class myStack
{
private:
std::vector<T> elements;
...
};
Then, use of capacity can be replaced by elements.size().
push can be simplified to:
template <typename T>
void myStack<T>::push(T pushed)
{
this->elements.push_back(pushed);
}
You'll have to update show() accordingly too.

Link List of Class, How to get call toString while Transversing?

I was wondering how I could call the toString() method in my Link List of the class BoxClass. BoxClass has a double length, width and height.
my BoxClass:
class BoxClass{
private:
double length;
double width;
double height;
public:
// Default constructor w/ no parameters
BoxClass(){
length = 0;
width = 0;
height = 0;
}
// Constructor with arguments
BoxClass(double boxLength, double boxWidth, double boxHeight){
length = boxLength;
width = boxWidth;
height = boxHeight;
}
// Setters and Getters
void setLength(double boxLength){
length = boxLength;
}
double getLength(){
return length;
}
void setWidth(double boxWidth){
width = boxWidth;
}
double getWidth(){
return width;
}
void setHeight(double boxHeight){
height = boxHeight;
}
double getHeight(){
return height;
}
// Returns the volume of the boxes
double Volume(){
return (length * width * height);
}
// toString method for boxes, returns "(length) x (width) x (height) string
string toString(){
return ("(" + to_string(length)+ "x" + to_string(width) + "x" + to_string(height) + ")");
}
}; // End of BoxClass() class
LinkNode.h
//Template ListNode class definition.
#ifndef LINKNODE_H
#define LINKNODE_H
template <typename T> class LinkList;
template <typename T> class LinkNode{
friend class LinkNode <T>;
public:
LinkNode(const T &);
T getData()const;
T data;
LinkNode <T> *nextPtr;
};
template <typename T> LinkNode <T>::LinkNode(const T &info):data(info), nextPtr(NULL){
// Empty body
}
template <typename T>T LinkNode<T>::getData()const{
return data;
}
#endif
Main (Creating the class, adding it to Link List
// Create the Box class
BoxClass userBox(length, width, height);
// Add box class to Link List
Box.insertNode(userBox);
Box.print();
LinkList.h print() method
template<typename T>void LinkList<T>::print()const {
// To list off nodes
int counter = 1;
if (isEmpty()) {
cout << "No boxes in list!\n";
} else {
LinkNode<T>*currentPtr = firstPtr;
cout << "Your boxes in increasing order of volume is:\n";
// while (currentPtr) {
while (currentPtr != NULL) {
// Output as "#. (length x width x height)
cout << counter << ". " << currentPtr->data << endl;
printf(" %i. %.2f\n", counter, currentPtr->data);
currentPtr = currentPtr->nextPtr;
counter++;
}
}
}
LinkList.h
//Template LinkList class definition.
#ifndef LINKLIST_H
#define LINKLIST_H
#include <iostream>
#include "LinkNode.h"
using namespace std;
template<typename T> class LinkList {
public:
LinkList();
void addNode(const T &);
void insertNode(const T &);
bool isEmpty() const;
void print() const;
private:
LinkNode<T>*firstPtr;
LinkNode<T>*getNewNode(const T &);
};
template<typename T>LinkList<T>::LinkList() :firstPtr(NULL) {
// Empty body
}
template <typename T>void LinkList<T>::insertNode(const T &value) {
LinkNode<T>*newPtr = getNewNode(value);
bool inserted = false;
if (isEmpty() || (newPtr->data < firstPtr->data)) {
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
// cout << " " << newPtr->data << " inserted at front of list.\n";
printf(" %.2f inserted at front of list.\n", newPtr->data);
} else {
LinkNode<T>*currentPtr = firstPtr;
while (currentPtr->nextPtr && !inserted) {
if (newPtr->data < currentPtr->nextPtr->data) {
// cout << " " << newPtr->data << " inserted before " << currentPtr->nextPtr->data << ". " << endl;
printf(" %.2f inserted before %.2f.\n", newPtr->data, currentPtr->nextPtr->data);
newPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr = newPtr;
inserted = true;
} else {
currentPtr = currentPtr->nextPtr;
}
}
if (!inserted) {
currentPtr->nextPtr = newPtr;
printf(" %.2f inserted at the end of list.\n", newPtr->data);
}
}
}
template<typename T>bool LinkList<T>::isEmpty()const {
return firstPtr == NULL;
}
template<typename T>LinkNode<T>*LinkList<T>::getNewNode(const T &value) {
return new LinkNode<T>(value);
}
template<typename T>void LinkList<T>::print()const {
// To list off nodes
int counter = 1;
if (isEmpty()) {
cout << "No boxes in list!\n";
} else {
LinkNode<T>*currentPtr = firstPtr;
cout << "Your boxes in increasing order of volume is:\n";
// while (currentPtr) {
while (currentPtr != NULL) {
// Output as "#. (length x width x height)
cout << counter << ". " << currentPtr->data << endl;
printf(" %i. %.2f\n", counter, currentPtr->data);
currentPtr = currentPtr->nextPtr;
counter++;
}
}
}
#endif
So again, my question is- How do I go about traversing the list and calling the toString() BoxClass method? I tried everything from cout << data.toString() << endl; but that doesn't work. I've been stuck on this for days, can someone help me out?
edit: added LinkList.h
When you write template <typename T> class LinkNode{ you are specifically stating that your node class will have no built-in knowledge of the type of the node that it contains.
You have not shown us your LinkList<T> class, but obviously, the same thing applies to it: since it consists of LinkNode<T> it has to also accept a generic parameter of type <T>, so it cannot have built-in knowledge of the actual type of <T> either.
Therefore, you cannot suddenly introduce a method which has such knowledge. It does not make sense. "It does not compute".
What you need to do instead is add this print() method of yours elsewhere, and make it accept a LinkList<BoxClass>. Then, it will be able to view the LinkNodes as LinkNode<BoxClass>, and it will be able to invoke linkNode.data.toString().
The problem is that your implementation of LinkList<T> class has no way for the client code to go through each node of the list in a loop. What if we don't want to print, but do something else with each box?
In addition, it would look weird if I have a LinkList<Widget>, and I see the text when I call print():
"Your boxes in increasing order of volume is:";
I would say, "what boxes? what volume? I have Widgets, not boxes".
A more complete implementation would look something like this (caveat: This has not been compiled. It is to give you the gist of what you should be doing):
template<typename T> class LinkList {
public:
LinkList();
void addNode(const T &);
void insertNode(const T &);
bool isEmpty() const;
// this is what you're missing from the current implementation
typedef LinkNode<T>* Iterator;
Iterator begin() { return firstPtr; }
Iterator next(Iterator ptr) { return ptr->nextPtr; }
Iterator end() { return NULL; }
private:
LinkNode<T>* firstPtr;
LinkNode<T>* getNewNode(const T &);
};
Then with this, the print function need not be part of the linked list. It can live on the outside:
LinkList<BoxClass> boxList;
//...
void print()
{
if (boxList.isEmpty())
cout << "No boxes in list!\n";
else
{
int counter = 1;
cout << "Your boxes in increasing order of volume is:\n";
// get first box
LinkList<BoxClass>::Iterator curBox = boxList.begin();
// loop until no more boxes
while (curBox != boxList.end())
{
// now use curBox to do whatever you want with this box
BoxClass& b = curBox->getData();
cout << counter << ". " << b.toString();
// go to the next box
curBox = boxList.next(curBox);
counter++;
}
}
}
Note how print is no longer a member of LinkList. Also, note the typedef to give us a "nice" name for the LinkNode pointer that the client uses. The comments should be self-explanatory.
I didn't want to overcomplicate the code by introducing a "real" iterator (i.e. overloaded ++), but that operator would replace the LinkList<T>:::next() function call. I leave that to you as an additional exercise.

Super basic hash table set-up memory allocation errors

Yes I am still learning C++ and I am trying to create my own Hash.h header with several hash functions (non-cryptic) such as MurmurHash and CityHash. To start this off I just wanted a very basic implementation first. But it is not compiling as I have some memory errors. It is due to adding a entry where data is passed to the class function. Changing to pointers/references gave similar errors and I just can't seem to fix it. Can somebody see what is going wrong such that I know what to look for in the future? Thanks!
//Hash.h
template<class T>
class HashEntry{
private:
int key;
T data;
public:
HashEntry() {};
void putData(T input, int keyVal){
//SEGMENTATION FAULT HAPPENS HERE!!
key = keyVal;
data = input;
}
T getData(){
return data;
}
};
template<class T>
class Hash{
private:
int myFunc;
size_t sizeHash;
HashEntry<T> **table;
public:
Hash(int func, size_t size): myFunc(func), sizeHash(size-1) {
table = new HashEntry<T>*[sizeHash];
for (int i=0;i<sizeHash;i++) table[i] = NULL;
}
int hashFunc(int key){
try{
if (myFunc == 0){
return key % sizeHash;
} else if (myFunc == 1){
//other has func's
} else {string excep = "No such hash function!"; throw excep;}
} catch(string e) {cout << "Hash::hashFunc(): Exception Raised: " << e << endl; exit(0);}
return -1;
}
void addEntry(T data, int key){
int myHash = hashFunc(key);
table[myHash]->putData(data, key);
}
};
Call from main:
Hash<char> *myHash = new Hash<char>(0,10);
myHash->addEntry('s', 11);
return 0;

Class Template and Reference Return Type

Long-time reader, first-time poster!
A few comments before I begin: I'm not looking for anyone to do my work for me, I just need a little guidance. Also, I've done a decent amount of googling, and I haven't been able to find any solutions yet.
I have a class assignment that involves creating a template for the following class:
class SimpleStack
{
public:
SimpleStack();
SimpleStack& push(int value);
int pop();
private:
static const int MAX_SIZE = 100;
int items[MAX_SIZE];
int top;
};
SimpleStack::SimpleStack() : top(-1)
{}
SimpleStack& SimpleStack::push(int value)
{
items[++top] = value;
return *this;
}
int SimpleStack::pop()
{
return items[top--];
}
Everything seems to work except SimpleStack& push(int value):
template <class T>
class SimpleStack
{
public:
SimpleStack();
SimpleStack& push(T value);
T pop();
private:
static const int MAX_SIZE = 100;
T items[MAX_SIZE];
int top;
};
template <class T>
SimpleStack<T>::SimpleStack() : top(-1)
{}
template <class T>
SimpleStack& SimpleStack<T>::push(T value)
{
items[++top] = value;
return *this;
}
template <class T>
T SimpleStack<T>::pop()
{
return items[top--];
}
I keep getting the following errors on the definition of SimpleStack& push(int value): "use of class template requires template argument list," and "unable to match function definition to an existing declaration."
Here is main if it helps:
#include <iostream>
#include <iomanip>
#include <string>
#include "SimpleStack.h"
using namespace std;
int main()
{
const int NUM_STACK_VALUES = 5;
SimpleStack<int> intStack;
SimpleStack<string> strStack;
SimpleStack<char> charStack;
// Store different data values
for (int i = 0; i < NUM_STACK_VALUES; ++i)
{
intStack.push(i);
charStack.push((char)(i + 65));
}
strStack.push("a").push("b").push("c").push("d").push("e");
// Display all values
for (int i = 0; i < NUM_STACK_VALUES; i++)
cout << setw(3) << intStack.pop();
cout << endl;
for (int i = 0; i < NUM_STACK_VALUES; i++)
cout << setw(3) << charStack.pop();
cout << endl;
for (int i = 0; i < NUM_STACK_VALUES; i++)
cout << setw(3) << strStack.pop();
cout << endl;
return 0;
}
Sorry for the excessive code pasting!
Make it
template <class T>
SimpleStack<T>& SimpleStack<T>::push(T value) {...}

Unexplainable call to class constructor generates "matching function error"

When I call merge_sort I get a string of errors as such the most readable is:
no matching function call to dynamic_array<int>::dynamic_array()
Does having a base class instantiate a sub class cause that sub-class to re-instantiate the calling base class?
This was my first guess.
// Calling main function
#include "c_dynamic_array.cpp"
int main()
{
dynamic_array<int> d1(20);
d1.order();cout << d1 << endl;
d1.rorder();cout << d1 << endl;
d1.randorder();cout << d1 << endl;
d1.merge_sort();cout << d1 << endl; // This line starts a string of errors
}
// Dynamic Array Class and Merge Inner (merge sort) Class
#include "c_include.cpp"
/*
Dynamic Array
*/
using namespace std;
template <typename> class merge_inner;
template <class T> class dynamic_array
{
protected:
T* array;
public:
int size;
void rorder();
void order();
void randorder();
void print_operator(ostream&)const;
dynamic_array(int sizein)
{
size=sizein;
array=new T[size]();
}
void merge_sort()
{
merge_inner<T> M1;
}
};
template <class T> void dynamic_array<T>::print_operator(ostream &os=cout)const
{
for (int i = 0; i < size; i++) os << array[i] << endl;
}
template <class T> void dynamic_array<T>::randorder()
{
srand(time(NULL));
int *ap;
for(ap=array;ap!=array+size;++ap){*ap=rand()%size;}
}
template <class T> void dynamic_array<T>::order()
{
int *ap,i=0;
for(ap=array;ap!=array+size;++ap)
{
*ap=i;
++i;
}
}
template <class T> void dynamic_array<T>::rorder()
{
int *ap,i=size-1;
for(ap=array;ap!=array+size;++ap)
{
*ap=i;
--i;
}
}
template<class T> ostream& operator<<(ostream& stream, dynamic_array<T> const& data)
{
data.print_operator(stream);
return stream;
}
/*
Merge Sort
*/
template <class T> class merge_inner : public dynamic_array <T>
{
using dynamic_array<T>::array;
private:
const static int size;
T *scratch;
void flip_if_unordered(int &x, int &y)
{
if(array[x]>array[y])
{
int tmp=array[x];
array[x]=array[y];
array[y]=tmp;
}
}
void merge_algo(int &left, int &right_begin, int &right)
{
int iter,iter_left=left,iter_right=right_begin;
for(iter=left;iter<=right;++iter)
{
if( (iter_right>right) || ((iter_left < right_begin) && (array[iter_left]<=array[iter_right])))
{
scratch[iter]=array[iter_left];
++iter_left;
}
else
{
scratch[iter]=array[iter_right];
++iter_right;
}
}
for(iter=left;iter<=right;++iter){array[iter]=scratch[iter];}
}
void merge_recurse(int left,int right)
{
int left_end=(left+((right-left)/2));
int right_begin=left_end+1;
if(((left+1)==right)){flip_if_unordered(left,right);return;}
else if ((left==right)){return;}
else
{
merge_recurse(left,left_end);
merge_recurse(right_begin,right);
merge_algo(left,right_begin,right);
}
}
public:
merge_inner()
{
scratch = new T[size]();
if(scratch != NULL){merge_recurse(0, size);}
}
};
/*Quick Sort
void quick_sort()
{
quick_recurse(0,size);
}
void quick_recurse(int left, int right)
{
int l = left, r = right, tmp;
int pivot = array[(left + right) / 2];
while (l <= r)
{
while (array[l] < pivot)l++;
while (array[r] > pivot)r--;
if (l <= r)
{
tmp = array[l];
array[l] = array[r];
array[r] = tmp;
l++;
r--;
}
}
if (left < r)quick_recurse(left, r);
if (l < right)quick_recurse(l, right);
}
*/
dynamic_array seems to be missing a default constructor, and since it has a custom constructor the compiler will not provide one. Add this to your class:
dynamic_array()
{
size = 0;
array = new T[0](); // or NULL, but note that new T[0] will be != NULL
}
Alternatively, provide a default sizein for your existing constructor so that it can be used as a default constructor as well:
dynamic_array(int sizein = 0)
Since your base class dynamic_array<T> doesn't have a default constructor, every derived class constructor must call some base constructor one way or another. Put the base initialization in the constructor initializer list. For example:
template <typename T>
class merge_inner : public dynamic_array<T>
{
public:
merge_inner() : dynamic_array<T>(0) { }
// ...
};