🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Function memory question in C

Started by
3 comments, last by Prototype 4 years, 2 months ago

Hi guys,

I have a question about an architecture where you have access to any point in RAM. (C64 6502, if anyone is wondering ?).

Is it possible in ‘C’ to specify the location in RAM for a particular function? For example, if I want function XYZ() to reside exactly at $8000 in RAM, can this be done?

Many thanks in advance ?

Advertisement

As far as I know that is not defined in the C language standard. I would be surprised if it was btw, the language tries to be system-agnostic, and being able to specify an address means that computer systems that don't give such control at user level cannot implement the language.

I think you might want to look at specifying such a thing to a linker. That sounds like a more viable candidate for such requests to me.

Alternatively, you can likely create some assembly language glue that defines an absolute jump instruction (I think it exists in 6502, long time ago :p ) at that address to the start of the C code. The latter kind of stuff may be needed anyway, as the compiler might use some standard assumptions about registers, stack content and so on, which likely don't hold if you just jump to an address.

You can try to make use of pointers to functions in C.
Depending of what is the thing you exactly want to achieve, it probably will do the job for you.

https://www.geeksforgeeks.org/function-pointer-in-c/

You can use arrays of pointers to functions in C.

For example, if you use an interpreted language, you can have arrays of data and the indices to that array are your pointer-like equivalent to pointers in C/C++. You emulate the dogma of pointers.

Using pointers to functions is a very powerful tool. Are you sure, putting a function to a place in RAM is the only solution to your problem? Don't answer to me, ask yourself and answer to yourself. Maybe you can redesign things.

This answer maybe works for you, but he suggests to use pointers to functions too:
https://stackoverflow.com/questions/18778323/how-to-specify-a-memory-location-at-which-function-will-get-stored

The thing is, if you move your programming logic to the compiler/linker/configfile, you lose programability and make things more manual. Try to stay inside the programming language and not wander outside it.

Not really. You can do that sort of thing with an assembler but you will end up with unused memory blocks that will be stored into your program. You can however specify a starting address with the --start-addr parameter, assuming you're using CC65. Note that you can't load it like a normal BASIC program that way and have to start with a SYS command.

This topic is closed to new replies.

Advertisement