array containing structures, grow dynamic memory

Hi guys! kind of stuck in this single problem since yesterday. so any help to give me 'redemption'
;)
from this problem will be highly appreciated.
The problem, in the specific section of code, is I want to grow(increase memory of) the dynamic array that contains the data in the form of structures. but I am getting some kind of errors.
I will post the codes of these specific sections. I get errors in 3 lines all of which are commented below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
///below is also the structure that I declared

struct contact
{
    string name;
    int days;
};

//the function to grow array containing structures below
contact* grow_array(int* dummy_array,int* p_array_size)
{ 
    contact* temp_array=new contact[*p_array_size*2];
    for(int i=0;i<*p_array_size;i++)
    {

        temp_array[i].name=dummy_array[i].name;//get error 1 in this line
        temp_array[i].days=dummy_array[i].days;//get error 2 in this line 
    }
    delete[] dummy_array;
    *p_array_size*=2; ///updating the array_size.
    return temp_array;
} 


///int main function of specific part

int main()
{
    int x=0;  ///to update the position array upto where position is filled.
    int array_size=5;
    string choice;
    contact* array=new contact[array_size];
    do
    {
        cout<<endl<<endl;
        cout<<"A to add contact\t U to update contact\t Da to display all contact\nE to exit\tDs to display info of specific contact\nEnter what would you like to do: ";
        cin>>choice;
        if(array_size-1==x)  ///to update size of dynamically created array
        {
                array=grow_array(array,&array_size);   ///error no. 3 here

        }
        ///some other codes in int main. 


If you can't find the problem based in these codes, the entire codes can be found here: http://cpp.sh/4qgc
closed account (SECMoG1T)
Surely we should get some errors but that's normal, look at this yourself

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

struct contact///this is a struct 

contact* grow_array(int* dummy_array,int* p_array_size)///what type is dummy_array , 
{                                                      ///ooh ,it's of type int[]
    contact* temp_array=new contact[*p_array_size*2];   
    for(int i=0;i<*p_array_size;i++)  ///here temp_array is of type contact, yeh that's it
    {

        temp_array[i].name=dummy_array[i].name;
        ///are we trying to copy something called name from int , ooh yeah that's the problem
        temp_array[i].days=dummy_array[i].days;//get error 2 in this line 
    }
    delete[] dummy_array;
    *p_array_size*=2; ///updating the array_size.
    return temp_array;
} 


//in main 
contact* array=new contact[array_size];
array=grow_array(array,&array_size);///array is of typr contact[] not  int[]
             ///grow_array()  expects an array of int[] 
Last edited on
oh, solved the problem. just changing int to 'contact' did it.
but stil, just adding a single contact crashes the program. just can't spot the problem. would be glad if you could have a look at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
using namespace std;
struct contact
{
    string name;
    int days;
};
void sort(contact array[],int array_size);

contact* grow_array(contact* dummy_array,int* p_array_size)
{
    contact* temp_array=new contact[*p_array_size*2];
    for(int i=0;i<*p_array_size;i++)
    {

        temp_array[i].name=dummy_array[i].name;
        temp_array[i].days=dummy_array[i].days;
    }
    delete[] dummy_array;
    *p_array_size*=2; ///updating the array_size.
    return temp_array;
}


void add_contact(contact *dummy_array,int *p_x,int array_size)
{
        cout<<"Enter name: ";
        cin>>dummy_array[*p_x].name;
        do
        {
        cout<<"Enter number of days you last met "<<dummy_array[*p_x].name<<": ";
        cin>>dummy_array[*p_x].days;
        }while(dummy_array[*p_x].days<0);
        (*p_x)++;
        sort(dummy_array,array_size);
}
void update_contact(contact array[],int array_size)
{
    int i=0;
    string search_name;
    cout<<"Enter name whose info you would like to update(case sensitive): ";
    cin>>search_name;
    for(i=0;search_name!=array[i].name&&i<array_size;i++)
    {
        if(search_name==array[i].name)///condition of array_size already included in for function above
        {
            cout<<"Found "<<array[i].name<<". Last met "<<array[i].days<<" ago! updated number of days last met: ";
            cin>>array[i].days;
        }
        else if(i=(array_size-1)&&array[i].name!=search_name)
        {
            cout<<search_name<<" not found in database!";
        }
    }

}
void display_spec_contact(contact array[], int array_size)
{
    int i=0;
    string search_name;
    cout<<"Enter name whose info you would like to see(case sensitive): ";
    cin>>search_name;
    for(i=0;array[i].name!=search_name&&i<array_size;i++)
    {
        if(search_name==array[i].name)///condition of array_size already included in for function above
        {
            cout<<"Name: "<<array[i].name<<"\nLast met: "<<array[i].days<<" ago";
        }
        else if(i=(array_size)-1&&search_name!=array[i].name)
        {
            cout<<search_name<<" not found in database!";
        }
    }
}
void display_all_contact(contact array[],int array_size)
{

    for(int i=0;i<array_size;i++)  ///displays contact till the end of array size
    {
        cout<<"Name: "<<array[i].name<<endl<<"Last met: "<<array[i].days<<endl<<endl;
    }
}
void sort(contact array[], int array_size)
{
    contact temp;
    for(int i=0;i<array_size;i++)///till end of array_size
    {
        int i_of_smallest_days=i; ///  i_of_smallest days  = meaning =  index_of_smallest_days
        for(int j=(i++);j<array_size;j++)   ///only 5 for now.. need to update
        {
            if(array[j].days<array[i_of_smallest_days].days)
            {
                i_of_smallest_days=j;
            }
        }
        temp.name=array[i_of_smallest_days].name;
        temp.days=array[i_of_smallest_days].days;
        array[i].name=array[i_of_smallest_days].name;
        array[i].days=array[i_of_smallest_days].days;
        array[i_of_smallest_days].name=temp.name;
        array[i_of_smallest_days].days=temp.days;
    }
}
int main()
{
    int x=0;  ///to update the position array upto where position is filled.
    int array_size=5;
    string choice;
    contact* array=new contact[array_size];
    do
    {
        cout<<endl<<endl;
        cout<<"A to add contact\t U to update contact\t Da to display all contact\nE to exit\tDs to display info of specific contact\nEnter what would you like to do: ";
        cin>>choice;
        if(array_size-1==x)  ///to update size of dynamically created array
        {
                array=grow_array(array,&array_size);

        }
        if(choice=="A")
        {
            add_contact(array,&x,array_size);
        }

        else if(choice=="U")
        {
            update_contact(array,array_size);
        }
        else if(choice=="Da")
        {
            display_all_contact(array,array_size);
        }
        else if(choice=="Ds")
        {
            display_spec_contact(array,array_size);
        }
        else if(choice=="E")
        {
            cout<<"Goodbye!\n";
        }

        else
        {
            cout<<"Wrong choice!";
        }
    }while(choice!="E");
}


closed account (SECMoG1T)
I believe the problem is in your "sort" function program crashes mostly result from access violations and other problems that might not fit in this context, it seems on line 89 you are fetching the value of i and then incrementing it that might cause your problem though am not very sure

1
2
3
 for(int j=(i++);j<array_size;j++)/// try change this
 //to
 for(int j=(i);j<array_size;j++)
I told you already about the sort problem. Wonder why you ignored it...

Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sort(contact array[], int array_size)
{
    for(int i=0;i<array_size;i++)///till end of array_size
    {
        bool is_swapped = false;
        for(int j=(i + 1);j<array_size;j++)   ///only 5 for now.. need to update
        {
            if(array[j - 1].days > array[j].days)
            {
                std::swap(array[j - 1], array[j]);
                is_swapped = true;
            }
        }
        if(is_swapped)
            ;
        else
            break;
    }
}
Not tested!

for std::swap(...) see:
http://www.cplusplus.com/reference/utility/swap/?kw=swap
Last edited on
@andy1992 implementing your suggestion didn't work. apparently they don't even get stored as nothing prints when I call display_all_contact function.

@coder777 using the code you suggested doesn't give the error message but doesn't work in desired way. eg: after adding second contact, when I display all contacts using 'Da', the first contact is wiped out and only second contact remains.
and actually I did consider your suggestion about sort problem
Due to line 61 (i++) it is out of bounds for the last element.
After sorting the second last element, only last element is left. So, despite being out of bound, it doesn't need be sorted because after sorting second last element, last element would become largest by default.
Or at least that is what I figured. Thus, didn't change sort function but definitely didn't ignore your suggestion. Please correct me if I got it wrong.


closed account (SECMoG1T)
Never mind your sort function must be misbehaving here

1
2
3
4
if(array[j].days<array[i_of_smallest_days].days)
   {
                i_of_smallest_days=j;
   }


So i basically decided to rework your sort function with my favorite selection sort function
... consisting of three easy to debug helper functions , if you like it replace your sort funct definition with this simple functions then your program should work fine i believe .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int smallest_ind(contact dummy[],int start,int stop)
{
    int curr=start;
   while(start<stop)
   {
       if(dummy[start].days<dummy[curr].days&&dummy[start].days>0)
         curr=start;
       ++start;
   }

   return curr;
}

void my_swap(contact &cont1,contact& cont2)
{
    std::swap(cont1,cont2);
}

void sort(contact array[],int array_size)
{
    for(int i=0;i<array_size;i++)
    {
        int smallest=smallest_ind(array,i,array_size);
        my_swap(array[smallest],array[i]);
    }
}


Hope that helps.
Last edited on
Your code did work but the program, overall didn't work as I expected. There are other anomolies. I think I would leave it for now, move on to next problems and chapters and back to it later on.
Though I didn't quite understand the need of this:
&&dummy[start].days>0) ///in line 6 .
guess it was already covered initially in program.
thanks for all the help.
closed account (SECMoG1T)
That was where your bug sourced, this is what happened, when you created your array some of your contact.days were default initialized {0} so that when you run this two antagonizing snippets

1
2
3
4
5
6
7
while(dummy_array[*p_x].days<0);
//and 

if(array[j].days<array[i_of_smallest_days].days)////some values are default init to 0
 {
     i_of_smallest_days=j;
 }


they definitely get us into trouble, think of what would happen here


Jane 2   ///first read x=0 ,x++
empty 0
-
-
-


if we sort the above array , jane would be replaced with {empty, 0} then on the next read array[x] would be on the current position occupied by jane hence overwriting the entry, simple as that, at the end we owuld have an empty array note: av also realized that using this condition {dummy[start].days>0} is not a solution, maybe a better solution would be to sort your array before reallocation all after completing reading your data.

What other problems did yo encounter?
Last edited on
I don't think anyone pointed out the problem with line 35 yet.

sort(dummy_array,array_size);

should be

sort(dummy_array, *p_x);
@andy1992 your explanation about what goes behind the scene made it very clear. thnx for that. but
maybe a better solution would be to sort your array before reallocation all after completing reading your data.

if so, when i call display_all_function() before, reallocation, my results wouldn't be sorted while question asks us to do so.
others problem is with update function. if I try to update the name that I actually have added, the next output asking us to update the info isn't printed(which should have been). and if i enter name that doesn't exist, infinite loop of text saying "not found in database" is outputted. but i think i took care of both the problem in the code.
i am running in so many problem at this stage. do you think i wrote a bad code for the problem? also this many bugs at this stage, just finished pointers, normal in c++?

@fg109 I did try your approach later, but still so many bugs mentioned above.
Last edited on
closed account (SECMoG1T)
Just a question , have you learnt about linked list? because you said just finished pointers, normal in c++? <>:}
Not yet, but that's the name of the next chapter.
Topic archived. No new replies allowed.