Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory allocation problem

Reply
Thread Tools

Memory allocation problem

 
 
aki
Guest
Posts: n/a
 
      07-30-2008
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[i]=NULL;
}
tos=0;

}
stack::~stack()
{

}
int stack::isempty()
{

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

}
int stack::isfull()
{

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

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

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


}

int stack:op()
{
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

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      07-30-2008
aki a écrit :
[snip]
> int stack::isempty()
> {
>
> return (tos==0?:1,0);
>
> }
> [snip]


You made a typo, should be 'return (tos==0?1:0);'.

Nice bug

--
Michael
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      07-30-2008
On 2008-07-30 10:51, aki wrote:
> 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
it by including <string>, without the ".h") which is much better when
dealing with text.

> #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[i]=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:ush(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:op()
> {
> 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.

--
Erik Wikström
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-30-2008
On Jul 30, 1:48 pm, Erik Wikström <(E-Mail Removed)> wrote:
> 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.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments