Problems with creating a pointer to struct at a specific address

This is a discussion on Problems with creating a pointer to struct at a specific address within the C Programming forums, part of the General Programming Boards category; Hello, I have problems when I trying to create a pointer to a struct initialized at a specific address. I ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    6

    Problems with creating a pointer to struct at a specific address

    Hello, I have problems when I trying to create a pointer to a struct initialized at a specific address. I tied a lot of different combinations, but still I does not get it to work. Here is one example:

    Code:
    #include <stdio.h> 
    #define INIT_ADR ((pointerFOO) 0x00000000);  
     
    typedef struct FOO_tag{ 
        int     PER; 
        int     PDR; 
        int     PSR; 
    } FOO, *pointerFOO; 
     
     
     
    int main(void) 
    { 
     
      pointerFOO pFOO = INIT_ADR; 
     
      printf("&(pFOO->PER)= %i\n",&(pFOO->PER)); 
      printf("&(pFOO->PDR)= %i\n",&(pFOO->PDR)); 
     
      pFOO->PER = 1;  
     
     
       return(0); 
    }
    I get some error with pFOO->PER = 1 when I am trying to run the code. Is there some who knows how I can solve this problem?

    /Dan

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by danba185 View Post
    I get some error with pFOO->PER = 1 when I am trying to run the code. Is there some who knows how I can solve this problem?
    yes try another address, preferable one that has been returned by a call to malloc().
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    What do you mean by "a struct initialized at a specific address"? are you talking about that FOO global variable? What do you want to do exactly?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Maybe you should say what that "some error" is, then we might be able to help.

    ZuK: I suspect this is for some kind of embedded system which has to use a specific address for some hardware registers, and malloc will likely not return the desired address.

    Edit: I compiled the code myself and got warnings about format %i expecting an int but the argument is int *. After changing %i to %p it compiled without warnings but crashed with a segmentation fault at the line with "pFOO->PER = 1;" (which I fully expected to happen). Of course, I'm running this on Linux rather than whatever system you're writing this code for, so who knows what the desired behavior is for your system.
    Last edited by christop; 07-30-2012 at 02:23 PM.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    @ christop: the op specifically told that the error happens at the line "pFOO->PER = 1;
    that's why I think that NULL is not a valid address to write to on his system ( embedded system or not ).
    and yes I didn't spot the type missmatch in the printf() calls.
    Kurt

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Hi, thank you all for the replies. Yes I will run this on an embedded system and that is why the memory address is very important. I am also running Linux and it crashed with a segmentation fault for me as well. Do you have any suggestions of how I can specify the address of where the struct shall be located and still using pointers? I tried to use
    Code:
     pFOO = (struct FOO *) malloc(sizeof(struct FOO));
    and it did work fine and I did not get any segmentation fault, but is it possible to define the address?

    /Dan

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You code is fine if you change the printf calls. Just make shure that the address you specify is valid. NULL seems not to be writable.
    Kurt

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Can you give me an example of how I shall do if I want to specify the address to 0x12345678?
    /Dan

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <stdio.h>
    #define INIT_ADR ((pointerFOO)0x12345678); 
      
    typedef struct FOO_tag{
        int     PER;
        int     PDR;
        int     PSR;
    } FOO, *pointerFOO;
      
    int main(void)
    {
      pointerFOO pFOO = INIT_ADR;
      
      pFOO->PER = 1; 
       return(0);
    }
    Kurt

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    It does not work, this was my main problem and I still get a segmentation fault when I run the code?
    /Daniel

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You can't just pick an address randomly like that.
    If the underlying O/S uses virtual memory, that address must be on a mapped page (static data, stack, heap, whatever), otherwise you get a seg fault.
    If there's no virtual memory, then you can indicate the linker where objects are located (for instance), but this is beyond C programming.

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you run that on linux I don't know of a way to find a valid constant for that address.
    If you run it on an embedded system check the spec.
    Kurt

  13. #13
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    An address that is valid on an embedded system (or even from inside the Linux kernel, which sees "real" memory versus "virtual" memory) probably isn't going to be valid in a user-space program in Linux. For example, you might be able to write to address 0x00000000 or 0x12345678 in your embedded system (you will have to check the documentation for the embedded system to be sure), but it most likely won't be a valid address in your Linux program. Software for embedded systems generally isn't portable like that.

  14. #14
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    You are right, thank you all for the valuble information. It would be very powerful if I could debug the C-code before it is loaded into the embedded system. I am running eclipse, do you know if there is any plugin which makes this possible to run some virtual memory instead of accessing the real memory of the computer?

    /Dan

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Why not declare a static array at the beginning of your program to simulate the computer memory, e.g.:
    Code:
    char RAM[1024]; /* 1Kb of RAM */
    ...
    int main(void) {
      ...
    }
    Then if you want to set an object at a specific address, just do:
    Code:
    struct foo *p = (struct foo*)(RAM+offset); /* where offset is your custom address */
    As a last note, be careful about alignment, the address must be compatible with the pointed type.

    A better solution would be to use a simulator for whatever type of embedded system you're working on, but maybe it's expensive.
    Last edited by root4; 07-31-2012 at 04:10 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 04:38 PM
  2. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 09:04 PM
  3. Allocate a variable at a specific address
    By mermaidsrule in forum C Programming
    Replies: 11
    Last Post: 05-03-2007, 05:25 AM
  4. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM
  5. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 04:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21