Thread: Performing One's complement on Pointers

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    Performing One's complement on Pointers

    Hi, I am writing a safe pointer object by checking that the pointer is not corrupted before I dereferencing it. I am using the one's complement technique (store a one's complement copy and compare it will the pointer). How do I perform a one's complement on a pointer value? Using ~ on a pointer is invalid (compile complains). I can cast it but I don't think there is any standard on the size of a pointer (so I can't just cast it to integer).

    Is there anyway to do it properly or any other ideas? Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you'll definitely want to check into this to be sure, but I think that pointers are guaranteed, by the standard, to be the same size as an unsigned long. with a cast, you can then convert a pointer to an unsigned long, and perform the ones complement upon it.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    No; please, don't do whatever you are doing.

    Still, the `size_t' type is your best bet.

    Soma

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Hi Soma, could you please explain why? Is it dangerous to do that?

    I actually found that there is a intptr_t which is used to hold the reinterpret_cast result as an int.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    It isn't dangerous if done with extreme care. So long as you are only storing the resulting value for comparison you'll probably be fine, but I've seen a lot of people use different variations on the theme, and others besides, and wind up trampling memory they didn't own because they wound up using the intermediate result as a pointer.

    Consider using, or implementing if you want, a full featured smart pointer.

    I'm pretty sure that `intptr_t' is a C99 thing meaning some C++ compilers will not have it. In theory, `size_t' isn't guaranteed to work, but in practice it is the correct "width" and extremely portable.

    Soma

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Thanks for your help. Could you point me to the right library for a smart pointer that perform such check? Most smart pointer I know are for memory deallocation and checking for uninitialized pointer, but there is not checking for corrupted pointer.
    Yes, I am aware that the intptr_t is C99 but I think we can create a similar type using template. Also, I think it is in C++0x as well? Thanks again.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Hi, I am writing a safe pointer object by checking that the pointer is not corrupted before I dereferencing it.
    So you've got a program that is so broken that there's a chance of overwriting random memory, and you're trying to mitigate the problem? Wouldn't it be more effective to use a memory sanitizer (such as valgrind, or Clang's experimental address sanitizer) to actually find and fix those random accesses?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Thanks for pointing me to those tools CornedBee, they could be useful tools for me.
    But no, it is not because the code is so broken, but it is for a safety critical device and we have to know what would happen when memory corruption DOES happen. And we also have to worry about hardware failure (e.g. RAM not working properly). Proving that things will never go wrong to the auditor is harder than showing them that the device is fail safe.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    ...but I think that pointers are guaranteed, by the standard, to be the same size as an unsigned long...
    Sincerely doubt it.
    long on Windows = 32 bits.
    Pointer on x64 Windows = 64 bits.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Just access the pointer on a byte level. Better yet, employ a template function so that the operation can be generalized for use with any POD.

    Code:
    template <typename T>
    T bit_complement_of(T t)
    {
    	typedef unsigned char U;
    	typedef U* P;
    	U m = ~0;
    	P b = P(&t), e = b + sizeof(T);
    	while(b != e)
    		*b++ ^= m;
    	return t;
    }
    AFIAK, that should be 100% portable.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Minus points for obfuscation, though. Use proper variable names and typedef names. Also get rid of the pointer typedef.
    Also, conversion from T* to unsigned char* is either a compile error or a warning. You should fix that.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    get rid of the pointer typedef.
    Why?

    conversion from T* to unsigned char* is either a compile error or a warning.
    gcc -Wall -pedantic gives not warnings.

    Code:
    template <typename T>
    T bit_complement_of(T data) {
        typedef unsigned char* PUC;
        PUC begin = PUC(&data), end = begin + sizeof(T);
        while (begin != end)
            *begin++ ^= ~0;
        return data;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by oogabooga View Post
    Why?
    Why do you want to use it in the first place?
    It's confusing and saves you only one "*".

    gcc -Wall -pedantic gives not warnings.
    It is weird that it would not. What types have you tried it with?
    Obviously conversion from, say, unsigned int* to unsigned char* (like int n; unsigned char* p = &n) is going to generate warnings or errors. They are totally unrelated types.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Elysia
    Obviously conversion from, say, unsigned int* to unsigned char* (like int n; unsigned char* p = &n) is going to generate warnings or errors. They are totally unrelated types.
    There's a cast involved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, there is, now that you mention it... The code threw me off.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two's complement
    By matthayzon89 in forum Tech Board
    Replies: 7
    Last Post: 08-30-2010, 12:22 AM
  2. 1's and 2's complement
    By Jaguar in forum C Programming
    Replies: 3
    Last Post: 10-14-2003, 09:12 AM
  3. how to complement
    By stautze in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 06:16 AM
  4. 2's complement in C
    By Kokila in forum C Programming
    Replies: 3
    Last Post: 09-07-2002, 11:51 AM
  5. Two's Complement
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 02-26-2002, 05:52 AM