push object in the heap using pointer to the static container - c++

class Node_Str{
public:
string name;
string value;
string type;
Node_Str(string name,string value,string type){
name=name;
value=value;
type=type;}};
static stack<Node_Str> s;
void find_token(string input){
int cursor=0;
string current="";
while(cursor<input.length()){
char value;
value=input[cursor];
cout<<value<<endl;
if(value=='('||value==')'||value=='+'||value=='-
'||value=='*'||value=='/'){
Node_Str* p=new Node_Str("pare",string(1,value),"Pare");
s.push(*p);
cursor++;
delete p;}
if(value==' '){
cursor++;
}
if(value=='1'||value=='2'||value=='3'||value=='4'){
Node_Str* p=new Node_Str("num",string(1,value),"Number");
s.push(*p);
cursor++;
delete p;}}}
int main(){
while(!s.empty()){
cout<<s.top().value<<" ";
s.pop(); }
return 0; }
The find_token function should separate the input string by white Space and constructing the Node_Str object with the value of that string. Then in the
main function, I would like to print it. The characters are limit. Just '1', '2','3','4','+,'-','*','/'.
Input is 4 + 4 , output should be 4+4. However, there is no output.

The comments already said about memory leak and forgetting to call find.
In addition to that, stack is a container in which to last to be pushed in would be the first to be popped out. In order to output 0 1 2 3, you would need to push in the stack in the sequence of 3 2 1 0.
Just giving a better version.
#include <iostream>
#include <stack>
using std::cout;
using std::stack;
static stack<int> s;
void find()
{
int* p;
for (int i = 3; i >= 0; i--) {
p = new int(i);
s.push(*p);
delete p; // p itself does not have to be returned so it can be safely deleted here
//This can also ne replaced by directly using s.push(i)
}
}
int main() {
find();
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
return 0;
}

as suggested by leyanpan, data should be pushed into reverse order, to get the desired output. One more point is no need of dynamic allocation for int type. It is always better to store non array built in types in stack rather than heap.
Also allocating stack data structure as static variable will extend scope up to program termination. Better to use stack object as a local variable in main and pass it as are reference argument to function find.

Related

Unlimited Object Creation in C++

While learning the dynamic object creation in C++ i have encountered a doubt . Here is my code.
And my question is , when the limiting condition in the loop is same as that of the no of objects created it works fine. But what happens when the loop works for more than the size given , it seems printing the values entered , but we have created only 4 objects and changed the condition of loop to more than 4
#include <iostream>
using namespace std;
class item{
int number;
public:
item(){
cout<<"Constructor"<<endl;
}
~item(){
cout<<"Destructor"<<endl;
}
void get_num(int num){
number = num
};
void show_num(){
cout<<"Number is "<<number<<endl;
}
};
const int size=4;
int main() {
item *itemObj = new item[size];
item *d = itemObj; //copy the address of itemObj inorder to access its member functions later
int tempNum;
for (int i = 0; i < size; ++i) {
cout<<"Enter the Number"<<endl;
cin>>tempNum;
itemObj->get_num(tempNum);
itemObj++;
}
//to print the numbers entered
for (int i = 0; i < size; ++i) {
d->show_data();
d++;
cout<<d<<endl;
}
delete itemObj;
return 0;
}
Your code isn't working fine at all. Because you change the value of the pointer that you requested from the new operator. When you call the delete for the itemObj at the last line, it doesn't have its original value.
So, instead of modifying the itemObj, you should modify the copy of it which is the pointer d here. Therefore, the problem isn't about the iteration amount of the loop. It's actually the violation on the heap memory.
Also, if you're creating a dynamic array, you should call delete [] instead of delete.

While i compile and about to push element I get Segmentation error

While i compile and about to push element I get Segmentation error.
What is segmentation error?Can anyone explain me about such type of error.
Is it related to memory handling?
#include<iostream>
#define MAX 10
using namespace std ;
typedef struct
{
int items[MAX] ;
int top=-1;
}node;
int isFull(node *s){
if (s->top==MAX-1)
{
return 1 ;
}
else{
return 0;
}
}
int isEmpty(node *s){
if (s->top==-1)
{
return 1 ;
}
else{
return 0;
}
}
void push(node *s , int );
void pop(node *s);
void push(node *s , int n ){
if (isFull(s))
{
cout<<"Stack overflow"<<endl;
}
else{
s->items[++(s->top)]=n;
}
}
void pop(node *s){
if(isEmpty(s)){
cout<<"The stack is empty";
}
else{
cout<<"item poppe is "<< s->items[s->top--] <<endl;
}
}
int main(){
int num, choice ;
node *s ;
int flag ;
do{
cout<<"Enter your choice"<<endl;
cout<<"1.Push"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.Exit"<<endl;
cin>>choice;
switch(choice){
case 1 :
cout<<"Enter the number to insert "<<endl;
cin>>num;
push(s,num );
break ;
case 2 :
pop(s);
break ;
default:
cout<<"Error";
break;
}
}
while(flag!=0);
return 0 ;
}
ERROR IS :
Segmentation fault
Program finished with exit code 139
What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?
You define a pointer to a node (actually a complete stack), but you do not create a node object to which this pointer can point to. Hence, you dereference an uninitialized pointer, which yields undefined behaviour (e.g. a segfault).
Instead of
node *s ;
...
push(s,num );
Write
node s ;
...
push(&s,num );
Or
node *s = new node(); // or = malloc(sizeof(node)) in C
...
push(s,num );
...
// once the stack is not used any more:
delete s; // or free(s) in C.
such that you create an actual object, which's address you can pass then.
A segmentation error means you have accessed some area of memory that you shouldn't.
In your case it's because the pointer s is uninitialised.
In this case the right thing to do is not use a pointer for your stack, and to use the address-of operator & to get the pointer you need.
int main(){
...
node s; // not a pointer
...
push(&s, num); // use & operator
...
pop(&s); // use & operator
Pointers never 'magically' point at objects, you have to allocate the objects either by declaring variables or by using new.
You defined a pointer to a node, but you did not create a node object to which this pointer can point to. Hence, you defined as an uninitialized pointer, which yields undefined behaviour (e.g. a segfault).
Again a segmentation error means you have accessed some area of memory that you shouldn't.
In your case it's because the pointer "s" is uninitialised.Here you have to give "&" which is the address of the operator.
You have to edit your code like this.
int main () {
node s;
push (&s, num);
pop (&s);

Stack push using external function

I'm trying to implement stacks using constructors in C++. I'm required to use an external function to push an element on the stack, however, it doesn't seem to work properly. The pushexternal function seems to "enter" the push function, but it doesn't increase the ind value, therefore it doesn't add a new element onto the stack (for example in my code, all pushxternals will try to push a value onto the same index, the last one used by s.push - ind==2). I'm not sure what I'm doing wrong.
Oh, I'm only supposed to modify the class code - the pushexternal and main have to remain unchanged.
#include <iostream>
using namespace std;
class Stack {
public:
int ind;
int * arr;
Stack()
{
arr = new int[25];
ind = -1;
}
~Stack()
{
delete [] arr;
}
void push(int val)
{
arr[++ind] = val;
cout << "Added " << arr[ind] << " to " << ind << endl;
}
void top()
{
cout << "Last: " << arr[ind];
}
};
void pushexternal(Stack s, int a) {
s.push(a);
}
int main() {
Stack s;
s.push(0);
s.push(1);
s.push(2);
pushexternal(s, 3);
pushexternal(s, 4);
pushexternal(s, 5);
return 0;
}
Results:
Added 0 to 0
Added 1 to 1
Added 2 to 2
Added 3 to 3
Added 4 to 3
Added 5 to 3
Top: 2
void pushexternal(Stack s, int a) {
s.push(a);
}
receives a Stack as a parameter, which means it receives an object which is a copy of your object.
You should operate on references, this way you will not send a copy of the object to be manipulated, but the reference of the object, thus the original object will be manipulated.
void pushexternal(Stack s, int a) {
s.push(a);
}
You are passing the Stack object to this function by value, not by reference. This makes a temporary copy of the original Stack object, so the original object remains unchanged. Not to mention that this will result in memory corruption, since the RAII principle has been violated.
Just by the luck of the draw, I guess, your code is not segfaulting.

Trying to calculate ASCII code sum of my "sir" (string) class

#include <iostream>
#include <string.h>
using namespace std;
class sir{
int lung; //sir lenght
char* sirul; //pointer to the first character form sir
public:
sir(const char*);//constructor,for test
~sir();// destructor
char operator[](int index);
int cod();
};
int sir::cod()
{
sir u=*this;
char* s=sirul;
int i,sum,l=lung;
if (lung=0)
cout<<"ASCII code sum is 0";
else
{
for(i=0; i<l; i++)
{
sum=sum+int(s[i]);
}
}
return sum;
}
sir::sir(const char* siroriginal)
{
int lungime=strlen(siroriginal);//string length
lung = lungime+1;
sirul = new char[lung];
strcpy(sirul,siroriginal);
}
sir::~sir(){
delete sirul;
}
char sir::operator[](int index){
if(index<lung && index >=0)return sirul[index];//verificare minima, verific daca este in limite
return '\0';
}
int main(){
cout<<"--------------test for sir-----------------\n";
sir s("un sir meserias");
char c=s[1];
cout<<c<<"\n";
cout<<s.cod();
}
When I'm executing this program an error says that "double free or corruption", I don't understand what causes this error. It appears after I'm trying to calculate ASCII code sum of my sir(string) with cod method, that should return an integer value.
How can I solve the problem?
There are few problems within your code :
1. First :
You have a mismatched new[]/delete call here. Replace delete sirul by delete[] sirul.
2. Second :
Inside sir::cod, you are actually copying your object by doing
sir u=*this;
Since you are not using u at all, you should remove this line. It is the source of your double free. At the end of the cod function, u will be destroyed and your internal pointer sirul will be deleted. But since you haven't defined any copy operator, the compiler will generate it for you and you'll end up sharing a pointer across two sir instances.
When the second instance is destroyed, sirul is deleted again. Resulting in the error.
3. Third :
Next, in cod, you forgot to initialize sum to 0. That's why you have bad results.
4. Fourth :
In cod, you are doing if (sum = 0). You are missing the == here.

Implementation of stack in C++ without using <stack>

I want to make an implementation of stack, I found a working model on the internet, unfortunately it is based on the idea that I know the size of the stack I want to implement right away. What I want to do is be able to add segments to my stack as they are needed, because potential maximum amount of the slots required goes into 10s of thousands and from my understanding making the size set in stone (when all of it is not needed most of the time) is a huge waste of memory and loss of the execution speed of the program. I also do not want to use any complex prewritten functions in my implementation (the functions provided by STL or different libraries such as vector etc.) as I want to understand all of them more by trying to make them myself/with brief help.
struct variabl {
char *given_name;
double value;
};
variabl* variables[50000];
int c = 0;
int end_of_stack = 0;
class Stack
{
private:
int top, length;
char *z;
int index_struc = 0;
public:
Stack(int = 0);
~Stack();
char pop();
void push();
};
Stack::Stack(int size) /*
This is where the problem begins, I want to be able to allocate the size
dynamically.
*/
{
top = -1;
length = size;
z = new char[length];
}
void Stack::push()
{
++top;
z[top] = variables[index_struc]->value;
index_struc++;
}
char Stack::pop()
{
end_of_stack = 0;
if (z == 0 || top == -1)
{
end_of_stack = 1;
return NULL;
}
char top_stack = z[top];
top--;
length--;
return top_stack;
}
Stack::~Stack()
{
delete[] z;
}
I had somewhat of a idea, and tried doing
Stack stackk
//whenever I want to put another thing into stack
stackk.push = new char;
but then I didnt completely understand how will it work for my purpose, I don't think it will be fully accessible with the pop method etc because it will be a set of separate arrays/variables right? I want the implementation to remain reasonably simple so I can understand it.
Change your push function to take a parameter, rather than needing to reference variables.
To handle pushes, start with an initial length of your array z (and change z to a better variable name). When you are pushing a new value, check if the new value will mean that the size of your array is too small (by comparing length and top). If it will exceed the current size, allocate a bigger array and copy the values from z to the new array, free up z, and make z point to the new array.
Here you have a simple implementation without the need of reallocating arrays. It uses the auxiliary class Node, that holds a value, and a pointer to another Node (that is set to NULL to indicate the end of the stack).
main() tests the stack by reading commands of the form
p c: push c to the stack
g: print top of stack and pop
#include <cstdlib>
#include <iostream>
using namespace std;
class Node {
private:
char c;
Node *next;
public:
Node(char cc, Node *nnext){
c = cc;
next = nnext;
}
char getChar(){
return c;
}
Node *getNext(){
return next;
}
~Node(){}
};
class Stack {
private:
Node *start;
public:
Stack(){
start = NULL;
}
void push(char c){
start = new Node(c, start);
}
char pop(){
if(start == NULL){
//Handle error
cerr << "pop on empty stack" << endl;
exit(1);
}
else {
char r = (*start).getChar();
Node* newstart = (*start).getNext();
delete start;
start = newstart;
return r;
}
}
bool empty(){
return start == NULL;
}
};
int main(){
char c, k;
Stack st;
while(cin>>c){
switch(c){
case 'p':
cin >> k;
st.push(k);
break;
case 'g':
cout << st.pop()<<endl;
break;
}
}
return 0;
}