-3

Who can explain why access violation reading location erorr is thrown and why in a[] i get "erorr reading characters of string"? I have two strings and must to remove all words from first string that containing other string. What i do wrong?

#include "stdafx.h"
#include<iostream>
#include<cstring>
using namespace std;
char s1[100] = {};
char s2[100] = {};
void Words(char s1[], char s2[]) {
  int k = 0;
  char*p1 = nullptr;
  char*np1 = nullptr;
  char*p2 = nullptr;
  char*np2 = nullptr;
  char *m[20];
  char *a[20];
  char s3[100] = {};
  for (int i = 0; i < 20; i++) {
    char n[50] = {};
    char l[50] = {};
    m[i] = n;
    a[i] = l;

  }
  char delimeter[] = " ,.!?;:";
  p2 = strtok_s(s2, delimeter, &np2);
  while (p2 != nullptr) {
    strcpy(a[k], p2);
    k++;
    p2 = strtok_s(nullptr, delimeter, &np2);
  }
  k = 0;
  p1 = strtok_s(s1, delimeter, &np1);
  while (p1 != nullptr) {
    strcpy(m[k], p1);
    k++;
    p1 = strtok_s(nullptr, delimeter, &np1);
  }

  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 20; j++) {
      if (strcmp(m[i], a[j]) != 0 && m[i] != 0 && a[j] != 0) {
        strcat(s3, m[i]);
      }
    }
  }
  puts(s3);
  for (int i = 0; i < 20; i++) {
    delete m[i];
    delete a[i];
  }
}

Main function:

int main()
{
  gets_s(s1);
  gets_s(s2);
  Words(s1, s2);
  return 0;
}
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • 3
    Why on earth don't you simply use `std::string`? This is horrible c++ code after all, more like c. – user0042 Dec 15 '17 at 13:37
  • because in exercise i must write code with char* or char[] strings. – Юра Безлюдний Dec 15 '17 at 13:40
  • 1
    That's a waste of time and probably does more harm than good. Your teacher should feel bad and you should complement that course with a good C++ book. – Baum mit Augen Dec 15 '17 at 13:42
  • `m` and `a` are full of pointers to arrays that don't exist anymore. – François Andrieux Dec 15 '17 at 13:46
  • 1
    Your choice of one letter variable names is unwise and makes your (already weird) code even harder to understand. – Jabberwocky Dec 15 '17 at 13:48
  • maybe, but i still don't understand why i have this problem in code. – Юра Безлюдний Dec 15 '17 at 13:49
  • 1
    Good variable names it not for the compiler, and not for us, it's for _you_ so you can understand you own code more easily while debugging. BTW Visual studio has a world class and very easy to use debugger. Learn how to use it. – Jabberwocky Dec 15 '17 at 13:54
  • 3
    @ЮраБезлюдний Point your teacher [here](https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr) please. – user0042 Dec 15 '17 at 14:03
  • 1
    _"Who can explain why access violation reading location erorr is thrown"_ No one, since you didn't bother to tell readers **where** the error occurs or what it is. Learn how to create an [MCVE](https://stackoverflow.com/help/mcve). – underscore_d Dec 15 '17 at 14:05
  • 1
    YAIT (yet another incompetent teacher). –  Dec 15 '17 at 14:14
  • @manni66 Given the quality of this question I think that is very generous of you. – Jonathan Mee Dec 15 '17 at 14:21

2 Answers2

2

There is at least one problem here:

  for (int i = 0; i < 20; i++) {
    char n[50] = {};
    char l[50] = {};
    m[i] = n;
    a[i] = l;
  }

After that loop all elements of m and a point to variables that have gone out of scope. The variables n and l cease to exist once the scope between the {} of the for loop has been left.

You have many misconceptions about pointers and you should probably read some good book about the C language (the code you wrote is actually more C than C++).

There are certainly more errors.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

There is so much evil in here I don't know where to start.

  • You're calling delete on a statically allocated array
  • p1, p2, np1, and np2 are all unallocated and you're writing to them

Please use string:

const regex re{ "([^ ,.!?;:]+)" };
vector<string> s1Tokens{ sregex_token_iterator(cbegin(s1), cend(s1), re, 1), sregex_token_iterator() };
vector<string> s2Tokens{ sregex_token_iterator(cbegin(s2), cend(s2), re, 1), sregex_token_iterator() };

sort(begin(s1Tokens), end(s1Tokens));
sort(begin(s2Tokens), end(s2Tokens));

set_difference(cbegin(s1Tokens), cend(s1Tokens), cbegin(s2Tokens), cend(s2Tokens), ostream_iterator<string>(cout, "\n"));

Live Example

This example could easily be fleshed out further if after understanding the above example you find yourself interested in studying further I'd start here: https://stackoverflow.com/a/38595708/2642059

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288