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
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?
im thinking of doing something similar, but im doing it wrong, i tried to do the same thing in my class, but i get errorsCode:#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; }
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.
Yes, just use std::vector:
And then fix your indentation.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; }); }
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
here i declare a variable *x which is the dynamic array that i create for the stackCode: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&); };
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
i hope it's correct nowCode: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; }
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.
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
and this would be the codeCode:~stack(); //this would be in stack.h
but doesnt windows free the allocated memory by itself?Code:~stack::stack(){ delete x; }
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
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
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.