How I free memory and return value from that memory area?

Discussion in 'C++' started by sam, Jun 25, 2003.

  1. sam

    sam Guest

    I am using Sun server which did not have STL. I wrote a small Stack class as
    I listed below. If I remove one item from stack and want to free that
    memory area. I am using "data" member (temp variable) to hold and return
    and deleting original one.
    My question is:
    How I free memory and return value from that memory area? ( scroll down to
    function pop) I did not want to use line 39.
    Please comment also over all program. Thank you very much in advance.

    #include<iostream>
    #include<string>
    #include<stdlib>
    class Stackitem{
    public:
    char *item;
    Stackitem *next;
    };

    class Stack{
    private:
    Stackitem* top;
    char* data; // Temp member
    public:
    Stack();
    void push( char *);
    char* pop();

    };
    Stack::Stack() {

    data = new char[40];
    }
    void Stack::push(char* val1){

    Stackitem *st = new Stackitem;
    st->item = new char[strlen(val1)+1];
    st->item=val1;
    top->next=top;
    top=st;
    }
    char* Stack::pop(){
    Stackitem* t =top;
    top = top->next;
    strcpy(data,t->item); // Line 39 (I did not want to
    use this line. what is the alternative)
    delete t;
    return t->item;

    }
    int main(){
    Stack *s1 = new Stack();
    s1->push("20");
    s1->push("30");
    s1->push("50");
    cout << "pop="<<s1->pop() <<"\n" ;

    Stack s2;
    s2.push("500");
    cout << "pop="<<s2.pop() <<"\n" ;
    delete s1;
    return (0);
    }
    sam, Jun 25, 2003
    #1
    1. Advertising

  2. "sam" <> wrote...
    > I am using Sun server which did not have STL. I wrote a small Stack class

    as
    > I listed below. If I remove one item from stack and want to free that
    > memory area. I am using "data" member (temp variable) to hold and return
    > and deleting original one.


    That's a strange approach. But let's move on...

    > My question is:
    > How I free memory and return value from that memory area? ( scroll down to
    > function pop) I did not want to use line 39.


    I don't think that you have a choice given this design.

    > Please comment also over all program. Thank you very much in advance.
    >
    > #include<iostream>
    > #include<string>


    I don't see you using anything from this header. Why do you need it?
    Did you intend to use 'string' instead of 'char*'? What stopped you?

    > #include<stdlib>


    There is no such header. You probably mean

    #include <stdlib.h>

    > class Stackitem{
    > public:
    > char *item;
    > Stackitem *next;
    > };
    >
    > class Stack{
    > private:
    > Stackitem* top;
    > char* data; // Temp member
    > public:
    > Stack();
    > void push( char *);
    > char* pop();
    >
    > };
    > Stack::Stack() {
    >
    > data = new char[40];


    Are you sure the contents of Stackitem never going to get longer
    than 39 symbols? If they do, you're going to experience the wrath
    of C++ gods...

    > }
    > void Stack::push(char* val1){
    >
    > Stackitem *st = new Stackitem;
    > st->item = new char[strlen(val1)+1];
    > st->item=val1;


    Here you have a memory leak. You allocate something, get
    a value of the pointer into 'st->item', then IMMEDIATELY
    overwrite that value with the parameter. Why did you allocate?

    Did you mean to _copy_ the contents of 'val1' into 'st->item'?
    Then you need to use strcpy.

    And, by the way, shouldn't "Stackitem" do the allocation of
    its members?

    > top->next=top;
    > top=st;


    This is where you lose the 'next' member you just set.

    > }
    > char* Stack::pop(){
    > Stackitem* t =top;
    > top = top->next;
    > strcpy(data,t->item); // Line 39 (I did not want

    to
    > use this line. what is the alternative)


    No alternative in your case.

    > delete t;
    > return t->item;


    You can't do that. 't' doesn't exist after you 'delete' it.

    >
    > }
    > int main(){
    > Stack *s1 = new Stack();
    > s1->push("20");
    > s1->push("30");
    > s1->push("50");
    > cout << "pop="<<s1->pop() <<"\n" ;
    >
    > Stack s2;
    > s2.push("500");
    > cout << "pop="<<s2.pop() <<"\n" ;
    > delete s1;
    > return (0);
    > }


    Awful, overall. You should have used std::string for storage
    and really really really need to pay attention to how you link
    the elements of the stack (if you decided to use a singly-linked
    list for that).

    #include <iostream>
    #include <string>
    using namespace std;

    class Stackitem {
    string data;
    Stackitem *next;
    public:
    Stackitem(const string& d, Stackitem *n = 0)
    : data(d), next(n) {}
    string getData() const { return data; }
    Stackitem* getNext() const { return next; }
    };

    class Stack {
    Stackitem *top;
    int length;
    public:
    Stack() : top(0), length(0) {}
    ~Stack() {
    while (top) {
    Stackitem *p = top->getNext();
    delete top;
    top = p;
    }
    }
    void push(const string& s) {
    top = new Stackitem(s, top);
    }
    string pop() {
    if (!top)
    throw "Attempt to pop an empty stack";
    string s = top->getData();
    Stackitem *n = top->getNext();
    delete top;
    top = n;
    return s;
    }
    };

    int main() {
    Stack s1;
    s1.push("20");
    s1.push("30");
    s1.push("50");
    cout << "pop=" << s1.pop() << endl;

    Stack s2;
    s2.push("500");
    cout << "pop=" << s2.pop() << endl;

    return 0;
    }

    Victor
    Victor Bazarov, Jun 26, 2003
    #2
    1. Advertising

  3. sam

    Dhruv Guest

    On Wed, 25 Jun 2003 22:36:40 +0000, sam wrote:

    > I am using Sun server which did not have STL. I wrote a small Stack class as
    > I listed below. If I remove one item from stack and want to free that
    > memory area. I am using "data" member (temp variable) to hold and return
    > and deleting original one.
    > My question is:
    > How I free memory and return value from that memory area? ( scroll down to
    > function pop) I did not want to use line 39.
    > Please comment also over all program. Thank you very much in advance.
    >

    [major snip here]......

    I agree with what victor said.
    I'd like to emphasize again USE std::string (But you said that the
    std::library was not available for your machine? )

    Just as a thought, you could use boost::shared_ptr<>, and your problems
    with pop will be solved.

    Something like this:

    boost::shared_ptr<type> pop (void)
    {
    boost::shared_ptr<type> temp (actual pointer);
    //do relinking of linked list;
    //do not do delete pointer;
    return temp;
    }

    Then, in the main (calling) code, write:

    shared_ptr<type> t;
    t = stack.pop ();

    And, when t goes out of scope, or is re-assigned, it will self destruct
    (boofff!!!).

    HTH,
    -Dhruv.
    Dhruv, Jun 27, 2003
    #3
    1. Advertising

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. Mark
    Replies:
    1
    Views:
    575
    Larry Barowski
    Jun 27, 2005
  2. Greenhorn
    Replies:
    15
    Views:
    801
    Keith Thompson
    Mar 6, 2005
  3. hotadvice
    Replies:
    14
    Views:
    697
    hotadvice
    Oct 2, 2007
  4. Andy B.
    Replies:
    0
    Views:
    798
    Andy B.
    Apr 30, 2010
  5. Jim
    Replies:
    0
    Views:
    181
Loading...

Share This Page