The program's task: read a string from the file, each word from which must be assigned to a separate element of the array. Next, find the 3 longest and 3 shortest words, and then print all the words in reverse order of the alphabetic. All tasks written by me the program solves, but I can not clear the memory. The problem is in a piece of the program, in which the elements of the array b are assigned a string from the variable token (all this in a while loop). It is after this action that I cannot delete []. All arrays have already done the same dimension, but still it did not help. What could be the problem? The code is attached below. Thank you in advance!

#define _CRT_SECURE_NO_WARNINGS #include "stdafx.h" #include <stdlib.h> #include <stdbool.h> #include <iostream> #include <locale.h> #include <windows.h> #include <ctype.h> #include <fstream> #include <malloc.h> #include <conio.h> #include <vector> #include <algorithm> #include <string> #include <cstring> using namespace std; void bigswap(int i, int len, int biggest[]) { if (len > biggest[0]) { biggest[4] = biggest[2]; biggest[5] = biggest[3]; biggest[2] = biggest[0]; biggest[3] = biggest[1]; biggest[0] = len; biggest[1] = i - 1; } else { if (len > biggest[2]) { biggest[4] = biggest[2]; biggest[5] = biggest[3]; biggest[2] = len; biggest[3] = i - 1; } else { if (len > biggest[4]) { biggest[4] = len; biggest[5] = i - 1; } } } } void littleswap(int i, int len, int little[]) { if (len < little[0]) { little[4] = little[2]; little[5] = little[3]; little[2] = little[0]; little[3] = little[1]; little[0] = len; little[1] = i - 1; } else { if (len < little[2]) { little[4] = little[2]; little[5] = little[3]; little[2] = len; little[3] = i - 1; } else { if (len < little[4]) { little[4] = len; little[5] = i - 1; } } } } void gettext(char **b, int kolslov) { printf("Слова, выведенные в порядке, обратном алфавитному:\n\n"); for (int i = 0; i < kolslov; i++) { printf("%s\n", b[i]); } printf("\n\n"); } void minusswap(char **b, int kolslov) { for (int i = 0; i < (kolslov - 1); i++) { for (int j = i + 1; j < kolslov; j++) { if ((int)b[i][0] < 91) { b[i][0] = (char)((int)b[i][0] + 32); } if ((int)b[j][0] < 91) { b[j][0] = (char)((int)b[j][0] + 32); } if (strcmp(b[i], b[j]) < 0) swap(b[i], b[j]); } } } void initialization(int biggest[], int little[]) { for (int i = 0; i < 6; i++) { biggest[i] = -1; little[i] = 100; } } int main() { char **b, *buffer, *token, delims[] = " \n.,!?/<>|)(*:;\""; const int len_text = 300; int max_len = 0, size_len = 0, Nslov = 0, kolslov = 0, menu = 0; int biggest[6], little[6]; FILE *load = NULL; setlocale(LC_ALL, "Russian"); SetConsoleCP(1251); SetConsoleOutputCP(1251); printf("Данная программа считывает ваш текст из файла, находит в нем три самых длинных и три самых коротких слова, а также располагает все слова в порядке, обратном алфавитному.\n\n\n"); system("pause"); system("cls"); for (;;) { load = fopen("FILE.txt", "r"); fscanf_s(load, "%i\n", &kolslov); initialization(biggest, little); b = new char*[kolslov]; /*for (int i = 0; i < kolslov; i++) { b[i] = new char[30]; }*/ buffer = new char[300]; token = new char[300]; fgets(buffer, len_text, load); printf("Первоначальный текст:\n\n%s\n\n\nНайденные слова:\n\n", buffer); token = strtok(buffer, delims); int i = 0; while (token != NULL) { int len = strlen(token); b[i] = new char[300]; b[i] = token; Nslov++; printf("%s\n", b[i]); i++; bigswap(i, len, biggest); littleswap(i, len, little); token = strtok(NULL, delims); } printf("\n\nТри самых длинных слова в данном тексте:\n\n%s\n%s\n%s\n\n", b[biggest[1]], b[biggest[3]], b[biggest[5]]); printf("\nТри самых коротких слова в данном тексте:\n\n%s\n%s\n%s\n\n\n", b[little[1]], b[little[3]], b[little[5]]); minusswap(b, kolslov); gettext(b, kolslov); system("pause"); for (int i = 0; i < kolslov; i++) { printf("\n%s\n", b[i]); delete[] b[i]; } delete[] b; fclose(load); system("pause"); system("cls"); } system("pause"); } 

The code of the place where the problem occurs, which later causes an error:

 while (token != NULL) { int len = strlen(token); b[i] = new char[300]; b[i] = token; Nslov++; printf("%s\n", b[i]); i++; bigswap(i, len, biggest); littleswap(i, len, little); token = strtok(NULL, delims); } 

The code that goes through the error:

 for (int i = 0; i < kolslov; i++) { printf("\n%s\n", b[i]); delete[] b[i]; } delete[] b; 

Error: trigger point triggered in delete_scalar.cpp function

 // // delete_scalar.cpp // // Copyright (c) Microsoft Corporation. All rights reserved. // // Defines the scalar operator delete. // #include <crtdbg.h> #include <malloc.h> #include <vcruntime_new.h> void __CRTDECL operator delete(void* const block) noexcept { #ifdef _DEBUG _free_dbg(block, _UNKNOWN_BLOCK); #else free(block); #endif } 

Error in line: _free_dbg (block, _UNKNOWN_BLOCK);

Closed due to the fact that off-topic participants VTT , aleksandr barakin , mkkik , 0xdb , entithat May 3 at 9:20 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - VTT, mkkik, 0xdb, entithat
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Fixed for pluses - mr_kosten 1:29 pm
  • However, to throw off a fragment of a code with an error, you throw off the whole - SKIP
  • @mr_kosten and how exactly "does not work"? does not compile, crashes when it starts, does a meteorite arrive and beat on fingers? - KoVadim pm
  • @SKIP inserted the necessary pieces of code - mr_kosten
  • @KoVadim painted the error in detail - mr_kosten 4:26 pm

1 answer 1

The problem is clearly with memory allocations. For example, I really don’t like the variable kolslov - anything can happen there.

And the reason is similar in the strings b[i] = new char[300]; b[i] = token; b[i] = new char[300]; b[i] = token; - we first allocate the memory, and then we rewrite it. But these are just flowers. The token array is created before entering the loop. This means that the same array will be assigned to the elements of array b . And when the memory is released, we try to delete the same block twice. And we get from runtime on the fingers.

But then I looked even more attentively and realized that everything was very sad. We follow hands:

  • allocate memory for token first
  • then we write strtok results into it (we have already lost the previously allocated memory)
  • then, this is a pointer to the middle of the buffer is spread over the array b (yes, now b stores pointers to the middle of buffer, plus the memory allocated for the elements of the array b is already lost.
  • at the end, when the memory is freed, delete[] takes a pointer to the middle of the buffer array and tries to delete it, but this is not the best idea and crashes.

What to do? as a minimum, do not write to the C and semi-manual memory management (or all the same, rewrite everything to C and change the tag). Also, do not mix C and C ++ functions. And finally, examine how strtok works (and find out that it spoils the original array. But it has a pair function that does not do this).