Memory allocation problem

A

aki

Hi All ,
I am learning some basic concepts .
i have written a program for stack implementation .
// Progarm start
#include<iostream>
using namespace std;
#include<string.h>
#include <curses.h>
#define SIZE 5
class stack
{
int a[SIZE];
int tos;
public:
stack();
~stack();
void push(int);
int pop();
int isfull();
int isempty();
};
stack::stack()
{
for(int i=0;i<SIZE;i++)
{
a=NULL;
}
tos=0;

}
stack::~stack()
{

}
int stack::isempty()
{

return (tos==0?:1,0);

}
int stack::isfull()
{

return(tos==SIZE?:1,0);

}
void stack::push(int n)
{
if(!isfull())
{
a[tos]=n;
tos++;

}
else
cout<<"stack overflow ..sorry"<<endl;


}

int stack::pop()
{
if(!isempty())
{
return( a[--tos]);

}
else

cout <<"stack underflow...sorry"<<endl;
return 0;
}
int main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Mani Menu"<<endl<<
"1.Push"<<endl<<
"2.Pop"<<endl<<
"3.IsEmpty"<<endl<<
"4.IsFull"<<endl<<
"0.Exit"<<endl;


cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty"):(cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full.");
break;
default:
cout<<"Illegal Option.Please try again"<<endl;
}
}//end of while
//getch();
cin>>ch;
return 0;
}



//Program end
Compilation is ok .
When i am running the program with option 2 (pop ) operation .
As initially no element is in stack it should throw error according
to program
but i am getting return value 2 ..

this i do not understand .
I guess memory handling , i am not doing properly ..

Can somebody look into this

Thanks and regards
Aki

// output start
Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
2
Number popped from the stack is: 2
Stack Operations Mani Menu
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
// output end
 
E

Erik Wikström

Hi All ,
I am learning some basic concepts .
i have written a program for stack implementation .

Since you are still learning I'll give you some comments about the code
in addition to the solution Michael gave.
// Progarm start
#include<iostream>
using namespace std;
#include<string.h>
#include <curses.h>

You do not use either of those headers so you are better of not
including them. And when writing C++ code try to stay away from
<string.h> unless you really have to, C++ provides std::string (you get
without the ".h") which is much better when said:
#define SIZE 5

Try to not use defines for constants, C++ (and C) provides the const
keyword for this purpose. You should probably also make the size of the
stack a const member of the stack-class instead of a public variable.
class stack
{
int a[SIZE];
int tos;
public:
stack();
~stack();
void push(int);
int pop();
int isfull();
int isempty();
};
stack::stack()
{
for(int i=0;i<SIZE;i++)
{
a=NULL;
}
tos=0;

}
stack::~stack()
{

}


If you do not use the destructor there is really no point in declaring
and defining it, though I kind of approve of making it a habit so you do
not forget it when you do need it.
int stack::isempty()
{

return (tos==0?:1,0);

}
int stack::isfull()
{

return(tos==SIZE?:1,0);

}

Is this the code you compiled and ran? Because I can not get it past my
compiler. Since you got it right later in the code I'll just assume that
it was a typo.

Another thing, if you have functions which can only return two possible
values you might want to make them return a bool (true or false) instead.
void stack::push(int n)
{
if(!isfull())
{
a[tos]=n;
tos++;

}
else
cout<<"stack overflow ..sorry"<<endl;

Strictly speaking the stack never did overflow since you never pushed
anything on it, perhaps "stack full... sorry" might be better.
}

int stack::pop()
{
if(!isempty())
{
return( a[--tos]);

}
else

cout <<"stack underflow...sorry"<<endl;
return 0;
}

Here you have a problem with is quite common in stack implementations,
the fact that ever if there is no element to pop the function will
return a value. How can you, without looking at the output, tell if you
just poped the value 0 from the stack or if there was no value to pop?

There are two ways of solving this, one is to throw an exception if
there are not values to pop. The other is to have two functions; top()
which returns the value at the top of the stack, and pop() which removes
the topmost value from the stack, but does not return anything. Of
course this still leaves the problem of what top() should return if the
stack is empty, so you probably want to throw an exception here to.
int main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Mani Menu"<<endl<<
"1.Push"<<endl<<
"2.Pop"<<endl<<
"3.IsEmpty"<<endl<<
"4.IsFull"<<endl<<
"0.Exit"<<endl;


cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty"):(cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full.");
break;
default:
cout<<"Illegal Option.Please try again"<<endl;
}
}//end of while
//getch();
cin>>ch;
return 0;
}

I see that you are quite fond of using the ?: operator, and while there
is nothing wrong with it I think you should only use it in very small
expressions (like in isempty() and isfull()) since it can make the code
harder to read, if-statements will work just as well but is often easier
to read.
 
J

James Kanze

On 2008-07-30 10:51, aki wrote:

[...]
I see that you are quite fond of using the ?: operator, and
while there is nothing wrong with it I think you should only
use it in very small expressions (like in isempty() and
isfull()) since it can make the code harder to read,
if-statements will work just as well but is often easier to
read.

It's a question of spacing. Start nesting ?: on a single line,
and I think everyone will have problems with it. But something
like:

return someCondition()
? ...
: ... ;

for example, shouldn't pose any problems different from those of
an if. And if the function should return a value, it's far
clearer that it always does so if the only statement in the
function is a return, rather than to have a sequence of if/else
if/.../else, with the possibility that a return was forgotten in
one of the branches.

(This works in general. If the main point is that you return,
or assign to x, or whatever, that should appear at the highest
level, with the conditionals expressed using ?:. If the
important point is the decision, and you really do do something
different in the different cases, then you should use if/else.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top