Here is a simple C++ linked list program . The issue i am facing is when I run my code it takes the first data but then next it shows an exception error.I am running this code on Visual Studio 2017. I have tried a lot but couldn't understand the reason why the code is failing to work.
#include<iostream>
using namespace std;
struct Node {
int data;
Node *next;
}*head = NULL, *temp = NULL , *temp1 = NULL;
Node* Create_New_Node(int);
int Insert(Node *);
Node* Create_New_Node(int a)
{
Node *np = new Node;
np->data = a;
np->next = NULL;
return np;
}
int Insert(Node *np)
{
if (head == NULL) {
head = np;
return 0;
}
temp1 = head;
while(temp1 != NULL) {
temp1 = temp1->next;
}
temp1->next = np;
}
int main()
{
char ch = 'y';
int inf;
while (ch == 'y' || ch == 'Y')
{
system("cls");
cout << "Enter data : " << endl;
cin >> inf;
temp = Create_New_Node(inf);
Insert(temp);
cout << "Press y to continue " << endl;
cin >> ch;
}
system("pause");
return 0;
}
Here is the error output :
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Symbols loaded.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Symbols loaded.
Exception thrown: write access violation.
**temp1** was nullptr.
The program '[10588] Project2.exe' has exited with code 0 (0x0).
Can some one help me with the code since I am new to C++ linked list concept as well as Stack Overflow. Do correct me wherever i go wrong Thanks.
Look at this loop
while(temp1 != NULL) { // <---
temp1 = temp1->next;
}
temp1->next = np;
it ends when temp1 is NULL, then you are trying to access next member for NULL pointer as a result you have segmentation fault.
temp1 can be advanced only if temp1->next is not NULL, so can modify your function as follows
while(temp1->next)
{
temp1 = temp1->next;
}
We don't need to check temp1 if is not NULL because iterating over list starting at head node, and temp1 is always updated to non-NULL value by assignment in above loop.
Related
I am going to create a single linked list and construct a function (Locate()) that returns the address of the element.But in the end, I didn't see the result of this function. I tried it. This function should be run, but the result is different from what I expected.
use vs2019 on WIndows10,a student:)
#include<iostream>
using namespace std;
struct Node { //Node
int data;
Node* link;
Node(int item, Node* l = NULL)
{
data = item;
link = l;
}
Node(Node* l = NULL)
{
data = 0;
link = l;
}
};
class Link :public Node { //Link
private:
Node* first;
public:
Link(Node* l = NULL)
{
first = l;
}
Link(int d, Node* l = NULL)
{
first = new Node(d);
}
Node* Locate(int i);
};
Node* Link::Locate(int i) //Locate()
{
if (i < 0)
{
cerr << "wrong operation when locating" << endl;
exit(1);
}
int count = 0;
Node* current = first;
while (count < i && current->link != NULL)
{
current = current->link;
count++;
}
return current;
}
int main()
{
Link a;
Node* b = new Node(1);
Node* c = new Node(2);
a.link = b;
b->link = c;
cout << a.data << ' ' << b->data << ' '<<c->data<<endl;
cout << a.Locate(1) << endl;
return 0;
}
Will not output the result of this function 'Locate()' being called
Locate() accesses first->link. At that time, first is a null pointer. Whereupon your program exhibits undefined behavior; in practice, it most likely crashes.
When I re-modify the List constructor, its(Locate()) output is normal and the expected result is obtained.
The modified constructors are as follows:
Link()
{
first = new Node;
}
Link(int d)
{
first = new Node(d);
}
Node* Locate(int i);
#include<cassert>
#include<initializer_list>
#include<iostream>
class IntArray {
int m_length;
int *m_data;
public:
IntArray() : m_length(0), m_data(nullptr) {}
IntArray(int length) : m_length(length) {
m_data = new int(length);
}
IntArray(const std::initializer_list<int> &list) : IntArray(list.size()) {
int count = 0;
for (auto &element : list) {
std::cout << element << '\n';
m_data[count] = element;
++count;
}
}
~IntArray() { delete[] m_data; }
int& operator[](int index) {
assert(index >= 0 && index < m_length);
return m_data[index];
}
int getLength() { return m_length; }
};
int main() {
IntArray array{ 4,3,2,1 };
for (int i = 0; i < array.getLength(); i++)
std::cout << array[i] << " ";
system("pause");
return 0;
}
Breakpoint achieved at the line system("pause");
Critical error detected in visual studio while the above code works fine in online c++ repl. I just don't get why the above error is showing up on visual studio.
Your program has undefined behaviour. You allocated memory only for one int
m_data = new int(length);
but you are writing more than one elements, so you are modyfing memory area to which you don't have the access - it is UB.
Change to
m_data = new int[length];
I've implemented a method to find a value from a 'Stack'. I just want to know that are there any logical errors or any kind of errors in this code?
Thanks.
bool about::findData(char key)
{
if(top==-1)
cout<<"Stack is Empty"<<endl;
else
{
for(int x=0; x<maxSize-1; x++)
{
if(stackArray[x]==key)
return stackArray[x];
}
}
}
I don't really know what you want to do / ask. From a logical point of view this program will work.
But you got some hops and jumps in there. I will start with a working example and point out the differences.
Note that I won't address the stack logic in itself. It seems like you did start out with a stack like thing, but got driven quite away from it?
You have a (top == -1) query in there, but never ever declared top / assigned a value to it. I assumed that you meant the size of the array (not that this is probably not the best way to calculate the array size, especially as C++ has a vector container anyways).
You iterate over maxSize elements, yet didn't declare maxSize anywhere. This also assumes that your stack is of fixed size and all elements are initiated with a sane value. I am just iterating over the size of the current array right now.
You return the value return stackArray[x];, yet declared your function to have a bool return value - this does no harm in itself, but I went for returning true and false to adhere to the return value.
Try the code online.
#include <iostream>
using namespace std;
int maxSize = 10;
char stackArray[] = {'b', 'a', 'c', 'd'};
//char stackArray[] = {};
bool findData(char key)
{
int top = (sizeof(stackArray)/sizeof(*stackArray));
cout << "Size of array is: " << top << endl;
if(top <= 0)
cout<<"Stack is Empty"<<endl;
else
{
for(int x=0; x<top; x++)
{
if(stackArray[x]==key)
return true;
}
return false;
}
}
int main() {
if(findData('a')) {
cout << "We found the value!" << endl;
} else {
cout << "We didn't find the value!" << endl;
}
}
EDIT
And as I couldn't resist I jump started with an array based stack architecture from here and implemented our beloved findValue within this stack.
#include <iostream>
#include <string>
using namespace std;
class Stack {
private:
int top;
int capacity;
int *storage;
public:
Stack(int capacity) {
if (capacity <= 0)
throw string("Stack's capacity must be positive");
storage = new int[capacity];
this->capacity = capacity;
top = -1;
}
void push(int value) {
if (top == capacity)
throw string("Stack's underlying storage is overflow");
top++;
storage[top] = value;
}
int peek() {
if (top == -1)
throw string("Stack is empty");
return storage[top];
}
void pop() {
if (top == -1)
throw string("Stack is empty");
top--;
}
/* Our findValue function. */
bool findValue(int key) {
/* Now the usage of top makes perfect sense,
the counter variable always contains the amount
of stored elements. */
if (top == -1) {
throw string("Stack is empty");
}
/* We traverse all elements of our storage. */
for (int i = 0; i < top; i++) {
if(storage[i] == key) {
return true;
}
}
return false;
}
bool isEmpty() {
return (top == -1);
}
~Stack() {
delete[] storage;
}
};
int main() {
/* Init the new stack. */
Stack* stack = new Stack(10);
/* Push some elements for more fun. */
stack->push('a');
stack->push('b');
stack->push('c');
stack->push('d');
stack->push('e');
stack->push('f');
/* Show charcode of last pushed element. */
cout << stack->peek() << endl;
/* Find 'e'. */
if (stack->findValue('e')) {
cout << "We found the value" << endl;
}
/* Try and not find 'g'. */
if (!(stack->findValue('g'))) {
cout << "We didn't find the value" << endl;
}
delete stack;
return 0;
}
I am trying to directly access integer from a pointer class, by overloading * operator, but it seems VC++ 10 is not allowing it. Kindly help:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int MAX7 = 10;
struct node{
int value;
node *next;
};
struct node *head = NULL;
struct node *current = NULL;
int count = 0;
class SmartPointer{
public:
SmartPointer(){
}
int push(int i){
if(count == MAX7) return 0;
if(head == NULL){
head = new node();
current = head;
head -> next = NULL;
head -> value = i;
count = 1;
}
else{
struct node *ptr = head;
while(ptr->next != NULL) ptr = ptr->next;
ptr->next = new node;
ptr = ptr->next;
ptr->next = NULL;
ptr->value = i;
count++;
}
return 1;
}
void Display(){
node *ptr = head;
while(ptr != NULL){
cout << ptr->value << "(" << ptr << ")";
if( ptr == current )
cout << "*";
cout << ", ";
ptr = ptr->next;
}
}
int operator *(){
if(current == NULL) return -1;
struct node *ptr = current;
return ptr->value;
}
};
int main(){
SmartPointer *sp;
sp = new SmartPointer();
sp->push(99);
for(int i=100; i<120; i++){
if(sp->push(i))
cout << "\nPushing ("<<i<<"): Successful!";
else
cout << "\nPushing ("<<i<<"): Failed!";
}
cout << "\n";
sp->Display();
int i = *sp;
getch();
return 0;
}
Error#
1>test7.cpp(71): error C2440: 'initializing' : cannot convert from 'SmartPointer' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
sp is not a smart pointer - it's a plain old dumb pointer to SmartPointer class. *sp uses built-in dereference operator, producing an lvalue of SmartPointer type. It does not call SmartPointer::operator*() - for that, you need to write **sp (two stars).
It's not at all clear why you want to allocate SmartPointer instance on the heap. That's an unusual thing to want to do (also too, you leak it). I'm pretty sure you would be better off with
SmartPointer sp;
sp.push(99);
and so on.
short answer:
int i = **sp;
You should not allocate objects with new. Your code looks like java. In C++, you must delete everything you allocate with new. In C++ you can write:
SmartPointer sp;
sp.push(99);
int i = *sp;
I'm having a slight problem with my code here. I just made a small little program to test to put into my actual program. The idea is to replace all the "(star)here(star)" with the string in the add variable. However, when I run the program my final answer is not exactly correct. These are my results:
I_have_a_star_which is super pretty
I_have_a_star_which is super pretty._That_is_great!_I_also_have_a_girlfriendwhich is super pretty
I_have_a_star_which is super pretty._That_is_great!_I_also_have_a_girlfriendwhich is super prett!
Any ideas on what the problem could be would be much appreciated.
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main ( )
{
char array[500] = "I_have_a_star_*here*._That_is_great!_I_also_have_a_girlfriend_*here*!",
temp[500],
add[500] = "which is super pretty";
int i=0, j=0;
while(array[i] != '\0')
{
if(array[i] != '*')
{
temp[j]=array[i];
i++;
j++;
}
else
{
strcat(temp,add);
cout << temp << endl;
i+=6;
j+=strlen(add);
}
}
cout << temp << endl;
return 0;
}
The problem is that you're copying characters into the temp array without initializing it or ever NUL terminating it. So when you call strcat or try to print the contents of temp extra garbage on the end may screw things up. Stick memset(temp, 0, sizeof(temp));
before your while loop or change the declaration to temp[500] = "". Alternately, you could add temp[j] = '\0'; just before the call to strcat and just after then loop.
Dude try this...
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main ( )
{
char array[500] = "I_have_a_star_*here*._That_is_great!_I_also_have_a_girlfriend_*here*!",
temp[500],
add[500] = "which is super pretty";
int i=0, j=0;
while(array[i] != '\0')
{
if(array[i] != '*')
{
temp[j]=array[i];
i++;
j++;
}
else
{
strcat(temp,add);
i+=6;
j+=strlen(add);
}
}
cout << temp << endl;
return 0;
}