1

I have wrote a code (shown below) that stores the integer 0 to an arbitrary memory location, and then prints the pointer to this memory location and the binary address of this memory location.

The binary memory address in 8 bits long, which I did not expect. I have 4Gb of ram, so surely if the integer is stored in this ram, you will need a lot more than 8 bits to uniquely address a cell(byte) in this ram? Even more confusingly the pointer is 8 symbols long. If the binary address is 8 bits long surely you only need 2 symbols for the pointer to this address?

So why does the address pointer have 8 symbols, and integer binary address locations have 8 bits?

I have taken the byte_to_binary function from this SE Question. Im still a beginner so my code may not be correct.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>     /* strcat */
#include <stdlib.h>     /* strtol */


const char *byte_to_binary(int x)  //code taken from-https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
{
    static char b[9];
    b[0] = '\0';

    int z;
    for (z = 128; z > 0; z >>= 1)
    {
        strcat(b, ((x & z) == z) ? "1" : "0");
    }

    return b;
}

int main(void) //my code starts here
{
    int A = 0;
    int *Ap = &A;
    unsigned int number = (unsigned int)Ap;

    printf("%s \t %d \n \n","int Value",A); //prints the value stored in Ap as an integer

    printf("%s \t %s \n \n","bin Value",byte_to_binary(A)); //prints the value stored in Ap as a binary number 

    printf("%s \t %p \n \n","adress Pointer",Ap); //prints pointer address as pointer type

    printf("%s \t \t %s \n \n","adress",byte_to_binary(number)); //prints pointer address in binary


    return 0;
}

This outputs:

int Value        0

bin Value        00000000

adress Pointer   003FFE1C

adress           00011100

Press any key to continue . . .
Community
  • 1
  • 1
Blue7
  • 1,750
  • 4
  • 31
  • 55
  • How did you conclude that an address is only 8 bits? – Oliver Charlesworth May 05 '14 at 19:22
  • You should have included the output. It's not clear what your question is without it. – user3344003 May 05 '14 at 19:24
  • @ Oli Charlesworth When I print the binary address it is only 8 bits long. I've added a print screen to show what the code returns. – Blue7 May 05 '14 at 19:25
  • @user3344003 I have edited the question and included the output. – Blue7 May 05 '14 at 19:25
  • 3
    "When I print the binary address it is only 8 bits long" - that's because your loop only runs 8 times, which is utterly broken. Pointers on your platform are evidently 32 bits (as evidenced by the output of the `printf` call with the `%p` format specifier), so you need to look at all 32 bits. – nobody May 05 '14 at 19:26
  • @user3344003 - your `byte_to_binary` function is exactly what it claims. It converts 1 byte to string representing that byte. It only converts the lowermost byte. See that loop. It counts down from `128`, which is `10000000` in binary. – ArjunShankar May 05 '14 at 19:29
  • @ArjunShankar If I edit the code to count down from 2^32 = 4294967296, should it work then? – Blue7 May 05 '14 at 19:31
  • 1
    It's far better to copy-and-paste the output as text rather than posting a screenshot of your console window. – Keith Thompson May 05 '14 at 19:31
  • @KeithThompson haha, i've only just now realised you can do that. (Told you I was a beginner). I'll edit the question now. – Blue7 May 05 '14 at 19:33
  • @Blue7 - I expect so. But probably there is a more convenient way to print pointers in Windows. I know that on my box (a GNU/Linux), `printf` can print pointers with `%p`. – ArjunShankar May 05 '14 at 19:33
  • See [this question](http://stackoverflow.com/q/2369541/274261) about printing pointers and `%p`. – ArjunShankar May 05 '14 at 19:35
  • @ArjunShankar Thanks. I have used `%p` to print the pointer, shown in the code in the post, but it printed it in hex form. I wanted it in binary. – Blue7 May 05 '14 at 19:36
  • @Blue7 - Apologies. I see that now. I expect you'll be satisfied with @user3344003's answer below, which essentially is your `2^32` idea written more nicely. – ArjunShankar May 05 '14 at 19:39
  • 1
    @Blue7: Why do you want to print a pointer value in binary? Hexadecimal contains the same information and is much more concise. Would you rather read `0xdeadbeef` or `11011110101011011011111011101111`? – Keith Thompson May 05 '14 at 19:46
  • @KeithThompson Just as a learning exercise. I'm trying to understand pointers, and I wasn't convinced `%p` was actually giving the address in my RAM, so I wanted to print out the binary memory address (without just doing a hex to bin conversion on the `%p` value) so I could compare them. I guess I could have done a "Byte-to-Hex" conversion instead of a "Byte-to-Binary" conversion on the variable `number`, but i didn't think of that at the time. – Blue7 May 05 '14 at 19:55
  • @Blue7 - `%p` is still not going to be an address in your RAM. It addresses [virtual memory](http://en.wikipedia.org/wiki/Virtual_memory). – ArjunShankar May 06 '14 at 05:51

1 Answers1

3

It's your binary conversion function that is not working correctly.

 0x003FFE1C 

is 00000000001111111111111000011100

You're not going far enough with the bits.

You might want to do a

 sizeof (Ap) 

to see how many bits you need. (Here 32)

You should change your loop index so that it is the bit position rather than the shifted value, otherwise you are risking overflow.

for (int ii = 31 ; ii >= 0 ; -- ii)
{

   if (x & (1 << ii) != 0)
      strcat (b, "1) ;
   else
      strcat (b, "0") ;
}
user3344003
  • 20,574
  • 3
  • 26
  • 62