Are you also unfamiliar with a complete program?
Obviously your program is not complete since it has no header includes
and it says "Node" in main, which of course doesn't exist since you called your struct "Nod".
foo.cpp:22:22: runtime error: member access within null pointer of type 'Nod<Node *>'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior foo.cpp:22:22 in
foo.cpp:22:22: runtime error: store to null pointer of type 'Node *'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior foo.cpp:22:22 in
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==10579==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55efdb8daf00 bp 0x7ffc213993b0 sp 0x7ffc21399330 T10579)
==10579==The signal is caused by a WRITE memory access.
==10579==Hint: address points to the zero page.
#0 0x55efdb8daf00 (a.out+0x2ef00)
#1 0x55efdb8da3cd (a.out+0x2e3cd)
#2 0x7f262040fb24 (/usr/lib/libc.so.6+0x27b24)
#3 0x55efdb8b236d (a.out+0x636d)
UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV (a.out+0x2ef00)
==10579==ABORTING
#include <iostream>
#include <string> //string for std::string (no .h)
usingnamespace std;
/* ¿what's the purpose of this class?
struct Node{
char data;
Node* link = nullptr;
Node(char d){data=d;}
};
*/
template<typename T>
struct Nod{
T data;
Nod *link = nullptr;
Nod(T y){data=y;}
};
template<class Tx>
class ModStack{
private:
Nod<Tx> *top = nullptr;
public:
void Push(Tx x){
if(!top){
//top->data = x; //top is null, you can't dereference it
top = new Nod(x); //allocate memory for it
top->link = nullptr; //now it is valid and can be dereferenced
return;
}
Nod<Tx> *n = new Nod<Tx>(x);
n->link = top;
top = n;
}
void Pop(){
if(!top){cout<<"Stack is empty!"<<endl;return;}
top = top->link; //memory leak
}
Tx Top(){
if(!top){ //¡indent!
cout<<"Stack is empty!"<<endl;
return NULL; //¿what if NULL is not a proper Tx?
}
return top->data;
}
bool IsEmpty(){
return not top; //be concise
/*
if(!top)
return true;
return false;
*/
}
/* No appropriate destructor (leak)
* No appropriate copy-constructor nor assignment operator
*/
};
/* this seems to implement a global queue
Node *head = nullptr;
void Insert(char a){
Node* to_insert = new Node(a);
if(!head){
head = to_insert;
return;
}else{
Node* t = head;
while(t->link){
t = t->link;
}
t->link = to_insert;
}
}
*/
int // main must return int
main(){
string b = "tiresome12";
/* Create an object, not a pointer, no need for new
* It stores chars (or that's what the user knows)
*/
ModStack<char> stck;
for(int i=0;i<10;i++){
stck.Push(b[i]);
}
while(not stck.IsEmpty()){
cout<<stck.Top()<<endl;
stck.Pop();
}
}
ne555, Use case was to use the head for as the type for template class. And creating a general Stack of Objects.
Although, Can you explain the part of memory leak.
Rightly used C++ doesn't produce garbage so there is no need for garbage collection.
The problem is that too many old books and crappy courses teach very C-ish old stuff.
> Use case was to use the head for as the type for template class.
¿ah?
> And creating a general Stack of Objects.
it is general
ModStack<char> stores characters
ModStack<int> stores numbers
ModStack<peanuts> stores peanuts
@seeplus:
1 2 3 4 5 6 7 8
void Pop() {
if (!top) {
std::cout << "Stack is empty!\n";
return;
}
top = top->link;
}
I see that ok, But destructors are really necessary?
And deleting the node from the memory too, Cause it might effect the memory space if large number of entries are to be inserted.