Jump to content

Using an exact width integer to determine the width of a pointer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I created an exact width integer (which I can do in c99 using stdint.h) that, by default, holds an integer value large enough to contain a pointer.

First I set its value to an integer that requires 64 bits:


uintptr_t val = 1.8e15;


When I tried to compile it I got an error that said "warning: overflow in implicit constant conversion".

Then I changed it so the variable holds a value that only requires 32 bits:


uintptr_t val = 1e9;


That compiled. So I am now pretty sure that pointer variables are 32 bits on my computer.

Now I'm wondering: If I were to install 3 more GB of RAM (since that would bring it over 4 GB, requiring 64 bit addresses), would the width of the pointer types change? Also, if I compile it on my machine with a limit of of 4 GB of RAM, and then someone runs it on a machine that has a 64 bit addressing scheme, will it still run without getting a bus error? I'm not sure if this is the best place to ask, because I'm asking a hardware-related question on a software development forum, but I just thought I'd give it a shot.
Life's too short to be cool. Be a nerd.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Ask yourself a little question: what generates the application?
A: The compiler/linker.

The toolchain builds a particular type of target executable. So it will not matter what kind of things happen to be available on your particular computer at runtime to influence this.

And FYI: A pointer cannot be a double.

If you want to know the size of a pointer on your system, in bytes:
sizeof(char*)
sizeof(void*)
etc.

If you want to know the number of bits in a byte on your system, use CHAR_BIT (#include <limits.h>).

[edit]For example:
#include <stdio.h>

#include <limits.h>


int main()

{

   size_t size = CHAR_BIT * sizeof(void*);

   printf("size = %d bits\n", (int)size);

   return 0;

}


/* my output

size = 32 bits

*/