Thread: Pointer help

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Pointer help

    Hey guys

    I am working on an OS at the moment having troubles with pointers. I have a function that only returns an int, I cannot change this, and I need to return a data structure with it. Currrently I am returning the address of the data structure;
    Code:
    int func() {
       ......
       ......
       return (int)&data_struct
    }
    The problem i have is how to get a data structure pointer to point to the value of an integer.
    Code:
    int addr = func();
    ps_t *ps_ptr;
    ps_ptr = ????
    I need to get ps_ptr to point to the address that is the intergers value

    for example if addr = 0x1234, how can I get the pointer to point to this memory location

    any help would be greatly appricated

    - malkav

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I am working on an OS at the moment having troubles with pointers.
    Not a good sign

    > I have a function that only returns an int, I cannot change this,
    Why not? you're in control of everything.
    Sure you could smash the type system away and assume that pointers and integers are the same size, but that's hardly the way to go.
    How are you going to ensure for example that data_struct is always a ps_t ?

    > return (int)&data_struct
    What's the betting this is a local variable?

    > ps_ptr = ????
    You've already polluted your code with a cast, just add another one.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    >> I have a function that only returns an int, I cannot change this,
    > Why not? you're in control of everything.
    > Sure you could smash the type system away and assume that pointers and > > > integers are the same size, but that's hardly the way to go.
    > How are you going to ensure for example that data_struct is always a ps_t ?
    I cannot change what the function returns because they are system call. If I changed it, I would have to change all system calls and all processes that depend on the system calls. And no I did not didn't program this, it was programmed by some one else.

    >> return (int)&data_struct
    > What's the betting this is a local variable?
    It is not, the OS uses a flat memory space at the moment and processes can access any memory location

    >> ps_ptr = ????
    > You've already polluted your code with a cast, just add another one.
    Im not sure what you mean, ps_ptr = (ps_t *)addr will cast the value of addr into the structure. I need to point to the memory address of the value.

  4. #4
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by malkav
    Hey guys

    I am working on an OS at the moment having troubles with pointers.
    I almost laughed out loud when I read that!

    If you're simply just trying to return a pointer to something, and cast that return value to some other type of pointer, just return a pointer to void (void*) and then cast it. For example:

    Code:
    void* funct(somestruct* foo)
    {
         /*blah blah*/
         return &foo;
    }
    
    //in calling function
    otherstruct* bar = (otherstruct*) funct(blah);
    Last edited by homeyg; 03-17-2006 at 09:17 AM.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Well every one has to start some where, how else am i going to learn?

    Like i said if i could change the return type to a void * then I already would have but there is too much code that depends on it. All the systems calls have single entry point into the kernel so are contained in a wrapper function. This wrapper returns an int. There is no way I can feasibly change it without days and possible weeks of extra work

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    I can do it in assembly like this;

    mov $addr, %eax
    then access the structure with (%eax), 4(%eax) etc... but i would have like to find the solution in C

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > It is not, the OS uses a flat memory space at the moment and processes can access any memory location
    It seems to me you know squat about C, never mind anything else.
    I didn't ask whether your memory is flat, or a single address space, I asked if you were doing this
    Code:
    int func() {
       ps_t data_struct;
       ......
       return (int)&data_struct;
    }
    > There is no way I can feasibly change it without days and possible weeks of extra work
    Well the longer you leave it, the worse it will get.
    How long are you going to spend on finding unforced errors before you implement a system which at least offers some degree of automated checking?
    A few days rewriting now - or a few weeks chasing some very odd bugs later on - it's your call.


    > And no I did not didn't program this, it was programmed by some one else.
    Someone who also didn't have a clue how to generate interfaces either.
    What happens when int is no longer large enough to return your answer?

    > Im not sure what you mean, ps_ptr = (ps_t *)addr will cast the value of addr into the structure.
    Works for me.
    That is, if you're trying to recover
    ps_ptr = &data_struct;
    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.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    > It seems to me you know squat about C, never mind anything else.
    I wouldn't say I know squat, but I can only dream of the day I can proclaim myself "god of the C" and sit on forums all day flaming people.

    > Well the longer you leave it, the worse it will get.
    It is just not possible, there are external applications that are not under my control that depend on the syscall interface

    > What happens when int is no longer large enough to return your answer?
    It will always run on a 32bit architecture so should be adequate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM