-4

what I want to do is : Input a sentence from the user. Use full stop, space and comma as word separators. Each word should be stored in a 2D array whose columns vary in size and each row stores one word as a NULL terminated string.

For example, if the user inputs:

Hello how are you? It should be stored as:

H E l l o NULL

h o w NULL

a r e NULL

y o u ? NULL

so whenever I try to run my code either this error appears

Exception thrown at 0x00832605 in Project109.exe: 0xC0000005: Access violation writing location 0xFDFDFE03.   

or the program stops working.major problem is in
ptr[i][j] = str1[j];

`

char str1[20];
cin.get(str1, 20);

int  len, sum = 0;
len = strlen(str1);
int i = 0;

while (str1[i] != '\0')
{
    if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
    {
        sum = sum + 1;
    }
    i++;
}

char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{

    ptr[i] = new char[20];


}


for (int i = 0; i < sum; i++)
{

for (int j = 0; j < 20; j++)
{
    ptr[i][j] = '\0';
}}


for (int i = 0; i < sum; i++)
{
    for (int j = 0; j < 20; j++)
    {

        if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
        {
            i++;
        }

        else
        {

            ptr[i][j] = str1[j];

        }
    }
}
for (int i = 0; i < sum; i++)
{
    int j = 0;
    while (ptr[i][j] != '\0')
    {
        cout << ptr[i][j];
        j++;
    }
}


for (int i = 0; i < sum; i++)
{
    delete[] ptr[i];
}
delete[] ptr;


system("pause");
return 0;}

`

user
  • 934
  • 6
  • 17
Hamza Saeed
  • 164
  • 1
  • 15
  • i've mentioned this where compiler is pointing : ptr[i][j] = str1[j]; – Hamza Saeed Feb 26 '17 at 18:21
  • 1
    @HamzaSaeed: Well, that line of code is massively wrong. In your example the `w` is `str1[8]`, and should end up in `ptr[1][2]` but there's no way that `j` can be both `8` and `2` at the same time. – Ben Voigt Feb 26 '17 at 18:27
  • i removed null initialization from the code. now it works but shows junks with other characters... – Hamza Saeed Feb 26 '17 at 18:52

2 Answers2

0

You are indexing out of range, and hitting memory with fence bytes containing 0xFD.

Consider the loop here

for (int i = 0; i < sum; i++)
{
    for (int j = 0; j < 20; j++)
    {
        if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
        {
            i++;
        }
        else
        {
            ptr[i][j] = str1[j];
        }
    }
}

If i is already at (or near) it's maximum value, in the inner loop you might increment it one or more times before reaching ptr[i][j] = str1[j];. At that time i might be way more than sum.

Community
  • 1
  • 1
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

better solution but output is not that as required :

{  char str1[20];
cin.get(str1, 20);

int  len, sum = 0;
len = strlen(str1);
int i = 0;

while (str1[i] != '\0')
{
    if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
    {
        sum = sum + 1;
    }
    i++;
}

char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{

    ptr[i] = new char[len];


}



for (int i = 0; i < sum; i++)
{
    for (int j = 0; j < len; j++)
    {

        if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
        {
            ptr[i][j] = str1[j];
            i++;
        }

        else
        {

            ptr[i][j] = str1[j];

        }
    }
}

for (int i = 0; i < sum; i++)
{
    for (int j = 0; j < len; j++)
    {
        cout << ptr[i][j];

    }
    cout << endl;
}
for (int i = 0; i < sum; i++)
{
    delete[] ptr[i];
}
delete[] ptr;


system("pause");
return 0;}
Hamza Saeed
  • 164
  • 1
  • 15