0

I'm posting this question here because I'm having an issue at the memory level not the coding level currently I ran the below code

void* Newnumber;
int* ptr;
Newnumber = &dbCircle;
ptr = reinterpret_cast<int*>(Newnumber);
        
*ptr = 1; //memory error here why?

dbCircle(x, y, RADIUS); //Void function(int, int, int);

This code run in let's say int main() will compile fine but will produce this error:

Unhandled exception at program.exe: 0xC0000005: Access violation writing location 0x004e9e20

Why? essentially this should work as I'm trying to say if you return 1 point to function dbCircle and go from there

What I expect is if 1 is returned to point to void dbCircle function using a reference but for some reason I reach a complex memory error even though the code builds fine. To reproduce you can use any void function that returns 3 ints

wohlstad
  • 12,661
  • 10
  • 26
  • 39
DB_Coding
  • 17
  • 3
  • 1
    `ptr` holds the address of the function `dbCircle` (i.e. it's code). You cannot write to this memory and hence `*ptr = 1` is illegal. – wohlstad Dec 22 '22 at 08:02
  • 1
    "I'm trying to say if you return 1 point to function dbCircle and go from there" - But that's not even close to what your code is actually saying. What even makes you think it is? – Sebastian Redl Dec 22 '22 at 08:03
  • What are you trying to do? An access violation or some other crash is pretty much expected for trying to write to a function – Alan Birtles Dec 22 '22 at 08:05
  • I don't think you understand the task of course I could actually write an if statement that if 1 is true go to function I don't want that. I want to make dbCircle have an object ID of 1 which is why I'm using casting 1 to point to dbCircle. – DB_Coding Dec 22 '22 at 08:10
  • 1
    Maybe a [mre] would help explain what you're trying to do, it makes no sense to me – Alan Birtles Dec 22 '22 at 08:12
  • 1
    You have misunderstood some fundamental things, but it's not obvious what. "I want to make dbCircle have an object ID of 1 which is why I'm using casting 1 to point to dbCircle" makes no sense at all. – molbdnilo Dec 22 '22 at 08:14
  • For a different Function let's say I have void function (int); If I return this as void function(1); I want 1 to point to the dbCircle function. So the logic is void function(1) = dbCircle function. – DB_Coding Dec 22 '22 at 08:17
  • If you need objects then you don't need functions, but classes or structs. E.g if you want a circle with an id, a position and a radius . `struct Circle { int id; double x; double y; double radius };`. Also note that in C++ pointers are usually not the solution you will need first. – Pepijn Kramer Dec 22 '22 at 08:18
  • And functions can return values too. So you can have a function `int f(int n)` that does calculations on n and returns the result. I think you will be best helped with some more C++ training : https://www.learncpp.com/cpp-tutorial/introduction-to-functions/, https://www.learncpp.com/cpp-tutorial/classes-and-class-members/ – Pepijn Kramer Dec 22 '22 at 08:19
  • This is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Could you describe what you actually want to accomplish? (Not the code you want to write but your ultimate goal.) – molbdnilo Dec 22 '22 at 08:19
  • @molbdnilo Yes that thought occured to me too. I think he tried to explain, but it is not fully clear to me either. What I understood : "I want a circle with an id of one" which seems like he needs a struct/class. – Pepijn Kramer Dec 22 '22 at 08:21
  • My ultimate goal is to have a program navigate to functions by the use of numbers so in this case if a function returns 1 here go to another function to control. – DB_Coding Dec 22 '22 at 08:22
  • So, `if (some_value == 1) { dbCircle(x, y, RADIUS); }`? Or using a table of functions, like `my_functions[some_value](x, y, RADIUS);`? – molbdnilo Dec 22 '22 at 08:32
  • Example: FunctionA(1); 1 points to FunctionB(3,4,5) // Pointer to this function is 1 which is why I casted in the 1st place its not supposed to modify it only navigate to the function in code. – DB_Coding Dec 22 '22 at 08:52
  • A number can't "point" to a function. If you want to associate a number with a function, or with a specific function call, you need to use one of the methods I suggested. (And I would recommend reading a bit more in a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) as you seem to have some serious misconceptions.) – molbdnilo Dec 22 '22 at 11:40
  • Misconceptions No you have the option to change pointers in C++ I'm trying to turn 1 into a pointer for my function which is possible as I got it to compile even get past the code below example works as now I'm able to past the logic dbCircle(x, y, RADIUS); int i = 1; void** Newnumber; int* ptr; *Newnumber = *dbCircle; Newnumber = reinterpret_cast(ptr); ptr = reinterpret_cast(Newnumber); *ptr = i; CDllHelper Newmodule; Newmodule.objAIStart(); Newmodule.objAIAddEnemy3(2,1,3); – DB_Coding Dec 22 '22 at 12:06
  • That code technically works but when I attempt to access number 1 at objAIAddEnemy it still fails that I'm not sure why. – DB_Coding Dec 22 '22 at 12:11
  • @DB_Coding I rolled back you edit, because you should not modify the question in a significant way after you have a valid answer. Please post a new question if you like. – wohlstad Dec 23 '22 at 18:26
  • BTW I don't understand why the question was closed due to missing details or clarity. I think it is very clear. Voted to re-open. – wohlstad Dec 23 '22 at 19:50
  • It looks like you're using some sort of game engine which uses integers to identify game objects, and you're trying to tie one such ID (`1`) to one of your own (C++) objects that'd be a circle with `x`, `y` and `radius`. Is that right? In that case, you will need to see how the engine allows you to create custom objects and instantiate them, there's no way to do it from the outside. – Quentin Dec 24 '22 at 17:36

1 Answers1

2

"I'm trying to say if you return 1 point to function dbCircle and go from there"

Your intention is not entirely clear.
But your code definitely does not do anything like that.

This line:

Newnumber = &dbCircle;

Is assigning Newnumber to the address of the function dbCircle, i.e. the address where the code for the function resides.

This line:

ptr = reinterpret_cast<int*>(Newnumber);

Is assigning ptr to that same address, interpreted as if it was containing a valid int (which is doesn't).

Now this line:

*ptr = 1; //memory error here why?

It attempting to write 1 in the address stored in ptr, i.e. the address of the code for dbCircle.
Since this is illegal you get an access violation runtime error.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • reinterpret_cast isn't rewriting the address its rewriting the pointer that navigates to the address I'm not trying to change the address of the code here – DB_Coding Dec 23 '22 at 18:06
  • @DB_Coding, when you use `*ptr = 1`, you attempt to write to the location in memory the pointer is pointing to. The problem is that it points to the function's code. I.e. the problem is not the `reinterpret_cast` itself, but the fact that you assign the pointer the address of a function. – wohlstad Dec 23 '22 at 18:08
  • As I explained in my answer, this is the reason for the access violation you got, in the original posted code. – wohlstad Dec 23 '22 at 18:29
  • Just because you posted the answer doesn't make it right there is a way to do this I just never done it before. My question is not invalid a similar question or task was performed here https://stackoverflow.com/questions/30768714/properly-casting-a-void-to-an-integer-in-c so again the answer you provided is wrong – DB_Coding Dec 23 '22 at 19:08
  • No. The post you mentioned above is different. In that case the address assigned to the pointer was not the address of the code of a function like in your code. It was an integer value and it was cast to a void*. Some other comments above tried to explain the issue of assigning a function address to a data pointer as well. See e.g. https://stackoverflow.com/questions/74885577/reinterpret-castint-fails-when-referencing-a-void-function./74885698#comment132158754_74885577. – wohlstad Dec 23 '22 at 19:42