Pointer Arithmetic problems

This is a discussion on Pointer Arithmetic problems within the C++ Programming forums, part of the General Programming Boards category; Code: baseAddress = (IMAGE_DOS_HEADER*)MapViewOfFile(fileMapHandle, FILE_MAP_READ, 0, 0, 0 ); if( baseAddress != NULL ) { ntHeaderBaseAddress = (IMAGE_NT_HEADERS*)baseAddress + baseAddress->e_lfanew; ...

  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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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-18-2009, 11:14 PM
  3. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 12:54 PM
  4. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2008, 11:06 PM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM

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