Thread: C++ stack using class

  1. #16
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Elysia View Post
    I would make pop return the element that is removed and rename top to peek. I don't know why they did it like this in STL, but frankly, it's annoying and peek/pop is pretty much standard anyway.
    And StainedBlue, as we have already tried to explain, the OP should not be using dynamic memory at this point. A stack does not need dynamic memory. A vector or a deque should suffice here. Let's not try to put out different solutions here. It will only serve to confuse.
    Why not just make a vector wrapper, and forget manual memory management all together then?

    EDIT: By that I mean, why does the OP have a pointer at all?
    goto( comeFrom() );

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by StainedBlue View Post
    EDIT: By that I mean, why does the OP have a pointer at all?
    That is what we're trying to remove.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    i dont want to make it using structs, i made it using class, i used pointers that would help to free the stack when i finish and also reallok works like realloc

    about vectors, as I said i know nothing about them

    this is an implementation using no vectors, no structs, just classes, and i still cant understand what im doing wrong

    thanks

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And that is why we're cautioning you to use a vector. Too much complexity otherwise.
    We have already told you that realloc should not be used with new/delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by Elysia View Post
    And that is why we're cautioning you to use a vector. Too much complexity otherwise.
    We have already told you that realloc should not be used with new/delete.
    yes i understand that, but im not using realloc at all, im using reallok

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And what exactly is reallok?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    im trying to reallocate memory for the stack but it seems that im doing it wrong, is this a correct way to reallocate memory for a simple array?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int *reallok(int *x,int &size){
        int *y;
        size = size+2;
        y = new int[size];
        int i;
      //  cout<<x[0]<<x[1]<<endl;
      //  cin.get();
        for (i=0;i<size-2;i++){
            y[i] = x[i];
            }
        for(i;i<size;i++){
                          y[i] = 0;
                          }
        delete x;
        return y;
    }
    
        
    int main(void){
        
        int *a = new int[2];
        int *check;
        int size = 2;
        
        
        a[0] = 0;
        a[1] = 1;
        check = reallok(a,size);
        if(check!=NULL){
                        a = check;
                        cout<<"success"<<endl;
                       cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl; //prints 0 1 0 0
                        }
      
        cin.get();
        
        return 0;
    }
    im thinking of doing something similar, but im doing it wrong, i tried to do the same thing in my class, but i get errors

    i ll create another simple class to see what exactly im doing wrong, but i think that if i achieve this, the whole app will be working correctly, or not?
    Last edited by nik2; 06-05-2010 at 06:58 AM.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, just use std::vector:
    Code:
    #include <vector>
    #include <algorithm>
    
    int main()
    {
        std::vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        std::for_each(v.begin(), v.end(), [](int n) { std::cout << n << std::endl; });
    }
    And then fix your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    thanks for the alternative solution, but sadly i dont know anything about vectors yet & my professor used realloc malloc and free in his program

    i ll be taking exams in 3 days and was just curious how this could be implemented using C++ code without vectors becauwe we havent done them yet

    i think i have it correct now

    let me explain the code in detail

    stack.h has the class

    Code:
    class stack{
          
          private:
                  int *x;
                  int pos,size;
        
          public:
                 stack();
                 stack(int n);
                 stack(stack &other);
                 bool pop(int &element);
                 bool push(int element);
                 bool isempty();
                 int *reallok(int*,int&);
                 
          };
    here i declare a variable *x which is the dynamic array that i create for the stack

    then i have 3 constructors, the first one has no parameters and it creates an array of 10 elements which i can increace later on

    then pop gets the element that is in pos-1 position, push puts a new element on top, isempty checks if the stack is empty

    i forgot about the third constructor which i dont use at all, it just creates a new "copy" stack

    in main.cpp i add 200 elements in the stack, hence i need to have a reallok function which will give 10 more spaces in memory for new elements each time the position gets more than current size-1

    the code for reallok is this

    Code:
    int *stack::reallok(int *x,int &size){
        
        int *y,i;
        size = size+10;
        y = new int[size];
        for(i=0;i<size-10;i++){
                          y[i] = x[i];
                          }
        delete x;
        return y;  
        
    }
    i hope it's correct now

    it returns the address of the first element of the array that was created in the heap

    it has two parameters, *x points is a pointer to the x array of the class, the size is the current size of the stack

    then i create a new pointer y and assign size+10 elements for it using new

    and then i just copy the values from x to y, then delete x and then return y which is the address of the new memory that was allocated

    i hope it's correct now
    Last edited by nik2; 06-05-2010 at 08:32 AM.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code is not correct.
    First, I would increase allocation by 2x or 1.5x. Adding 10 each time will hurt performance.
    I'd also use std::copy(x, x + size, y) instead of your loop (update size AFTER you copy over the elements).
    Finally, you must do delete [] x. If you use new[], then you must use delete [].

    And as for the interface:
    pop return take no parameters and return the element it pops.
    You are missing a peek function which returns the top element.

    After that, you need to fix your indentation and also remember not to remove the names of the parameters in your declarations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by Elysia View Post
    The code is not correct.
    First, I would increase allocation by 2x or 1.5x. Adding 10 each time will hurt performance.
    I'd also use std::copy(x, x + size, y) instead of your loop (update size AFTER you copy over the elements).
    Finally, you must do delete [] x. If you use new[], then you must use delete [].

    And as for the interface:
    pop return take no parameters and return the element it pops.
    You are missing a peek function which returns the top element.

    After that, you need to fix your indentation and also remember not to remove the names of the parameters in your declarations.
    for the delete[] x i guess i should create a destructor right?

    something like this

    Code:
    ~stack(); //this would be in stack.h
    and this would be the code

    Code:
    ~stack::stack(){
       delete x;
    }
    but doesnt windows free the allocated memory by itself?

    about the pop function, i didnt understand clearly what you meant, im using a bool function and pass an int by reference and i change its value, where element = x[pos-1]

    it's pos-1 because when i push something i increase the pos after that

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A destructor runs when the object is destroyed. There is nothing else to that. Yes, your stack should use a destructor to free its memory when destroyed, but that has nothing to do with the delete statement.
    Since you allocate memory with new[], you must free it with delete[]. Simple. What is so difficult in understanding that?

    About pop, I mean it should look like this:
    int pop();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by Elysia View Post
    A destructor runs when the object is destroyed. There is nothing else to that. Yes, your stack should use a destructor to free its memory when destroyed, but that has nothing to do with the delete statement.
    Since you allocate memory with new[], you must free it with delete[]. Simple. What is so difficult in understanding that?

    About pop, I mean it should look like this:
    int pop();
    are you talking about y or x array?

    if i free y then i would lose the elements, hence i wouldnt return anything back to x(from the reallok)

    about the x i agree that i should delete the allocated memory but i cant understand why a simple destructor wouldnt be enough in this case

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are confusing destructors with something. Do you know what they are?
    About x or y--it doesn't matter! Whatever you allocate with new [] MUST be freed with delete [], regardless if it's x, y, z, 123 or !"#.
    Since you allocate memory with new[] and store it in x, then you must use delete[] on x, as well. Just delete won't work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by Elysia View Post
    You are confusing destructors with something. Do you know what they are?
    About x or y--it doesn't matter! Whatever you allocate with new [] MUST be freed with delete [], regardless if it's x, y, z, 123 or !"#.
    Since you allocate memory with new[] and store it in x, then you must use delete[] on x, as well. Just delete won't work.
    oh ok i understand it now

    thanks a lot for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM