0

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";

}
Reneq
  • 1
  • 1
  • 3
    That suggestion is nonsense and you should ignore it. You need `ptr` to point to some memory you can actually write to, e.g. `ptr = new int;`. What's the purpose of it, anyway? – Nate Eldredge Feb 26 '21 at 00:06
  • I was asked to fill a 2d matrix in a way that involves pointers. Kinda trying to figure it out myself, but hey It works with pointing to new int :) Thank you – Reneq Feb 26 '21 at 00:07
  • What the assignment is likely asking is for you to replace this little bit of [non-Standard code](https://stackoverflow.com/questions/1887097), `int A[r][c];` with what is described here: [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687) Note that in real-world programming making a 2D array like this would likely earn you the mockery of your peers. Use `std::vector`, and preferably a single `vector` that's [made to look like it is multi-dimensional](https://stackoverflow.com/a/2076668/4581301). – user4581301 Feb 26 '21 at 00:13
  • When you build arrays of pointers to other arrays, the program has to hop around in memory and cannot effectively use cache. This can slow the program by whole orders of magnitude. – user4581301 Feb 26 '21 at 00:14
  • 1
    Note: `*ptr = A[0][0];` is likely where the program crashes. If `ptr ` was not pointed at anything, you cannot safely dereference the pointer, `*ptr`, and assign to the variable at the pointer. The program now does not crash because `*ptr = __p__fmode();` pointed `ptr` at something, but it's not something you want to overwrite. Refresh your understanding of pointers by rereading the section in your textbook on the use of pointers. At the moment you seem to be flailing and have too little a grasp on the fundamentals to avoid grabbing onto incorrect solutions. – user4581301 Feb 26 '21 at 00:30
  • 1
    By the way, what IDE are you using? I'd love to know how it came to the conclusion that such a quickfix suggestion was appropriate. – user4581301 Feb 26 '21 at 00:33
  • @user4581301 I'm ready to bet the IDE got confused because of `using namespace std` importing garbage into global namespace. – spectras Feb 26 '21 at 00:52

2 Answers2

1

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++.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

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.

Peter
  • 35,646
  • 4
  • 32
  • 74