Thread: Memory Address and Hexidecimal

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    Memory Address and Hexidecimal

    Ok, this isn't really c++, but that is the language I'm working with. Basically I am suppose to determine how many bytes my compiler is using for each data type(i.e. int, double, char, etc..). So I'm printing my memory addresses out for example:

    Address of si1: 0012FF60
    Address of si2: 0012FF54

    those are 2 short ints that were defined back to back in my program. Now cleary I can tell the difference of 4 bytes, which means my compiler is using 4 bytes of memory for a short int. Now the problem comes in when I get memory address such as

    Address of boolVar1: 0012FEB7
    Address of boolVar2: 0012FEAB

    Now, I know A = 10 in decimal and B = 11, I'm not sure if that is where I should be looking, but how do I determine the difference in bytes between these 2 memory addresses?

    Help is much appreciated!

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    And you cannot use sizeof()?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can substract two pointers (into an array so as not to do something undefined) cast to char*, since the size of other types is a multiple of sizeof(char), and pointer arithmetic takes into account the size of the pointed type.

    Code:
    #include <iostream>
    
    int main()
    {
        int arr[2];
        std::cout << reinterpret_cast<char*>(&arr[1]) - reinterpret_cast<char*>(&arr[0]) << '\n';
    }
    Of course, sizeof gives you the answer without anything fancy.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by anon View Post
    You can substract two pointers (into an array so as not to do something undefined) cast to char*, since the size of other types is a multiple of sizeof(char), and pointer arithmetic takes into account the size of the pointed type.

    Code:
    #include <iostream>
    
    int main()
    {
        int arr[2];
        std::cout << reinterpret_cast<char*>(&arr[1]) - reinterpret_cast<char*>(&arr[0]) << '\n';
    }
    Of course, sizeof gives you the answer without anything fancy.
    True enough but since that answer (sizeof()) is so obvious I suspect the instructor or whatever wants to see if they understand this stuff at a memory address level. I think, who knows. It does seem to be a rather exotic method of learning the size of an int though
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    intervade: Assuming these variables are declared next to each other, you should be able to simply subtract the address from the one with the highest address with the one with the lowest. This should be simple subtraction, only that for each digit, there are 16 combinations instead of 10.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    intervade: Assuming these variables are declared next to each other, you should be able to simply subtract the address from the one with the highest address with the one with the lowest. This should be simple subtraction, only that for each digit, there are 16 combinations instead of 10.
    You might be able to do that, but it might also give false results. It depends on the cpu architecture and compiler optimization level.
    As an example we can take a variation of anon's code and have it use 2 char variables defined next to each other:
    Code:
    #include <iostream>
    
    void main()
    {
    	char a, b;
    	std::cout << "pointer substraction: " << reinterpret_cast<char*>(&b) - reinterpret_cast<char*>(&a) << std::endl;
    	std::cout << "sizeof(char): " << sizeof(char) << std::endl;
    
    }
    Compiled in debug mode the output is
    pointer substraction: 1
    sizeof(char): 1
    from both a 32 and 64-bit exe
    However, when compiled in release mode with optimizations enabled the output is
    pointer substraction: 4
    sizeof(char): 1
    for 32-bit and
    pointer substraction: 8
    sizeof(char): 1
    for 64-bit
    on an Intel Core2 cpu.

    As you can see variables are word-aligned. The only way to make sure your variables are right next to each other without padding is to use an array or the "#pragma pack" directive.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't use void main. Use int main.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    I know that void main() is technically not allowed in the C++ standard, but I see no problems with using it on an x86 system as it is an implicit "return 0". At least on MSVC which is the compiler I always use, I haven't tried any other ones so I can't comment on them.
    As long as you are aware of the limitations and dangers of it it is in my opinion quite harmless.

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by _Mike View Post
    I know that void main() is technically not allowed in the C++ standard, but I see no problems with using it on an x86 system as it is an implicit "return 0". At least on MSVC which is the compiler I always use, I haven't tried any other ones so I can't comment on them.
    As long as you are aware of the limitations and dangers of it it is in my opinion quite harmless.
    Well harmless maybe but worse, it discards an important and valuable bit of information for the OS and other applications making it useless as well. MS has really gone to great lengths to discourage the concept of using many small programs to accomplish a task (more the UNIX way of thinking) which IMHO has done this generation of coders a disservice since they are educating a useful tool right out of your mental toolset. The return code is useful and the implicit "return 0" means "lie to the calling program", not a cool thing when writing anything more than toy applications...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by jeffcobb View Post
    Well harmless maybe but worse, it discards an important and valuable bit of information for the OS and other applications making it useless as well. MS has really gone to great lengths to discourage the concept of using many small programs to accomplish a task (more the UNIX way of thinking) which IMHO has done this generation of coders a disservice since they are educating a useful tool right out of your mental toolset. The return code is useful and the implicit "return 0" means "lie to the calling program", not a cool thing when writing anything more than toy applications...
    But to be fair the code piece I wrote is just a toy application
    And there's not really anything that can go wrong in it that needs an exit code other than 0. (And it doesn't discard the exit code, it just forces it to zero)
    cout might possibly throw an exception but since I'm not using any exception handling I have no way of gracefully recovering from it, and an explicit return 0 would make no difference.
    However if I do write code where things can go wrong/unexpectedly or when I need to know the result of the operation I do use int main.
    I'm not saying that people should prefer void over int even for simple programs, but I also don't think void main should be completely disallowed as long as you are aware of what it does "behind the scenes" so to speak.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    void main is not standard, and that is reason enough not to use it.
    Besides that, void main does and cannot return 0. MSVC uses a hack that returns 0 in case of void main, and again, this is not standard! GCC, for example, doesn't return anything, and takes the return value of the function--which the function doesn't return!--and returns it to the OS.
    This is clearly undefined behavior.
    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. "Pointers" <-- crappy name
    By argv in forum General Discussions
    Replies: 64
    Last Post: 06-25-2009, 08:18 PM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM