copying dynamic arrays

i wrote a code that will asks the user to enter a sentence and store the string in a dynamic array. copy the content of the old dynamic array into the new dynamic array and the while loop keeps going and asks the user to enter something new again and store in a new dynamic array. when i run it i am getting :

Unhandled exception at 0x0FB44B19 (vcruntime140d.dll) in countA.exe: 0xC0000005: Access violation writing location 0x2B5063A5.

i know that i can use vectors but for practice purpose , i am only allowed to use dynamic arrays.

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

# include <iostream>


#include <sstream> // used for getline



using namespace std;

int main()
{

	char ch;

	int ctr = 0; 

	string s; 

	string dayName[7];



	unsigned size = 0;

	string * ptr = new string[size];



  //	int ctr = 0;

	while (getline(cin, s) )
	//while (cin.get(ch))
	{

		//dayName[i] = s;

		ptr[ctr] = s; 
 

		/*
		if (ch == 'a')
		{
			++ctr;

		}*/

		//cout.put(ch);


		

		//size*=2; 
		//++ctr; 

		string *temp = ptr; 

		ptr = new string[ctr + 1]; 

		for (unsigned int i = 0; i < ctr; ++i)
		{
			ptr[i] = temp[i]; 

		}

		++ctr ; 

	}


	/*

	for (int j = 0; j< sizeof(dayName) / sizeof(dayName[0]); ++j)

	{
		cout << dayName[j] << "," ; 

	}
	
	
	*/



	for (int j = 0; j< size ; ++j)

	{
		cout << ptr[j] << ",";

	}



	//cout << ctr << endl; 


	system("pause"); 


}

hello masterinex

the problem is that size is initially 0. That means you cannot assign a string. So either it starts with unsigned size = 1; or you move line 38 after line 64 (before line 66).
i canged the size to 1 , but i am still getting this error :

Exception thrown at 0x0FB44B19 (vcruntime140d.dll) in countA.exe: 0xC0000005: Access violation writing location 0x00000000.

at the line 56: string *temp = ptr;
i know that i can use vectors but for practice purpose , i am only allowed to use dynamic arrays.

you could have used dynamic arrays for the day names individually (instead of std:::string) as well as storing all the day names in one dynamic array container which appears to be purpose of this exercise:
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
# include <iostream>
# include <string>
# include <utility>

constexpr auto daysINweek = 7;

int main()
{
    std::string dayName;
    std::string* ptr = new std::string[daysINweek];

    size_t ctr{};
    while (ctr < daysINweek)
    {
        std::string dayName;
        std::cout << "Enter day " << ctr+1 << " of " << daysINweek << "\n";
        getline(std::cin, dayName);
        ptr[ctr] = std::move(dayName);
        dayName.clear();
        ++ctr;
    }
    for (auto i = 0; i < daysINweek; ++i)
    {
        std::cout << ptr[i] << "\n";
    }
    delete [] ptr;
}

and if you're allowed to use smart pointers like std::unique_ptr<std::string> then you don't have to delete the pointer as it clears up after itself:
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
# include <iostream>
# include <string>
# include <utility>
# include <memory>

constexpr auto daysINweek = 7;

int main()
{
    std::string dayName;
    //stdstd::string* ptr = new std::string[daysINweek];
    auto ptr = std::make_unique<std::string[]>(daysINweek);

    size_t ctr{};
    while (ctr < daysINweek)
    {
        std::string dayName;
        std::cout << "Enter day " << ctr+1 << " of " << daysINweek << "\n";
        getline(std::cin, dayName);
        ptr[ctr] = std::move(dayName);
        dayName.clear();
        ++ctr;
    }
    for (auto i = 0; i < daysINweek; ++i)
    {
        std::cout << ptr[i] << "\n";
    }
    //delete [] ptr;
}
Last edited on
i just can't figure out why i am getting an access

Access violation writing location 0x00000000.

at the line 56: string *temp = ptr;
Topic archived. No new replies allowed.