Incorrect memory



  • The code is compiled, but: (a) Compiled +- correctly only if the symbol is inserted, not the whole word (b) After the first cout cycle while() "murder"?

    Code:

    `   #include <iostream>
        #include <string.h>
        #include <stdio.h>
        #include <algorithm>
        #pragma warning (disable : 4996)
        using namespace std;
        int main(void)
        {
            char *a;
            int n;
            cout << "Enter the number of words which you wanna enter (max:10)" << endl;
            cin >> n;
            cout << "Enter a few words: " << endl;
            a = new char[n];
            for (int i = 0; i < n; i++)
            {
                cin >> a[i];
            }
            const char* limits = " ;.,-()";
            char *pw;
            cout << "Words: " << endl;
            pw = strtok(a, limits);
            while (pw != NULL)
            {
                int b=atoi(pw);
                cout<<"\t" << pw << "\t" << endl;
                cout << "As numbers: \n\t" << b << endl;
                int c = (*max_element(a, a + n));
                cout << "Max element: " <<c<< endl;
                pw = strtok(NULL, limits);
            }
            return 0;
        }`
    

    результат вывода:



  • In this exercise, you have a single set of symbols that you handwritten size. Instead of creating a mass of four words, this code creates a set of four symbols. Almost any function in the C++, processing the symbol arrays until they meet '0'. The original sets of symbols are entirely of zero-symbols, and then we finish our new meanings at the beginning. For example, if we had a mass of 5 symbols.

    _

    Then, after the introduction of four symbols at the beginning of the line, it would look like

    "help/0."

    and the cout function will remove the line from the first symbol until it meets /0. But if we introduce five symbols, the line takes the view of a helpm and cout doesn't know what time to stop. And he'll start taking out the next memory cells until he meets '0', which happened on the crypto. When you work with a set of symbols, you have to understand that you have limits on size not in n symbols but in n-1 symbols.

    Because the mass was limited to a size of four symbols, and you've made exactly more than three symbols - it's been the conclusion of an ununderstood marsor.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2