Creating realloc in C++

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 9487 Views - Last Post: 28 July 2010 - 07:30 AM Rate Topic: -----

#1 Fib   User is offline

  • D.I.C Addict
  • member icon

Reputation: 163
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Creating realloc in C++

Posted 08 July 2010 - 02:55 PM

I was trying to find a way to create a dynamic array that was able to have its size changed. I know that realloc does this, but it's only for C. I searched the internet and found some code to implement this in C++.

Here is the code I wrote based off of some examples I found from google.
void Realloc(int *ptr, int prevSize, int newSize)
{
    // Create a temporary array with the new size
    int *temp = new int[newSize];
    
    // Loop through the old array and set the values to the new array
    for (i = 0; i < prevSize; i++)
        temp[i] = ptr[i];
    
    // Unallocate the old array    
    delete[] ptr;
    
    // Set the old array equal to the new array with the new size.
    ptr = temp;
}



Now I'm not very experienced when it comes to memory management. I was wondering if this was a good way to accomplish this? Is there a safer/better/more efficient way I should be doing this?

Thanks.

This post has been edited by Fib: 08 July 2010 - 02:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating realloc in C++

#2 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Creating realloc in C++

Posted 08 July 2010 - 03:15 PM

see:

std::vector
std::string
std::list

there are tons of C++ containers, vectors are pretty general purpose, and strings will likely do what you want.

http://www.cplusplus.../vector/vector/
http://www.cplusplus.../string/string/


here's a list of the STL containers:
http://www.cplusplus.../reference/stl/

This post has been edited by taylorc8: 08 July 2010 - 03:17 PM

Was This Post Helpful? 4
  • +
  • -

#3 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Creating realloc in C++

Posted 08 July 2010 - 03:17 PM

No, it's not a good way. One just uses a standard container in C++. In your case, that would be using a vector and resizing the vector as needed.

As for your function, it does not do what you think it does, and in fact results in at least a memory leak.
Was This Post Helpful? 3
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Creating realloc in C++

Posted 08 July 2010 - 03:26 PM

Plus, if one were to mimic realloc() it should return a pointer to the new allocation.
Was This Post Helpful? 1
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Creating realloc in C++

Posted 08 July 2010 - 03:51 PM

The big problem with writing your own realloc function is that (unfortunately) the standard does not make the size of the currently allocated block available to you. That is you have no (standard) way of knowing what the current size of an allocated block of memory is, so it is difficult to copy that array.

The obvious way is to pass in the old size:
#include <cstring>
#include <algorithm>


template<typename T>
T*& myRealloc(T*& ptr, const size_t& prevSize, const size_t& size) {
    T* temp = new T[size];
    std::memcpy(temp, ptr, sizeof(ptr[0]) * std::min(prevSize, size));
    std::swap(temp, ptr);
    delete[] temp;
    return ptr;
}


#include <iostream>

using namespace std;

int main() {
    int *myArray = new int[10];
    for (size_t i = 0; i < 10; ++i) {
        myArray[i] = i;
    }
    cout << "Address of myArray = " << myArray << endl;
    myRealloc(myArray, 10, 20);
    cout << "Address of myArray = " << myArray << endl;
    
    for (size_t i = 0; i < 10; ++i) {
        myArray[i+10] = i+10;
    }
    
    for (size_t i = 0; i < 20; ++i) {
        cout << myArray[i] << ", ";
    }
    
    delete[] myArray;
    
    return 0;
}


A less obvious way is to create your own allocator which keeps track of the memory it allocates and then can of course properly reallocate without you having to spend a lot of time keeping track of array sizes. This is also helpful if you ever need to know what the current size of some array is! So this has its benefits.

** note on my code above -- this is ok for arrays of prmitive types -- but that memcpy() will cause troubles if you try this will objects with deconstructors!
Was This Post Helpful? 3
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7495
  • View blog
  • Posts: 15,531
  • Joined: 16-October 07

Re: Creating realloc in C++

Posted 08 July 2010 - 04:31 PM

Code looks ok, but passing locally created pointers about it a great way to get a memory leak. Nick's memcpy version is potentially more efficient, but is subject to the same problem.

I'd probably wrap the thing in a class.
#include <iostream>

using namespace std;

struct ArrayEx {
	int size, maxSize, *data;
	ArrayEx(int sz = 5) : size(0), maxSize(sz) { data = new int[maxSize]; }
	~ArrayEx() { delete [] data; }
	
	int &operator[](int index) { return data[index]; }

	void realloc(int newSize) {
		cout << "grow" << endl;
		maxSize = newSize;
		int *temp = data;
		data = new int[maxSize];
		for (int i = 0; i<size; i++) { data[i] = temp[i]; }
		delete[] temp;
	}
	
	void add(int n) { 
		data[size++] = n;
		if (size>=maxSize) { realloc(size + 5); }
	}
};


int main() {
	ArrayEx a;
	for (int i = 0; i < 12; i++) {
		a.add(i);
	}
	
	a[7] = 42;
	
	for (int i=0; i<a.size; i++) {
		cout << a[i] << ", ";
	}
	cout << endl;
	return 0;
}



This is a quick proof of concept. There a few more tweaks you could do. It does avoid memory leaks, though.
Was This Post Helpful? 4
  • +
  • -

#7 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Creating realloc in C++

Posted 08 July 2010 - 05:41 PM

*
POPULAR

Well I have created an improved version of my little function above. This one will handle objects (so long as they are properly defined and have an correct assignment operator). I wrapped everything up into a nice little namespace called MemoryUtil so that none of the utility functions are likely to clash (unless you "using namespace MemoryUtil" which kind of defeats the purpose).


Note that to help test I imported my Concatenate to String to help test the function with objects that require copying (the program above would have failed with strings because they can't be copied with memcpy).

#include <cstring>
#include <algorithm>

namespace MemoryUtil {
    //Keep track of different types of objects
    enum ObjectTypes {
       OBJECT_TYPE_OBJECT,      // will be copied individually
       OBJECT_TYPE_PRIMATIVE,   // will be copied with memcpy()
       OBJECT_TYPE_POINTER,     // will be copied with memcpy()
    };

    //Use template specialization to differentiate between types
    template<typename T> struct TypenameType        { enum { value = OBJECT_TYPE_OBJECT, }; };
    template<> struct TypenameType<char>            { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<unsigned char>   { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<short>           { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<unsigned short>  { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<int>             { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<unsigned int>    { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<long>            { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<unsigned long>   { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<long long>       { enum { value = OBJECT_TYPE_PRIMATIVE, }; };
    template<> struct TypenameType<unsigned long long> { enum { value = OBJECT_TYPE_PRIMATIVE, }; };

    template<typename T> struct TypenameType<T*> { enum { value = OBJECT_TYPE_POINTER, }; };

    //Special template to determine which type of copy gets preformed.
    template<class T, int TYPE>
    struct MyCopyBlock {
        static inline void Copy (T*& dest, T*& src, const size_t& size) {
            std::memcpy(dest, src, sizeof(src[0]) * size);
        }
    };
        
    template<class T>
    struct MyCopyBlock<T, 0> {
        static inline void Copy(T*& dest, T*& src, const size_t& size) {
            //cout << "***" << endl; // test to ensure this is only called for the string
            for(size_t i = 0; i < size; ++i) {
                dest[i] = src[i]; // use assignment operator
            }
        }
    };


    //The realloc function, the second line preforms the copy.
    template<typename T>
    T*& realloc(T*& ptr, const size_t& prevSize, const size_t& size) {
        T* temp = new T[size];
        MyCopyBlock<T>::Copy< TypenameType<T>::value >(temp, ptr, std::min(prevSize, size));
        std::swap(temp, ptr);
        delete[] temp;
        return ptr;
    }

}

// Include a snippet to help build string objects...
//-- Begin snippet: strcat.h ----------------
#ifndef STRCAT_HPP
#define STRCAT_HPP
#include <sstream>

template <class T>
std::string &operator<<(std::string &str, const T &obj) {
    std::ostringstream oss;
    oss << obj;
    str += oss.str();
    return str;
}
#endif
//-- End snippet: strcat.h ------------------

#include <iostream>
#include <string>

using namespace std;
namespace mem = MemoryUtil;

//construct an output line for displayig the address of a pointer.
#define DISPLAY_ADDRESS(var) cout << "Address of " #var " = " << var << endl

int main() {
    int *myArray = new int[10];
    string* strs = new string[10];
    
    for (size_t i = 0; i < 10; ++i) {
        myArray[i] = i;
        strs[i] << "\"" << i <<"\", "; 
    }
    
    DISPLAY_ADDRESS(myArray);
    mem::realloc(myArray, 10, 20);
    DISPLAY_ADDRESS(myArray);

    DISPLAY_ADDRESS(strs);
    mem::realloc(strs, 10, 20);
    DISPLAY_ADDRESS(strs);
    
    for (size_t i = 0; i < 10; ++i) {
        myArray[i+10] = i+10;
        strs[i+10] << "\"" << i+10 <<"\", "; 
    }
    
    for (size_t i = 0; i < 20; ++i) {
        cout << myArray[i] << ", ";
    }
    cout << endl;
    for (size_t i = 0; i < 20; ++i) {
        cout << strs[i];
    }
    
    delete[] myArray;
    delete[] strs;
    return 0;
}




Note that it is BEST to use the advice of taylorc8 and use standard containers (which can dynamically resize themselves) or make your own vector class as baavgai suggested. If you insist upon playing with raw arrays then my little utility should work though.

<edit> besides... the above only seems to work in MSVS2010... back to the drawing board</edit> ** fixed
Was This Post Helpful? 6
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Creating realloc in C++

Posted 08 July 2010 - 09:27 PM

Was the namespace assignment of 'men' to MemoryUtil strictly because mem is shorter? Or is there a benefit I'm unaware of?
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7495
  • View blog
  • Posts: 15,531
  • Joined: 16-October 07

Re: Creating realloc in C++

Posted 09 July 2010 - 04:46 AM

View PostNickDMax, on 08 July 2010 - 06:41 PM, said:

Well I have created an improved version of my little function above.


Wow, and I thought I'd overdone it. :P

For the sake of silliness, if you're going to use a proper class, using realloc can be transparent to the user. This class is a very simple linked list that should play like an array. There is no limit to the size of the array, but it's not smart enough unallocate space. It's dumb, but it works.

#include <iostream>

using namespace std;

template<typename T, T defaultValue>
class Array {
private:
	struct Node {
		unsigned int index;
		T data;
		struct Node *next;
		Node(int i, struct Node *n) : index(i), next(n), data(defaultValue) {}
	} *head;
	int maxIndex;
public:
	Array() : head(NULL), maxIndex(-1) { }
	
	~Array() { 
		while(head!=NULL) {
			Node *p = head;
			head = head->next;
			delete p;
		}
	}
	
	T &operator[](int index) {
		Node *found = NULL;
		if (maxIndex<index) { 
			maxIndex=index;
		} else {
			found = head;
			while(found!=NULL && found->index!=index) { found = found->next; }
		}
		if (found!=NULL) { return found->data; }
		head = new Node(index, head);
		return head->data;
	}
	
	int size() const { return maxIndex+1; }
};


int main() {
	Array<int, 0> a;
	for (int i = 0; i < 12; i++) {
		a[i] = i;
	}
	
	a[7] = 42;
	a[22] = 10;
	
	cout << a.size() << endl;
	
	for (int i=0; i<a.size(); i++) {
		cout << a[i] << ", ";
	}
	cout << endl;
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#10 Fib   User is offline

  • D.I.C Addict
  • member icon

Reputation: 163
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Creating realloc in C++

Posted 09 July 2010 - 06:36 AM

Thank you everyone for your expertise. You people are EXTREMELY helpful!

I did in fact know about the STL containers, but in an attempt to understand memory allocation/deallocation I was trying to create my own version of the STL stack class. Which this obviously shows that I don't understand much.

I'm definitely going to try out all of the code you awesome people gave me.

Although this does bring up a few questions though.

Quote

As for your function, it does not do what you think it does, and in fact results in at least a memory leak.

How does my code result in a memory leak?

Quote

The big problem with writing your own realloc function is that (unfortunately) the standard does not make the size of the currently allocated block available to you. That is you have no (standard) way of knowing what the current size of an allocated block of memory is, so it is difficult to copy that array.

So are you saying that when you use the "new" operator, you can't know for sure the size of the memory being allocated?
Was This Post Helpful? 0
  • +
  • -

#11 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Creating realloc in C++

Posted 09 July 2010 - 06:45 AM

View PostFib, on 09 July 2010 - 06:36 AM, said:

Quote

As for your function, it does not do what you think it does, and in fact results in at least a memory leak.

How does my code result in a memory leak?


You passed a pointer as the first parameter. You can access what it points at by reference, but the pointer itself is passed by value. Thus, barring any of the very nice solutions above, returning a handle to the new memory is the way to go.


void reAlloc(int* ptr, int oldSize, int newSize){
	int* temp = new int[newSize];

	for(int i = 0; i < oldSize; i++){
		temp[i] = ptr[i];
	}
	delete[] ptr; 
	//memory leak here when you return ptr will not point at 'temp'
	//'temp' goes out of scope
	ptr = temp; 
}



View PostFib, on 09 July 2010 - 06:36 AM, said:

So are you saying that when you use the "new" operator, you can't know for sure the size of the memory being allocated?


He means that you can't request the array to tell you how big it is (like in Java). You could do sizeof() and things of a similar ilk, but there isn't one "standard" way to do it, i.e. it would vary by platform.
Was This Post Helpful? 1
  • +
  • -

#12 Fib   User is offline

  • D.I.C Addict
  • member icon

Reputation: 163
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Creating realloc in C++

Posted 09 July 2010 - 06:48 AM

View PostKYA, on 09 July 2010 - 06:45 AM, said:

View PostFib, on 09 July 2010 - 06:36 AM, said:

Quote

As for your function, it does not do what you think it does, and in fact results in at least a memory leak.

How does my code result in a memory leak?


You passed a pointer as the first parameter. You can access what it points at by reference, but the pointer itself is passed by value. Thus, barring any of the very nice solutions above, returning a handle to the new memory is the way to go.


void reAlloc(int* ptr, int oldSize, int newSize){
	int* temp = new int[newSize];

	for(int i = 0; i < oldSize; i++){
		temp[i] = ptr[i];
	}
	delete[] ptr; 
	//memory leak here when you return ptr will not point at 'temp'
	//'temp' goes out of scope
	ptr = temp; 
}



View PostFib, on 09 July 2010 - 06:36 AM, said:

So are you saying that when you use the "new" operator, you can't know for sure the size of the memory being allocated?


He means that you can't request the array to tell you how big it is (like in Java). You could do sizeof() and things of a similar ilk, but there isn't one "standard" way to do it, i.e. it would vary by platform.


Now I understand. Thank you very much.

This post has been edited by Fib: 09 July 2010 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#13 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Creating realloc in C++

Posted 09 July 2010 - 11:14 AM

You can't really get the size of an array allocated with new[] -- sizeof() will only give you the size of the pointer. -- There does not exist any standard way to get that size (note you can *hack* it out for a particular implementation since ofcourse this information is stored by the allocator).


Fib, when you create an STL container, keep track of the size yourself, then you can reallocate as you need to. I *highly* recommend reading Alexander Stepanov's (the principal STL designer) note given here if you are interested in creating an STL like container.

I also highly recommend downloading and looking over an STL implementation (note your compiler actually has one you can look over but I found the Apache STDXX and SGI ones a little more informative). I have only been though vector, set, and map, but I have found it to be a tremendous learning experience.
Was This Post Helpful? 2
  • +
  • -

#14 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Creating realloc in C++

Posted 09 July 2010 - 11:20 AM

The way malloc tracks the size is with a hash or a table that it maps memory addresses to sizes. When you go to realloc or anything like that, it simply looks it up in a table to figure out how big it was originally.

The problem with realloc is that it can be incredibly slow for large amounts of memory, and there's no guarantee you'll get the memory you need.
Was This Post Helpful? 1
  • +
  • -

#15 Fib   User is offline

  • D.I.C Addict
  • member icon

Reputation: 163
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: Creating realloc in C++

Posted 09 July 2010 - 12:46 PM

Thanks everyone for the replies. I thought that I would post the code that I wrote for a templated stack.

stack.h
#ifndef STACK_H
#define STACK_H

//-----------------------------------------------------------------------------
// Classes contained in this header file
//-----------------------------------------------------------------------------
template <class T>
class stack;


//-----------------------------------------------------------------------------
// Name: class stack
// Desc: A templated container for holding data within a stack data structure.
//-----------------------------------------------------------------------------
template <class T>
class stack
{
private:
    T *data;
    int topOfStack;

public:
    stack();
    ~stack();

     int push(T);
       T pop();
       T top();
     int clear();
     int size() const;
    bool empty() const;
};



//-----------------------------------------------------------------------------
// Name: stack::stack()
// Desc: The stack constructor.  Initializes an empty stack.
//-----------------------------------------------------------------------------
template <class T>
stack<T>::stack()
{
    topOfStack = -1;
    data = new T[1];
}


//-----------------------------------------------------------------------------
// Name: stack::push()
// Desc: Adds a data element on top of the stack.  The stack grows when needed.
//-----------------------------------------------------------------------------
template <class T>
int stack<T>::push(T value)
{
    topOfStack++;

    T *temp = data;

    data = new T[topOfStack + 2];
    for (int i = 0; i <= topOfStack; i++)
        data[i] = temp[i];

    delete [] temp;

    data[topOfStack] = value;

    return 0;

}


//-----------------------------------------------------------------------------
// Name: stack::pop()
// Desc: Removes a data element from the top of the stack.
//-----------------------------------------------------------------------------
template <class T>
T stack<T>::pop()
{
    if (topOfStack > -1)
    {
        T poppedData = data[topOfStack];
        data[topOfStack] = NULL;
        topOfStack--;

        return poppedData;
    }
    else
        return NULL;
}


//-----------------------------------------------------------------------------
// Name: stack::top()
// Desc: Returns the data element that is on top of the stack.
//-----------------------------------------------------------------------------
template <class T>
T stack<T>::top()
{
    if (topOfStack > -1)
        return data[topOfStack];
    else
        return NULL;
}


//-----------------------------------------------------------------------------
// Name: stack::clear()
// Desc: Removes all of the data elements in the stack.  The stack will be
//       considered empty after performing this operation.
//-----------------------------------------------------------------------------
template <class T>
int stack<T>::clear()
{
    if (topOfStack > -1)
    {
        for (int i = 0;i <= topOfStack; i++)
            data[i] = NULL;

        topOfStack = -1;
    }
    else
        return -1;
}


//-----------------------------------------------------------------------------
// Name: stack::size()
// Desc: Returns the number of elements that are in the stack.
//-----------------------------------------------------------------------------
template <class T>
int stack<T>::size() const
{
    return (topOfStack + 1);
}


//-----------------------------------------------------------------------------
// Name: stack::empty()
// Desc: Returns true if the stack is empty.  False if it's not.
//-----------------------------------------------------------------------------
template <class T>
bool stack<T>::empty() const
{
    if (topOfStack <= -1)
        return true;
    else
        return false;
}


//-----------------------------------------------------------------------------
// Name: stack::~stack()
// Desc: The stack deconstructor.  Unallocates the memory the stack held.
//-----------------------------------------------------------------------------
template <class T>
stack<T>::~stack()
{
    delete[] data;
}

#endif



Example use
#include <iostream>
#include "stack.h"

using namespace std;

int main()
{
    stack<char*> myStack;

    myStack.push("Ace of Spades");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("2 of Spades");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("King of Hearts");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("4 of Hearts");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("8 of Clubs");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("5 of Clubs");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("Queen of Clubs");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("Queen of Diamonds");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("9 of Diamonds");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("Ace of Diamonds ");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("8 of Clubs");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("5 of Spades");
    cout << myStack.top() << " was added to the deck" << endl;

    myStack.push("6 of Spades");
    cout << myStack.top() << " was added to the deck" << endl;

    cout << "\nThere are " << myStack.size() << " cards in the deck.\n" << endl;

    while (! myStack.empty())
    {
        cout << myStack.pop() << " was drawn from the deck" << endl;
    }

    return 0;
}



Output
Ace of Spades was added to the deck
2 of Spades was added to the deck
King of Hearts was added to the deck
4 of Hearts was added to the deck
8 of Clubs was added to the deck
5 of Clubs was added to the deck
Queen of Clubs was added to the deck
Queen of Diamonds was added to the deck
9 of Diamonds was added to the deck
Ace of Diamonds  was added to the deck
8 of Clubs was added to the deck
5 of Spades was added to the deck
6 of Spades was added to the deck

There are 13 cards in the deck.

6 of Spades was drawn from the deck
5 of Spades was drawn from the deck
8 of Clubs was drawn from the deck
Ace of Diamonds  was drawn from the deck
9 of Diamonds was drawn from the deck
Queen of Diamonds was drawn from the deck
Queen of Clubs was drawn from the deck
5 of Clubs was drawn from the deck
8 of Clubs was drawn from the deck
4 of Hearts was drawn from the deck
King of Hearts was drawn from the deck
2 of Spades was drawn from the deck
Ace of Spades was drawn from the deck



The push function is where I was trying to reallocate memory to allow the stack to grow. I'm not sure if I did it right because there may be memory leaks or something I don't even know about. It seems to work fine though.

This post has been edited by Fib: 09 July 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2