Page 19 of 20

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 10:02 am
by Matthew

Code: Select all

cout << "\n\nreturn_array() Test: newVec length: " << newVec.len() << "\newArray: ";
    char *newArray = new char[newVec.len()]; //Here you allocate memory for a char array of a certain length. newArray us a pointer to this memory
    newArray = newVec.return_array(); //Now you simply assign the array in the vector class to the pointer. The memory it did point to wasn't freed and has therefore been leaked.
    for (int i = 0; i < myVec.len()+1; i++)
        cout << newArray[i] << ", ";

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 2:43 pm
by Scott
Matthew wrote:

Code: Select all

char *newArray = new char[newVec.len()]; //Here you allocate memory for a char array of a certain length. newArray us a pointer to this memory
    newArray = newVec.return_array(); //Now you simply assign the array in the vector class to the pointer. The memory it did point to wasn't freed and has therefore been leaked.
Huh? new is just like malloc. You need to use it before you can point it towards anything.

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 4:41 pm
by Matthew
You need to look at arrays, pointers and memory management all over again.

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 5:17 pm
by Scott
Matthew wrote:You need to look at arrays, pointers and memory management all over again.
I understand arrays and pointers, but like I previously mentioned, I have never done anything with memory management and am in fact, clueless.

Reading over your comments I think I see what you meant. Is this proper:

Code: Select all

cout << "\n\nreturn_array() Test: newVec length: " << newVec.len() << "\nnewArray: ";
char *newArray = newVec.return_array();
:?:

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 5:23 pm
by Matthew
Yes. No allocation necessary as you allocate the array in your class.

When you allocate memory, you must free it at some point. You can only do this if you maintain the pointer to the memory.

With the code you just posted the pointer simply points to an array which is processed with the class. Be warned however because it's not a copy of the array, it's a pointer to the same array. That means any changes with the vector class will effect the array which is returned because it's the same thing. C has a memcpy function for easy copying of memory.

You could allocate memory for a copy in the method, copy the data over and then return the pointer.

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 5:33 pm
by Scott
Matthew wrote:Yes. No allocation necessary as you allocate the array in your class.

When you allocate memory, you must free it at some point. You can only do this if you maintain the pointer to the memory.

With the code you just posted the pointer simply points to an array which is processed with the class. Be warned however because it's not a copy of the array, it's a pointer to the same array. That means any changes with the vector class will effect the array which is returned because it's the same thing. C has a memcpy function for easy copying of memory.

You could allocate memory for a copy in the method, copy the data over and then return the pointer.
Ah, good point.

Code: Select all

T* return_array() //Returns an array if you want local data
        {
            T *tempArray = new T[itsLength];
            for (int i = 0; i < itsLength; i++)
                tempArray[i] = pVector[i];
            return tempArray;
        }
How would I delete tempArray then? Because it is local to the function is it freed by default?

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 6:02 pm
by Matthew
When you allocate memory, it is never freed by garbage collection. You are always responsible for using the delete statement in C++ or the free function in C.

When you return that array, you will need to free the array later on. If documenting your class you should make it clear the user of the class needs to do this. Else you could keep a copy of the pointer in your class and free the last array when you return a new one, or keep them all until the end of the program (Not recommended).

It's up to you as long as everything is freed properly to prevent memory leaks.

Re: C++/C programming discussion

Posted: Fri Oct 22, 2010 9:19 pm
by Scott
Matthew wrote:When you allocate memory, it is never freed by garbage collection. You are always responsible for using the delete statement in C++ or the free function in C.

When you return that array, you will need to free the array later on. If documenting your class you should make it clear the user of the class needs to do this. Else you could keep a copy of the pointer in your class and free the last array when you return a new one, or keep them all until the end of the program (Not recommended).

It's up to you as long as everything is freed properly to prevent memory leaks.
EDITED OUT CODE AS IT DOESN'T WORK YET.

If you have any other ideas for useful member functions or a better name than myVector, I am always glad to hear ideas.

EDIT:

I'm drawing a blank. How do you take an pointer to an array as a parameter and edit it's contents so that you can use the edited version in main?

Re: C++/C programming discussion

Posted: Sat Oct 23, 2010 10:08 am
by Matthew
In C++ I think you can use pass by reference. In C it is done with the usual pointer logic. I'll show you the C way (Code not tested once again so take with a pinch of salt):

Code: Select all

void inc_arr(int * arr,int len){ //Pass integer pointer to first element of array
    for(int x = 0;x < len;x++){
        arr[x]++; //Arrays are pointers to a number of elements of data in memory and the braket notation simply accesses an element so far from where it currently points. The braket notation doesn't modify pointers like pointer arithmetic does. You could instead use pointer arithmetic and dereferencing in this case but the square brakets are easier to understand.
    }
}
int main(){
    int integers[] = {1,2,3,4,5,6}; //integers is a pointer to the first element in this 6 integer array.
    inc_arr(integers,6); //Passing the size is crucial as there is no other method of knowing the size unless you use null terminated arrays instead.
    for(int x = 0;x < 6;x++){
        printf("%i, ",integers[x]);
    }
    return 0;
}

Re: C++/C programming discussion

Posted: Sat Oct 23, 2010 11:54 pm
by Scott
Ah, thanks, that clears things up. It was the declaration of the array pointer (pointer array?) that was throwing me off.

Now, the myVector implementation:

Code: Select all

cout << "\n\nreturn_array(ptr) Test: newVec length: " << newVec.len() << "\nnewArray: ";
    char *newArray = new char[newVec.len()];
    newVec.return_array(newArray);
    for (int i = 0; i < newVec.len(); i++)
        cout << newArray[i] << ", ";
    cout << "\nnewVec: ";
    for (int i = 0; i < newVec.len(); i++)
        cout << newVec[i] << ", ";
    cout << "\nmyVec: ";
    for (int i = 0; i < myVec.len(); i++)
        cout << myVec[i] << ", ";

Code: Select all

void return_array(T *outside_array) //Returns an array if you want local data
        {
            delete outside_array;
            outside_array = new T[itsLength];
            for (int i = 0; i < itsLength; i++)
                outside_array[i] = pVector[i];
        }

Re: C++/C programming discussion

Posted: Sun Oct 24, 2010 10:24 am
by Matthew
A pointer array is an array to pointers like this:

Code: Select all

char ** ptr_arr;
Or with the braket notation if you want to declare data (Static):

Code: Select all

char * ptr_arr[10];

The top example is the same type as the bottom one. The bottom one just declares space for 10 char pointers. The square bracket notation always gives a pointer (Pointer to the first element in the array). This is fine:

Code: Select all

char ** ptr_ptr;
char * ptr_arr[10];
ptr_ptr = ptr_arr;

Re: C++/C programming discussion

Posted: Mon Oct 25, 2010 9:50 pm
by Scott
Fixed up return_array(*ptr) and added replace_array(*ptr) which replaces whatever was in the array with the myVector data.

I also have an official name to replace myVector...drum roll....chamber! So now the way you declare one:

Code: Select all

chamber <typename> identifier;
I can't really think of any other functions and I have finished going back and documenting. I think it's pretty much finished. It was a fun project. Hopefully I can find some use for it...

EDIT:

Added:

Code: Select all

void reverse() //reverses order of the container
        {
            tempChamber = new T[itsLength];

            for (int i = itsLength-1, j = 0; i > -1; i--, j++)
                tempChamber[j] = pChamber[i];

            delete pChamber;
            pChamber = new T[itsLength];

            for (int i = 0; i < itsLength; i++)
                pChamber[i] = tempChamber[i];

            delete tempChamber;
        }
        int search(T term) //returns 1 if term exists
        {
            for (int i = 0; i < itsLength; i++)
            {
                if (pChamber[i] == term)
                    return 1;
            }
            return 0;
        }
        void search_results(T term, chamber<int>& otherChamber) //returns locations of the different terms into a chamber
        {
            for (int i = 0; i < itsLength; i++)
            {
                if (pChamber[i] == term)
                    otherChamber.add_on(i);
            }
        }
        int amount_found(T term) //returns the number of terms found
        {
            int num = 0;

            for (int i = 0; i < itsLength; i++)
            {
                if (pChamber[i] == term)
                    num++;
            }
            return num;
        }
Want to add:

Code: Select all

/*TO-DO: sort(), multi_search(T, T) - several terms, search!(T) - this finds all matches, if_alloted(T) - this returns true if term is there*/

Re: C++/C programming discussion

Posted: Wed Oct 27, 2010 2:30 pm
by Matthew
I see you used by-reference in C++ there.

Maybe you could do a binary search or did I miss that? The binary search algorithm could call the sorting algorithm to ensure the list is sorted.

Re: C++/C programming discussion

Posted: Wed Oct 27, 2010 9:16 pm
by Scott
Matthew wrote:I see you used by-reference in C++ there.
For passing class objects to class methods I typically do :)
Matthew wrote:Maybe you could do a binary search or did I miss that? The binary search algorithm could call the sorting algorithm to ensure the list is sorted.
The sort algorithm is used with iterators and therefore, would be difficult (if even possible at all) to implement. I've decided to use quicksort, but yet again, I am stuck. This is what I have so far:

Code: Select all

void sort()
        {
            int pivot = itsLength/2;
            chamber <T> LESS;
            chamber <T> GREAT;
            std::cout << "\nAfter Declarations\n";

            for (int i = 0; i < itsLength; i++)
            {
                if (pChamber[i] > pChamber[pivot] || pChamber[i] == pChamber[pivot])
                    GREAT.add_on(pChamber[i]);
                else
                    LESS.add_on(pChamber[i]);
            }
            std::cout << "\nAfter for-loop\n";
            while (GREAT.len() > 1 || LESS.len() > 1)
            {
                if (GREAT.len() > 1)
                    GREAT.sort();
                if (LESS.len() > 1)
                    LESS.sort();
            }
            std::cout << "\nAfter while-loop\n";

            if (GREAT.len() == 1 && LESS.len() == 1)
            {
                    add_on(LESS);
                    add_on(GREAT);
            }
            std::cout << "\nAfter add_on(chamber)\n";
        }
Now for the obvious question, do you have any suggestions to how I can get it to work?

Re: C++/C programming discussion

Posted: Thu Oct 28, 2010 6:44 am
by Matthew
I may look later as I'm on my iPhone at the moment. What is T?