Thread: Allocate a variable at a specific address

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Allocate a variable at a specific address

    I need to allocate a variable that needs to be located at a specific address, say at address 0x00010000 everytime I compile and run my program. I'm interfacing with another piece of hardware that expects a certain value to be at that address. I was just wondering how I can do this in C (or even if it could be done at all). While I know I could just do it with a pointer to that location,

    *(0x00010000) = number;

    I would really like to allocate space for it so it doesn't get overwritten by anything else.

    If not then I would need to allocate this variable in assembly (mips risc architecture) to be located at 0x00010000, and well, I don't know how to do that either. My guess is to use assembly directives (pseudo-ops?) to do this if can't be done in C.... Any help or suggestions would be appriciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's going to depend on your OS if you can actually get away with it. Assuming it is:
    Code:
    (pointer type*)<address> = malloc( somesize );
    Cast the address to a pointer of whatever type it's supposed to be, and assign the return from your allocation function. Otherwise, you'll do what you've done. Just type cast the address as a pointer to whatever type it is supposed to be, dereference it, and assign it a value.


    Quzah.
    Last edited by quzah; 05-02-2007 at 09:14 PM. Reason: Sheesh! Forgot my Crayons!
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The best place to look is in your manual. Such things are not really C-specific. Perhaps a #pragma, perhaps a header with #defines -- but such things are to be found in the manual.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why not:
    Code:
    int* p = 0x00010000;
    *p = 55;
    or you actually want to tell your compiler NOT to use this address for something else?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't just grab an address and take it and use it without expecting some problem. You have to specifically request it.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    int* p=(int*)0x00020000;

    *p=10;

    The above code stores 10 at the address 0x00020000.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by forumuser View Post
    int* p=(int*)0x00020000;

    *p=10;

    The above code stores 10 at the address 0x00020000.
    Caveat: maybe.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    int*p=(int*)0x00020000;
    casting is mandatory
    *p=10;

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MacGyver View Post
    You can't just grab an address and take it and use it without expecting some problem. You have to specifically request it.
    Actually it depends on your OS, as I stated in the first reply.
    Quote Originally Posted by forumuser View Post
    int*p=(int*)0x00020000;
    casting is mandatory
    *p=10;
    By 'maybe', Dave meant it depends on your OS, as I mentioned previously.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem with casting the address to a pointer type really is, what if the linker chooses to place some other variable at that address? The linker has no idea that you have "stolen" this memory location for your own use. So a global or function static variable might happen to be placed at that address by chance, which stomps over your other usage of that memory location.

    Of course, if you locate the variable at an address that you can guarantee the linker won't pick, you will not have this problem. Say for instance, a memory mapped IO port, or a video framebuffer. The linker usually knows the regions of VM where these things are placed, and would never put a variable there.

    In fact, many compilers/linkers can be commanded to place a certain C variable at a specific address, using some platform specific method. That would be the best way to do this. Portability obviously is no concern anyway.

    So the next question is, what is special about this address, and why do you want to use it this way?

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    2

    Smile

    Quote Originally Posted by brewbuck View Post

    So the next question is, what is special about this address, and why do you want to use it this way?
    I am interfacing with another piece of hardware. Basically my program and this other piece of hardware have to agree on a location in memory on the PC which the program is running where this variable is stored so we both have access to it. There is nothing special about the address in particular, it can be any memory location, just as long as my program and the hardware I am trying to get working with it both know where it is. I just picked 0x00010000 because that is the default setting for my hardware, but I can change it with some difficulty to be anything I want (about an 8 hour process though). That's why I want to fix this variable... anytime I change my program, the variable location changes and it takes 8 hours to get the external hardware to match the new location... so debugging is a nightmare!!!

    I know that I can write to this memory location ok, simply dereference a pointer, but as mentioned aboved, I want to be sure the linker doesn't put something else there, or have the stack grow into it, etc etc. I would like that memory location reserved for my variable so nothing else will accidently write to it. And this memory location needs to be the same everytime I recompile my program, so if I modify my program the hardware can still find this particular variable at the same location everytime.

    As far as the OS goes, windows XP, and microsoft's visual C++ or gcc for compiler or any other C compiler that might be able to accomplish this is fine, doesn't matter which to use, in case anyone wants to know. Also my program does not need to be portable as I am trying to accomplish a very specific task with a specialized piece of hardware, and will only be run from this particular PC anyways.

    quzah, I am going to try your malloc suggestion and see if that works. Thanks for the tip.

    Someone suggested use pragma's above. I'm not so familiar with pragmas, and have little experience with them. Anyone know how I might accomplish this with a pragma?

    Thanks so much!
    Last edited by mermaidsrule; 05-03-2007 at 01:04 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > If not then I would need to allocate this variable in assembly (mips risc architecture) to be located at 0x00010000
    > As far as the OS goes, windows XP, and microsoft's visual C++ or gcc for compiler
    Lemme get this straight, you're developing code on your PC, but running the code on a MIPS board?

    If it is a 'target board', then you're almost certainly using (or going to need) a cross-compiler to compile the code.

    Further, is this board plugged into say a PCI slot on your PC?

    We need some diagrams showing exactly what sort of system you have set up.

    For example, XP uses virtual memory, so unless you're writing a device driver, trying to locate at specific address just isn't going to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  2. eh, help?
    By The_Nymph in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 02:27 PM
  3. doubly linked lists
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:33 AM
  4. variable sized arrays: compiler specific?
    By dirkduck in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 11:01 PM
  5. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM