Memory allocation problem

Discussion in 'C++' started by aki, Jul 30, 2008.

  1. aki

    aki Guest

    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
     
    aki, Jul 30, 2008
    #1
    1. Advertisements

  2. 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
     
    Michael DOUBEZ, Jul 30, 2008
    #2
    1. Advertisements

  3. 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=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.

    --
    Erik Wikström
     
    Erik Wikström, Jul 30, 2008
    #3
  4. aki

    James Kanze Guest

    On Jul 30, 1:48 pm, Erik Wikström <> 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:
    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
     
    James Kanze, Jul 30, 2008
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    806
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    643
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,996
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,048
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,246
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page