Pointers, Structures, and Dynamic Allocation of Memory

Hello! I know I have been posting a lot but I have been learning so much from this community! So thank you. Someone has mentioned that it would more organized for me to use struct instead of writing two arrays. I agree, however, I am not quite sure how the syntax will work out. For example,

int *p_talk = new int[size];
string *p_name = new string[size];

How would I write this using structures? So this is what I did,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct info
{
	int *p_talk; 
	string *p_name; 
};


int main () 
{ 
        int size = 10; 
	person.p_talk = new int[size];
	person.p_name = new string[size];
return 0; 
}


However, this is writing two arrays again. Instead of using structures to organize my arrays better. Any suggestions?

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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

void bubbleSort (int *p_talk, string *p_name, int next_element)
{
	bool swapped = true; 
	int j = 0; 
	int temp; 
	string temp2; 
	while (swapped)
	{
		swapped = false; 
		j++; 
		for (int i = 0; i < next_element - j; i++)
		{
			if (p_talk[i] > p_talk[i + 1])
			{
				temp = p_talk[i];
				p_talk[i] = p_talk[i + 1]; 
				p_talk[i + 1] = temp; 

				temp2 = p_name[i];
				p_name[i] = p_name[i + 1]; 
				p_name[i + 1] = temp2; 
				swapped = true; 
			}
		}
	}
}

int *growArray (int *p_talk, int *size)
{
	*size *= 2;
	int *p_new_talk = new int [*size];
	for (int i = 0; i < *size; ++i)
	{
		p_new_talk[i] = p_talk[i];
	}
	delete [] p_talk;
	return p_new_talk;
}

int main () 
{ 
	int next_element = 0;
	int size = 10; 
	int *p_talk = new int[size];
	string *p_name = new string[size]; 
	int intUserInput; 
	string stringUserInput; 
	cin >> stringUserInput; 
	cin >> intUserInput;

	while (intUserInput > 0)
	{
		if (size == next_element)
		{
			p_talk = growArray(p_talk, &size);
		}
		p_talk[next_element] = intUserInput;
		p_name[next_element] = stringUserInput; 
		next_element++;
		cin >> stringUserInput; 
		cin >> intUserInput; 
	}

	bubbleSort (p_talk, p_name, next_element);

	for (int i = 0; i < next_element; i++)
	{
		cout << "The last time you talked to " << p_name[i] << " was " << p_talk[i] << " days ago." << endl;
	}

    return 0; 
}
softrix has posted a very nice example that is very close to what you are doing.
As his linked post indicated, it uses std::vector. Not clear if you've learned that yet.

To add some further explanation:

Your info struct should contain information about one and only one contact. You support multiple contacts by having an array (or vector) of contact instances.
1
2
3
4
struct info         // Represents one contact
{   int days_ago;  // changed the name to make it clearer
    string name;    // No need for this to be a pointer.
};

Now, you can represent multiple contacts in one of several ways.
1
2
3
4
5
6
7
8
9
//  1.  A simple array
  info   contacts[MAX_CONTACTS];  
//  2.  A vector of contacts (recommended)
  std::vector<info>  contacts;        
//  3.  A dynamically allocated array
  info * p_contacts;                        
  p_contacts = new info[MAX_CONTACTS];
... 
  delete [] p_contacts;  // Be sure to delete 


BTW: You have a memory leak in your original program. p_talk and p_name are never deleted. You should always have a delete for every new.


Last edited on
Topic archived. No new replies allowed.