What is the bit length of memory address in C?

Walkbitterwyrm Lightwarg 20 Reputation points
2023-09-03T21:27:05.9+00:00

I want to know what is the bit length of the memory address stored by the pointers in C on 64-bit processor machines and if any pointer needs an identifier as a name then is it also stored with a memory address?

Do pointers also take memory like variables?

Developer technologies | C++
Developer technologies | Visual Studio | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Barry Schwarz 3,746 Reputation points
    2023-09-03T23:35:56.43+00:00

    The bit length of a pointer is the number of bytes in the pointer multiplied by the number of bits in a byte. The compiler will tell you the size of a particular pointer if you use the sizeof operator. The number of bits in a byte is provide by the CHAR_BIT macro defined in header limits.h. If you need to, you can compute with

    bit_length = sizeof pointer_name * CHAR_BIT;

    Like all other variables, pointers require a valid name. The name of your pointer is not present in your executable code. (This is true also for the names of your other variables.) Unless optimized out of memory, a pointer will occupy sizeof pointer bytes. (The same is also true for your other variables.)

    While uncommon on most home systems today, it is possible for pointers of different types to have different sizes.

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-09-04T02:23:09.6566667+00:00

    on a typical 64 bit processor the size of a pointer variable is 64 bits or the same as a long int or 8 bytes.

    so in c

    int main()
    {
        char address = 'A';   // define an 1 bytes address initialsed to 'A' (65)
        char* pointer = &address; // pointer to this address
        long var1;            // variable larger enough to hold addresss
        char var2[4];         // another variable large enough
        
        var1 = (long) pointer;  // copy address to var1 
        
        // buffer copy of pointer value to array of bytes (or use memcpy())
        char* cp1 = (char*)pointer;
        char* cp2 = &var2[0];
        for (int i = 0; i < 4; ++i)
        {
            *cp2++ = *cp1++;
        }
        
        printf("%c %c %c\n", *pointer, *((char*) var1), *((char*) var2));
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.