Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly?
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <cstdlib>
#include <iostream>
using namespace std;
// Node class
class Node {
int data;
Node* next;
Node* prev;
public:
Node();
Node(int);
void SetData(int newData) { data = newData; };
void SetNext(Node* newNext) { next = newNext; };
void SetPrev(Node* newPrev) { prev = newPrev; };
int getData() { return data; };
Node* getNext() { return next; };
Node* getPrev() { return prev; };
};
class DynamicArray
{
Node *head;
int size;
public:
DynamicArray();
DynamicArray(int);
~DynamicArray(); //destructor to free memory
void addElementFront(int);
void addElementBack(int);
void insertElement(int, int);
void delementelement(int, int);
void del(int val);
void removeFront();
void removeBack();
int sizeArray();
bool contains(int);
void print();
};
Node::Node()
{
}
Node::Node(int val)
{
data = val;
}
DynamicArray::DynamicArray()
{
head = NULL;
size = 0;
}
DynamicArray::DynamicArray(int s)
{
head = new Node();
for(int i = 0; i < s - 1; i++)
{
addElementBack(0);
}
size = s;
}
DynamicArray::~DynamicArray()
{
//delete all nodes
delete head;
}
/*
* Function that adds the given value to the front of the
array
*/
void DynamicArray::addElementFront(int val)
{
if (head == NULL)
{
head = new Node(val);
head->SetNext(NULL);
}
else {
Node *newNode = new Node(val);
newNode->SetNext(head);
head->SetPrev(newNode);
head = newNode;
}
size++;
}
/*
* Function that adds the given value to the back of the array
*/
void DynamicArray::addElementBack(int val)
{
if (head == NULL)
{
head = new Node(val);
}
else if (head->getNext() == NULL)
{
Node* newNode = new Node(val);
head->SetNext(newNode);
newNode->SetPrev(head);
}
else {
Node *temp = head->getNext();
while (temp->getNext() != NULL)
{
temp = temp->getNext();
}
Node *newNode = new Node(val);
newNode->SetPrev(temp);
temp->SetNext(newNode);
}
size++;
}
/*
* Inserts a value at a given location. If the location in the array
is
* filled, it shifts the values from that location on one
time.
*/
void DynamicArray::insertElement(int loc, int
val)
{
if(loc == 0) //insert at head
{
Node *newNode = new Node(val);
newNode->SetNext(head);
head = newNode;
}
else
{
Node *temp = head;
int step = 0;
while (step != loc - 1)
{
temp = temp->getNext();
step++;
}
temp->SetNext(new Node(val));
}
size++;
}
/*
* Subtracts a given value from an element at a given location. If
the
* subtraction results in a negative number, that element is set to
zero.
*/
void DynamicArray::delementelement(int loc, int
val)
{
if(loc == 0) //delement head
{
head->SetData(head->getData() - val);
}
else
{
Node *temp = head;
int step = 0;
while (step != loc)
{
temp = temp->getNext();
step++;
}
temp->SetData(temp->getData() - val);
}
}
/*
* Deletes a given value from the array
*/
void DynamicArray::del(int val)
{
if (head == NULL)
{
printf("%s", "error: array is empty");
}
else if (head->getNext() == NULL && head->getData()
== val)
{
head = NULL;
size--;
}
else
{
Node *temp = head->getNext();
bool found = false;
while (temp != NULL && !found)
{
if(temp->getData() == val)
{
(temp->getPrev())->SetNext(temp->getNext());
(temp->getNext())->SetPrev(temp->getPrev());
found = true;
}
temp = temp->getNext();
}
delete temp;
size--;
}
}
void DynamicArray::removeFront()
{
if (head == NULL)
{
printf("%s", "error: array is empty");
}
else
{
Node *temp = head;
head = head->getNext();
head->SetPrev(NULL);
delete temp;
size--;
}
}
void DynamicArray::removeBack()
{
if (head == NULL)
{
printf("%s", "error: array is empty");
}
else if (head->getNext() == NULL)
{
head = NULL;
size--;
}
else
{
Node *temp = head->getNext();
while (temp->getNext() != NULL)
{
temp = temp->getNext();
}
(temp->getPrev())->SetNext(NULL);
delete temp;
size--;
}
}
int DynamicArray::sizeArray()
{
return size;
}
void DynamicArray::print()
{
Node *temp = head;
do {
printf("%d", temp->getData());
temp = temp->getNext();
} while(temp != NULL);
printf("%s", "\n");
}
bool DynamicArray::contains(int val)
{
if (head == NULL)
{
return false;
}
else
{
Node *temp = head;
do
{
if(temp->getData() == val)
{
return true;
}
temp = temp->getNext();
} while (temp != NULL);
}
return false;
}
#endif /* DYNAMICARRAY_H */
I didn't get segmentation fault for addElementBack. But in print i got. You have to check whether head is null or not before printing the values. This check is not there in your print function. That's why you got segmentation fault. The following is the program i tried and i fixed print function. But i'm didn't get segmentation fault in addElementBack. Please post your code (with main function) so that we can trace it.
Code:
main.cpp:
#include<iostream>
using namespace std;
#include "dynamicarray.h"
int main(void)
{
DynamicArray da;
da.print();
for(int i=10;i<60;i+=10)
da.addElementBack(i);
da.print();
return 0;
}
dynamicarray.cpp:
#include<stdio.h>
#include<iostream>
using namespace std;
#include "dynamicarray.h"
Node::Node()
{
}
Node::Node(int val)
{
data = val;
}
DynamicArray::DynamicArray()
{
head = NULL;
size = 0;
}
DynamicArray::DynamicArray(int s)
{
head = new Node();
for(int i = 0; i < s - 1; i++)
{
addElementBack(0);
}
size = s;
}
DynamicArray::~DynamicArray()
{
//delete all nodes
delete head;
}
/*
* Function that adds the given value to the front of the
array
*/
void DynamicArray::addElementFront(int val)
{
if (head == NULL)
{
head = new Node(val);
head->SetNext(NULL);
}
else {
Node *newNode = new Node(val);
newNode->SetNext(head);
head->SetPrev(newNode);
head = newNode;
}
size++;
}
/*
* Function that adds the given value to the back of the array
*/
void DynamicArray::addElementBack(int val)
{
if (head == NULL)
{
head = new Node(val);
}
else if (head->getNext() == NULL)
{
Node* newNode = new Node(val);
head->SetNext(newNode);
newNode->SetPrev(head);
}
else {
Node *temp = head->getNext();
while (temp->getNext() != NULL)
{
temp = temp->getNext();
}
Node *newNode = new Node(val);
newNode->SetPrev(temp);
temp->SetNext(newNode);
}
size++;
}
/*
* Inserts a value at a given location. If the location in the array
is
* filled, it shifts the values from that location on one
time.
*/
void DynamicArray::insertElement(int loc, int val)
{
if(loc == 0) //insert at head
{
Node *newNode = new Node(val);
newNode->SetNext(head);
head = newNode;
}
else
{
Node *temp = head;
int step = 0;
while (step != loc - 1)
{
temp = temp->getNext();
step++;
}
temp->SetNext(new Node(val));
}
size++;
}
/*
* Subtracts a given value from an element at a given location. If
the
* subtraction results in a negative number, that element is set to
zero.
*/
void DynamicArray::delementelement(int loc, int val)
{
if(loc == 0) //delement head
{
head->SetData(head->getData() - val);
}
else
{
Node *temp = head;
int step = 0;
while (step != loc)
{
temp = temp->getNext();
step++;
}
temp->SetData(temp->getData() - val);
}
}
/*
* Deletes a given value from the array
*/
void DynamicArray::del(int val)
{
if (head == NULL)
{
printf("%s", "error: array is empty");
}
else if (head->getNext() == NULL && head->getData()
== val)
{
head = NULL;
size--;
}
else
{
Node *temp = head->getNext();
bool found = false;
while (temp != NULL && !found)
{
if(temp->getData() == val)
{
(temp->getPrev())->SetNext(temp->getNext());
(temp->getNext())->SetPrev(temp->getPrev());
found = true;
}
temp = temp->getNext();
}
delete temp;
size--;
}
}
void DynamicArray::removeFront()
{
if (head == NULL)
{
printf("%s", "error: array is empty");
}
else
{
Node *temp = head;
head = head->getNext();
head->SetPrev(NULL);
delete temp;
size--;
}
}
void DynamicArray::removeBack()
{
if (head == NULL)
{
printf("%s", "error: array is empty");
}
else if (head->getNext() == NULL)
{
head = NULL;
size--;
}
else
{
Node *temp = head->getNext();
while (temp->getNext() != NULL)
{
temp = temp->getNext();
}
(temp->getPrev())->SetNext(NULL);
delete temp;
size--;
}
}
int DynamicArray::sizeArray()
{
return size;
}
void DynamicArray::print()
{
Node *temp = head;
if(temp==NULL) return;
do {
printf("%d", temp->getData());
temp = temp->getNext();
} while(temp != NULL);
printf("%s", "\n");
}
bool DynamicArray::contains(int val)
{
if (head == NULL)
{
return false;
}
else
{
Node *temp = head;
do
{
if(temp->getData() == val)
{
return true;
}
temp = temp->getNext();
} while (temp != NULL);
}
return false;
}
dynamicarray.h:
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <cstdlib>
#include <iostream>
using namespace std;
// Node class
class Node {
int data;
Node* next;
Node* prev;
public:
Node();
Node(int);
void SetData(int newData) { data = newData; };
void SetNext(Node* newNext) { next = newNext; };
void SetPrev(Node* newPrev) { prev = newPrev; };
int getData() { return data; };
Node* getNext() { return next; };
Node* getPrev() { return prev; };
};
class DynamicArray
{
Node *head;
int size;
public:
DynamicArray();
DynamicArray(int);
~DynamicArray(); //destructor to free memory
void addElementFront(int);
void addElementBack(int);
void insertElement(int, int);
void delementelement(int, int);
void del(int val);
void removeFront();
void removeBack();
int sizeArray();
bool contains(int);
void print();
};
#endif /* DYNAMICARRAY_H */
output:
10
20
30
40
50
Get Answers For Free
Most questions answered within 1 hours.