Проблема переповнення буферу char** copy в Visual Studio!
#include <iostream>
#include <string.h>
using namespace std;
char** AddPtr(char** pp, int size, const char* str);
char** DelPtr(char** pp, int size, int ncell);
char** InsPtr(char** pp, int size, int ncell, const char* str);
int main()
{
setlocale(LC_ALL, "rus");
int size = 0;
char** pp = 0;
cout << "~~~~~Додаєм~~~~~" << endl;
pp = AddPtr(pp, size, "11111111111111111");
size++; //1
pp = AddPtr(pp, size, "22222222222222222");
size++;//2
pp = AddPtr(pp, size, "33333333333333333");
size++;//3
pp = AddPtr(pp, size, "44444444444444444");
size++;//4
pp = AddPtr(pp, size, "55555555555555555");
size++;//5
for (int i = 0; i < size; i++)
cout << endl;
cout << "~~~~~Видаляєм 3 стрічку~~~~~" << endl;
pp = DelPtr(pp, size, 2);
size--; //4
for (int i = 0; i < size; i++)
cout << pp[i] << endl;
cout << endl;
cout << "~~~~~Додаєм вказівник ~~~~~" << endl;
pp = InsPtr(pp, size, 2, "333");
size++; //5
for (int i = 0; i < size; i++)
cout << endl;
for (int i = 0; i < size; i++)
{
delete[] pp[i];
}
delete[] pp;
system("pause");
return 0;
}
char** AddPtr(char** pp, int size, const char* str)
{
if (size == 0) {
pp = new char* [size + 1];
}
else {
char** copy = new char* [size + 1];
for (int i = 0; i < size; i++)
{
copy[i] = pp[i];
}
delete[] pp;
pp = copy;
}
size_t len = strlen(str);
pp[size] = new char[len + 1];
strcpy_s(pp[size], len + 1, str);
return pp;
}
char** DelPtr(char** pp, int size, int ncell)
{
char** copy = new char* [size - 1];
for (int i = 0; i < ncell; i++)
{
copy[i] = pp[i];
}
for (int i = ncell; i < size - 1; i++)
{
copy[i] = pp[i + 1];
}
delete[] pp[ncell];
delete[] pp;
pp = copy;
return pp;
}
char** InsPtr(char** pp, int size, int ncell, const char* str)
{
char** copy = new char* [size + 1];
for (int i = 0; i < ncell; i++)
{
copy[i] = pp[i];
}
size_t len = strlen(str);
copy[ncell] = new char[len + 1];
strcpy_s(copy[ncell], len + 1, str);
for (int i = ncell + 1; i < size + 1; i++)
{
copy[i] = pp[i - 1];
}
delete[] pp;
pp = copy;
return pp;
}