Pointer arithmetic question

This is a discussion on Pointer arithmetic question within the C Programming forums, part of the General Programming Boards category; I need a little help with manipulating pointers. Here is the problem. I have two pointers declared as follows: Header ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Pointer arithmetic question

    I need a little help with manipulating pointers. Here is the problem. I have two pointers declared as follows:

    Header *h1, *h2;

    Header is a struct in the program I'm working on. At this point in my program h1 already points to a location in memory and h2 is currently null. Now I want to make h2 point to a particular amount of bytes past h1 in memory. However, this displacement past h1 is not an integer multiple of the amount of bytes of a Header struct. For example, I need h2 to point to exactly 32 bytes past h1, but Headers are 12 bytes, so I cannot use regular pointer arithmetic (e.g. h2 = h1 + 3 would make h2 point to 36 bytes past h1, but that's the closest I can get to 32).

    Does anyone have any ideas? Thanks for any possible input or advice.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    will typecasting work? Why do you need this? If it is so the structs will be next to eachother remember that a structure has padding

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Does anyone have any ideas?

    With caution flags...
    Code:
    #include <stdio.h>
    
    struct sType
    {
       char element[12];
    };
    
    int main(void)
    {
       struct sType object = {0};
       struct sType *h1 = &object;
       struct sType *h2 = (char*)h1 + 32;
       printf("&object = %p, h1 = %p, h2 = %p\n",
              (void*)&object, (void*)h1, (void*)h2);
       return 0;
    }
    
    /* my output
    &object = 0012FF80, h1 = 0012FF80, h2 = 0012FFA0
    */
    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
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    The program is supposed to be a memory management program.

    h1 points to a header. Directly following that header is a free chunk of memory that a user requested to use which I allocated for him.

    The entire thing (header + memory requested) is 2^k bytes where k is some integer. I need to split that whole thing into halves. So h1 will still point to the same place, but h2 will now point to the middle of that 2^k block, and the whole block has essentially become two smallers ones which I can now use.

    So say the header + free memory for the user is 2^5 = 32 bytes. What I want to do is have h2 point to 2^(5 - 1) = 16 bytes past where h1 points to. Then h1 and h2 would each point to 16 byte blocks of memory, the beginnign 12 of each which would be a header struct. The remaining 4 bytes each would be for the user who requested the memory.

    I hope I explained that well enough.

    Once I point h2 to that right location I will type-cast it as a Header pointer but how do I get h2 to point to that location?

    I'm thinking something along the lines of:

    h2 = (Header *)(&h1 + #bytes);

    or something to that effect, but I'm not quite sure. Thanks.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Thanks Dave...i'm gonna give that a whirl.

    When adding 32 does that add 32 bytes or 32 bytes * sizeOf(char)?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >When adding 32 does that add 32 bytes or 32 bytes * sizeOf(char)?

    Yes. (Since sizeof(char) is 1 by definition, both would be the same.)
    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.*

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Yes I just realized that! Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  2. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  3. Pointer Arithmetic
    By taurus in forum C Programming
    Replies: 5
    Last Post: 11-14-2008, 02:28 AM
  4. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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