I have error "exception occurred at (ntdll.dll) access violation when reading at"?

Unhandled exception at(ntdll.dll) : Corrupt record LIST_ENTRY (i.e. double remove).
The damaged record LIST_ENTRY (i.e. double remove).
Exception occurred at (ntdll.dll) access violation when reading at.
//cut out the numbers and the name of the file

When you leave these things the program stops and indicates the line where I free the memory.
And again. This problem is only with 3 of the 4 tests.
What could be the problem?
The code in the spoilers

spoiler
#include "pch.h"
#include <iostream>
#include <stdio.h> 
#include <conio.h>
#include "Functions.h"

int main(int argn, char *argc[]) {
 FILE *dat, *res;
 dat = fopen(argc[1], "r"); // Open input file
 res = fopen(argc[2], "w"); // Otkrytie of vyhodnogo file
 int N = 0;
 int M = 0;
 double **A;
 //double *B;
 double *B = new double[N];// Declare dynamic array
/*
fscanf(dat, "%d", &N); 
fscanf(dat, "%d", &M); // Read matrix dimensions

 double **A = new double*[N];
 double *B = new double[N];// Declare dynamic array
 for (int i = 0; i < N; i++)
 A[i] = new double[M]; 
*/

 readMatrix(N, M, &A, dat); // Reading matrix
 fprintf(res, "Set the matrix A of %d rows and %d columns:\n", N, M);
 //writeMatrix(N, M, A, res); // Output matrix



 bool sum = summa(N, M, A); // Check sum
 if (sum==false)
{
 fprintf(res, "\Summa the elements of the last column is not negative\n");
 fprintf(res, "\n");
 massiv(N, M, A, B);
 writeMassiv(N, B, res); // Output array
 fprintf(res, "\n");
 fprintf(res, "\n");
 //writeMatrix(N, M, A, res); // Output matrix
}
 else {
 fprintf(res, "\Summa the elements of the last column is negative\n");
 fprintf(res, "\n");
 writeMatrix(N, M, A, res); // Output matrix
}

fclose(dat);
 fclose(res); // close the file

 for (int i = 0; i < N; i++)
 delete[] A[i];
 delete[] A; // Release memory from a matrix

 if (sum == false)
{
 delete[] B; // Release memory from a matrix
}
}

spoiler
#include <stdio.h> 
#include <conio.h>
#include "pch.h"
#include "Functions.h"

// Read matrix from file
void readMatrix(int &N, int &M, double ***A, FILE *dat){
 fscanf(dat, "%d", &N);
 fscanf(dat, "%d", &M); // Read matrix dimensions
 *A = new double*[N];

 for (int i = 0; i < N; i++)
 (*A)[i] = new double[M];
 for (int i = 0; i < N; i++)
 for (int j = 0; j < M; j++)
 fscanf(dat, "%lf", &((*A)[i][j]));


}

// Output matrix to a file
void writeMatrix( int N, int M, double **A, FILE *res){
 for (int i = 0; i < N; i++){
 for (int j = 0; j < M; j++)
 fprintf(res, "%6.2 lf ", A[i][j]);
 fprintf(res, "\n");
}
}

// Output the array to a file
void writeMassiv( int N, double *B, FILE *res) {
 for (int i = 0; i < N; i++) {
 fprintf(res, "%6.2 lf ", B[i]);
}
}

double massiv(int N,int M, double **A, double *B){

 for (int i = 0; i < N; i++)
{
 B[i] = sredn(i, M, A);
 } 
 return 0; 
}
// Calculation of the arithmetic mean
double sredn( int i, int M, double **A) {
 double summ = 0;
 for (int j = 0; j < M; j++)
{
 summ = summ + A[i][j];
}
return summ / (M); // Divide by number of elements
}

// Check amount
bool summa( int N, int M, double **A) {
 bool sum = false; // let's Say the amount is NOT negative
 double summ = 0;
 for(int i=0; i<N; i++)
{
 summ = summ + A[i][M-1];
}
 if (summ < 0) {
 sum = true;
}
 return sum;

 }

March 20th 20 at 11:38
1 answer
March 20th 20 at 11:40
Solution
Code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 

double sredn(int i, int M, double **A)
{
 double summ = 0;
 for (int j = 0; j < M; j++)
{
 summ = summ + A[i][j];
}
 return summ / M;
}

void readMatrix(int &N, int &M, double** (&A); FILE *dat) 
{
 fscanf(dat, "%d", &N);
 fscanf(dat, "%d", &M);

 A = new double*[N];

 for (int i = 0; i < N; i++)
{
 A[i] = new double[M];
}

 for (int i = 0; i < N; i++)
{
 for (int j = 0; j < M; j++)
{
 fscanf(dat, "%lf", &A[i][j]);
}
}
}

void writeMatrix(int N, int M, double **A, FILE *res) 
{
 for (int i = 0; i < N; i++) 
{
 for (int j = 0; j < M; j++)
{
 fprintf(res, "%6.2 lf ", A[i][j]);
}
 fprintf(res, "\n");
}
}

void writeMassiv(int N, double *B, FILE *res) 
{
 for (int i = 0; i < N; i++) 
{
 fprintf(res, "%6.2 lf ", B[i]);
}
}

double massiv(int N, int M, double **A, double *B) 
{

 for (int i = 0; i < N; i++)
{
 B[i] = sredn(i, M, A);
}
 return 0;
}

bool summa(int N, int M, double **A) 
{
 bool sum = false; // let's Say the amount is NOT negative
 double summ = 0;
 for (int i = 0; i < N; i++)
{
 summ = summ + A[i][M - 1];
}
 if (summ < 0) 
{
 sum = true;
}
 return sum;
}

int main(int argn, char *argc[]) 
{
 FILE *dat, *res;

 dat = fopen("D:\\in.txt", "r"); 
 res = fopen("D:\\out.txt", "w"); 
 int N = 0;
 int M = 0;
 double **A = nullptr;

 readMatrix(N, M, A, dat);

 double *B = new double[N];

 fprintf(res, "Set the matrix A of %d rows and %d columns:\n", N, M);

 bool sum = summa(N, M, A); 
 if (true)
{
 fprintf(res, "\Summa the elements of the last column is not negative\n");
 fprintf(res, "\n");
 massiv(N, M, A, B);
 writeMassiv(N, B, res);
 fprintf(res, "\n");
 fprintf(res, "\n");
}
 else 
{
 fprintf(res, "\Summa the elements of the last column is negative\n");
 fprintf(res, "\n");
 writeMatrix(N, M, A, res); 
}

fclose(dat);
 fclose(res); 

 for (int i = 0; i < N; i++)
{
 delete[] A[i];
}
 delete[] A;
 delete[] B;
}

I/o
3 3
1 2 3
4 5 6
7 8 9

You are given matrix A of 3 rows and 3 columns:

The sum of the elements of the last column is not negative

 2.00 5.00 8.00

copied, but still

error C2664: "void readMatrix(int &,int &,double ***,FILE *)": cannot convert argument 3 of "double **" to "double ***"
Types referenced by pointers, not unrelated; conversion requires reinterpret_cast, coercion in style With or coercion in style of function - lorna68 commented on March 20th 20 at 11:43
@lelah, copy little need to keep Ctrl + Shift + S
then Ctrl + Alt + F7

and don't forget to change your
dat = fopen("D:\\in.txt", "r"); 
res = fopen("D:\\out.txt", "w");
- tillman18 commented on March 20th 20 at 11:46
@Turner_Welchwarning C4005: _CRT_SECURE_NO_WARNINGS: change macros
note: see the previous definition of "_CRT_SECURE_NO_WARNINGS"
warning C4603: _CRT_SECURE_NO_WARNINGS: macro is not defined or definition is changed after using a precompiled header
note: adding a macro to precompiled header instead of defining it here
note: using precompiled header
error C2061: syntax error: identifier 'FILE'
error C2065: dat: undeclared identifier
error C3861: fscanf: identifier not found
error C2065: dat: undeclared identifier
error C3861: fscanf: identifier not found
error C2065: dat: undeclared identifier
error C3861: fscanf: identifier not found
error C2061: syntax error: identifier 'FILE'
error C2065: res: undeclared identifier
error C3861: fprintf: identifier not found
error C2065: res: undeclared identifier
error C3861: fprintf: identifier not found
error C2061: syntax error: identifier 'FILE'
error C2065: res: undeclared identifier
error C3861: fprintf: identifier not found
error C2664: "void readMatrix(int &,int &,double ***,FILE *)": cannot convert argument 3 of "double **" to "double ***"
note: Types that specify pointers, are not connected; conversion requires reinterpret_cast, coercion in style With or coercion in style of function - lorna68 commented on March 20th 20 at 11:49
@lelah,
Create an empty project https://metanit.com/cpp/tutorial/1.4.php
copypaste code from here in Oehser
profit - tillman18 commented on March 20th 20 at 11:52
@Turner_Welchswapped the following after the true and else and a little bit changed here
spoiler
bool sum = summa(N, M, A); // Check sum
 if (sum == true)
{
 writeMatrix(N, M, A, res);
 fprintf(res, "\Summa the elements of the last column is negative\n");
 fprintf(res, "\n");
}
else
{
 writeMatrix(N, M, A, res);
 fprintf(res, "\Summa the elements of the last column is not negative\n");
 fprintf(res, "\n");
 massiv(N, M, A, B);
 writeMassiv(N, B, res);
 fprintf(res, "\n");
 fprintf(res, "\n");
 }

So now everything works. Thank you! - lorna68 commented on March 20th 20 at 11:55

Find more questions by tags C