I've been playing around with pointers and i have encountered my process terminating after it's done with the traditional windows "is not responding" and exit code -1073741819 (0xC0000005).
quickfix suggested by my IDE was to initialize the *ptr with __p__fmode().
However I read that, "The __p__fmode function is for internal use only, and should not be called from user code."
If you would be so kind to help me with this problem pretty please :)
#include <iostream>
using namespace std;
// Code written in Jetbrains's CLion with mingw-w64 compiler
int main() {
int r, c, *ptr = __p__fmode(); // No idea what's that __p__fmode(), but without it process terminates
// with code "Process finished with exit code -1073741819 (0xC0000005)"
cout<<"Enter number of rows for Matrix A:";
cin>> r;
cout<<"Enter number of columns for Matrix A:";
cin>> c;
int A[r][c];
A[0][0] = 0;
A[0][1] = 1;
A[1][0] = 2;
*ptr = A[0][0];
cout<<*(*(A+0)+0)<<"\t";
cout<<*(*(A+0)+1)<<"\t";
cout<<*(*(A+1)+0)<<"\t";
}
However I read that, "The __p__fmode function is for internal use only, and should not be called from user code."
If you would be so kind to help me with this problem pretty please :)
The solution is to not call the function. In general, don't call functions unless you know what they do.
P.S.
cin>> r; cin>> c; int A[r][c];
The size of an array must be constant at compile time. The example program is ill-formed in C++.
When ptr
is uninitialised, the statement
*ptr = A[0][0];
has undefined behaviour. One possible (not guaranteed) consequence of undefined behaviour is an abnormal program termination - such as you are seeing.
The function __p__fmode()
returns a pointer to a global variable according to Microsoft documentation. The function returns an int *
which suggests (but does not guarantee) that the global variable involved as type int
.
So, initialising ptr
with the value returned by __p__fmode()
means that the assignment *ptr = A[0][0]
overwrites that global variable.
The Microsoft documentation also states "The __p__fmode
function is for internal use only, and should not be called from user code.". Which means using it, as you are, is inadvisable.
There is a secondary concern (at least, one you haven't asked about) with your code - a definition int A[r][c];
where r
and c
are variables, is not valid C++. If your compiler allows this, then it is as a non-standard extension.
User contributions licensed under CC BY-SA 3.0