Thread: Pointer Arithmetic problems

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    167

    Pointer Arithmetic problems

    Code:
     
        baseAddress = (IMAGE_DOS_HEADER*)MapViewOfFile(fileMapHandle, FILE_MAP_READ, 0, 0, 0 );
        if( baseAddress != NULL ) {
    		ntHeaderBaseAddress = (IMAGE_NT_HEADERS*)baseAddress + baseAddress->e_lfanew; 
           return true;
        } else {
           return 0;
        }
    The vc debugger says the the structure members for ntHeaderBaseAddress can't be evaluated. I think this a pointer arithmetic problem. What am I doing wrong?
    silk.odyssey

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    (IMAGE_NT_HEADERS*)baseAddress + baseAddress->e_lfanew;
    I'd bet that's where you error is. I can quite put my finger on it, but something about that equation just rubs me the wrong way. Maybe it's the scope of the typecast, maybe it's adding the member... But that's where I'd look.

    And this looks like straight C - is it?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    IIRC you can not add two pointers together. You can subtract a pointer from another pointer and can add an integer to a pointer, but you can not add two pointers together.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    I'm adding a pointer to an integer. e_lfanew is defined as a long.
    Last edited by silk.odyssey; 08-18-2004 at 05:29 AM.
    silk.odyssey

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ntHeaderBaseAddress = (IMAGE_NT_HEADERS*)baseAddress + baseAddress->e_lfanew;
    Remember that pointer arithmetic takes the size of what the pointer points to into account.

    Example, if sizeof(IMAGE_NT_HEADERS) is 10, and e_lfanew is say 4, then
    ntHeaderBaseAddress is 40 bytes away.
    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. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. pointer problems
    By BradC78 in forum C Programming
    Replies: 9
    Last Post: 02-19-2009, 12:14 AM
  3. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  4. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2008, 12:06 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM