Thread: another dynamic memory allocation problem

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    another dynamic memory allocation problem

    the question is;
    Write a program that lets users keep track of the last time they talked to each of their friends. Users should be able to add new friends (as many as they want!) and store the number of days ago that they last talked to each friend. Let users update this value (but don't let them put in bogus numbers like negative values). Make it possible to display the list sorted by the names of the friends of by how recently it was since they talked to each friend.

    Below is what I have done. The problem I am having is that the array will not grow in size. (Another problem which I have not tackled yet is how to remember if the same user updates their noDays figure, but will give that a go after the array grows);

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int *growArray (int * friends, int * size)
    {
        *size *= 2;
        int *new_friends = new int[*size];
        for (int i=0; i < *size; i++) {
            new_friends[i] = friends[i];
        }
        delete [] friends;
        return new_friends;
    }
    
    
    string *growArray2 (string * name1, int * size)
    {
        *size *= 2;
        string *new_name2 = new string[*size];
        for (int i=0; i < *size; i++) {
            new_name2[i] = name1[i];
        }
        delete [] name1;
        return new_name2;
    }
    
    
    void printArray (string *name1, int *friends,int size, int element_set)
    {
        cout << "the total size of the array is: " << size << endl;
        cout << "Values in the array: " << endl;
        for (int i = 0; i < element_set; ++i)
        {
            cout << name1[i] << "[" << i << "] = " << friends[i] << " days since you last spoke to them" << endl;
        }
    }
    
    
    int main()
    {
        string exit = "X";
        string name;
        int noDays;
        int next_element = 0;
        int size = 4;
        int *friends = new int[size];
        string *name1 = new string [size];
            cout << "Please enter your friends name" << endl;
            cin >> name;
            cout << "Please enter no of days you last spoke to them" << endl;
            cin >> noDays;
        while(true) {
            if (size == next_element+1) {
                friends = growArray(friends, &size);
                name1 = growArray2(name1, &size);
            }
    
    
            friends[next_element] = noDays;
            name1[next_element] = name;
            next_element++;
            printArray(name1, friends, size , next_element);
            cout << "Please enter your friends name or X to exit" << endl;
            cin >> name;
            if (name == exit) {
                break;
            }
            cout << "Please enter no of days you last spoke to them" << endl;
            cin >> noDays;
    
    
            }
            delete name1;
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Also I am not supposed to use vectors this is why it is written like this. It is a chapter on dynamic memory allocation and pointers

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > friends = growArray(friends, &size);
    > name1 = growArray2(name1, &size);
    Each growArray function begins with *size *= 2
    It seems to be getting larger than you expect.

    Since you're using C++, replacing all those pointers with references will make the code a lot easier to understand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you considered using a structure or class to hold the friend information instead of the parallel arrays?

    In your growArray() functions you are trying to access your array name1[] out of bounds. You have incremented the size of the array but remember name1[] only has the original size number of elements.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Thanks for the help initially I wrote a different code with a structure but I must have been coding it wrong because it didn't work. I have rectified my code now and change the pointer of "size" to an address. The arrays are growing however it is not printing out the value of the grown array as it would if it were a pointer. Is this correct so far (I still need to sort out if a user puts in multiple values and update them and print them in ascending order) ? I am struggling with this one. Everyone I speak to says use vectors so I may skip ahead to that chapter but want to try at least (also classes have not been covered yet this is why I haven't used any).

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int *growArray (int *friends, int &cur_size)
    {
    
    
        int *new_friends = new int[(cur_size==0 ? 1 : cur_size*2)];
        for (int i=0; i < cur_size; ++i) {
            new_friends[i] = friends[i];
        }
        //*cur_size = (*cur_size==0 ? 1 : *cur_size*2); //added here
        delete []friends;
        return new_friends;
    }
    
    
    string *growArray2 (string *name1, int &cur_size2)
    {
    
    
        string *new_name2 = new string[(cur_size2==0 ? 1 : cur_size2*2)];
        for (int i=0; i < cur_size2; ++i) {
            new_name2[i] = name1[i];
        }
        //*cur_size2 = (*cur_size2==0 ? 1 : *cur_size2*2);
    
    
        delete []name1;
        return new_name2;
    }
    
    
    void printArray (string *name1, int *friends, int &cur_size3, int element_set)
    {
    
    
        cout << "the total size of the array is: " << cur_size3 << endl;
        cout << "Values in the array: " << endl;
        for (int i = 0; i < element_set; ++i)
        {
            cout << name1[i] << "[" << i << "] = " << friends[i] << " days since you last spoke to them" << endl;
        }
    }
    
    
    int main()
    {
        string exit = "X";
        string name;
        int size = 10;
        int noDays;
        int next_element = 0;
        int *friends = new int[size];
        string *name1 = new string [size];
            cout << "Please enter your friends name" << endl;
            cin >> name;
            cout << "Please enter no of days you last spoke to them" << endl;
            cin >> noDays;
        while(true) {
    
    
            if (size==next_element+1) {
    
    
                friends = growArray(friends, size); //changed here
                name1 = growArray2(name1, size); //changed here
    
    
            }
            friends[next_element] = noDays;
            name1[next_element] = name;
            next_element++;
            printArray(name1, friends, size , next_element);
            cout << "Please enter your friends name or X to exit" << endl;
            cin >> name;
            if (name == exit) {
                break;
            }
            cout << "Please enter no of days you last spoke to them" << endl;
            cin >> noDays;
        }
            //printArray(name1, friends, size , next_element);
            return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you set a breakpoint and run this program through your debugger, watching the variables as you single step through the program?

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    No I have not, I have not even been taught debugging yet it is a later chapter in the e-book. I am beginning to think maybe it is time to skip ahead and read through, as it would make some previous problems easier.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Learn to use the debugger, the sooner the better.

    Jim

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    I have managed to sort it, but it is incrementing the "size" of the array by 1 each time, which I thought wastes memory? There were no errors when I ran the debugger

    Code:
     
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int *growArray (int *friends, int &cur_size)
    {
    
    
        int *new_friends = new int[(cur_size==0 ? 1 : cur_size*2)];
        for (int i=0; i < cur_size; ++i) {
            new_friends[i] = friends[i];
        }
        //*cur_size = (*cur_size==0 ? 1 : *cur_size*2); //added here
        delete []friends;
        return new_friends;
    }
    
    
    string *growArray2 (string *name1, int &cur_size2)
    {
    
    
        string *new_name2 = new string[(cur_size2==0 ? 1 : cur_size2*2)];
        for (int i=0; i < cur_size2; ++i) {
            new_name2[i] = name1[i];
        }
        //*cur_size2 = (*cur_size2==0 ? 1 : *cur_size2*2);
        delete []name1;
        return new_name2;
    }
    
    
    void printArray (string *name1, int *friends, int &cur_size3, int element_set)
    {
    
    
        cout << "the total size of the array is: " << cur_size3 << endl;
        cout << "Values in the array: " << endl;
        for (int i = 0; i < element_set; ++i)
        {
            cout << name1[i] << "[" << i << "] = " << friends[i] << " days since you last spoke to them" << endl;
        }
    }
    
    
    int main()
    {
        string exit = "X";
        string name;
        int size = 0;
        int noDays;
        int next_element = 0;
        int *friends = new int[size];
        string *name1 = new string [size];
            cout << "Please enter your friends name" << endl;
            cin >> name;
            cout << "Please enter no of days you last spoke to them" << endl;
            cin >> noDays;
        while(true) {
    
    
            size=next_element+1;
            friends = growArray(friends, size); //changed here
            name1 = growArray2(name1, size); //changed here
            friends[next_element] = noDays;
            name1[next_element] = name;
            next_element++;
            printArray(name1, friends, size , next_element);
            cout << "Please enter your friends name or X to exit" << endl;
            cin >> name;
            if (name == exit) {
                break;
            }
            cout << "Please enter no of days you last spoke to them" << endl;
            cin >> noDays;
        }
            //printArray(name1, friends, size , next_element);
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation
    By mp252 in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2013, 05:23 PM
  2. Basic dynamic memory allocation problem
    By Crosshash in forum C Programming
    Replies: 11
    Last Post: 03-22-2011, 01:14 PM
  3. Dynamic memory allocation problem
    By linuxlover in forum C Programming
    Replies: 17
    Last Post: 11-25-2010, 01:11 PM
  4. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  5. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM

Tags for this Thread