Search code examples
cpointerscompilationmemory-addressstatic-memory-allocation

C programming pointer, bytes and memory allocation have 8 question


I am trying to get the bytes and pointers and how they are stored can any one explain or answer some of my questions. Thank you

int num = 513;  <-- allocating a 4 bit memory by initializing
//[01][02][00][00] <-- (numbers are sorted and shown as litle endian)

char * ptr = &num; //char is (one byte)
   ↓
//[01][02][00][00] 
// pointer always start from the [0] (as in this array byte length) 
// in the allocated address in the memory ptr[0] is in this case = [01] 
// (printed as %x02 "printf("the byte %02x\n",ptr[0]);" - if it's only 
//single number 1 a zero will be added on the length so it prints out as 01)

int * ptr = &num; //now creating a pointer with the type of int (four bytes)
    ↓   ↓   ↓   ↓
//[01][02][00][00]
  • how can i access the first byte of this int pointer? [question01]
  • is there a way to see the bites inside the of the first byte([01])? [question02]
  • where does the pointer save the address? does it have to allocate a memory space in the ram to save whe address such as 0x233828ff21 and if so this(0x233828ff21) address requires a lot of bytes? [question03]
  • where does this int pointer stores it's type length (4bytes)? [question05]
  • what happens if i declare a type with longer byte memory allocation such as long long * ptr = &num; [01][02][00][00][00][00][00][00] since i am pointing a long long to a 4 byte int, can those 4 last already been allocated by another program and in use? can i read it? [question06]
  • binary are only 0 and 1 and whether one of those(0 or 1) is called a bite? [question07]
  • one byte is 8 bits right? why am i getting 16 bits 0000000000000001 when converting the number 1 in this website (https://www.rapidtables.com/convert/number/decimal-to-binary.html) shouldn't it be 8? [question08]

Solution

  • • how can i access the first byte of this int pointer? [question01]

    Generally, it is preferable to use unsigned char rather than char to access arbitrary bytes, so let’s do that.

    After unsigned char *ptr = &num;, ptr is a pointer to unsigned char, and you could access the first byte of the int with *ptr or ptr[0], as in printf("The first byte, in hexadecimal, is 0x%02hhx.\n", *ptr);.

    If instead you have int *ptr = &num;, there is no direct way to access the first byte. ptr here is a pointer to an int, and, to access an individual byte, you need a pointer to an unsigned char or other single-byte type. You could convert ptr to a pointer to unsigned char, as with (unsigned char *) ptr, and then you can access the individual byte with * (unsigned char *) ptr.

    • is there a way to see the bites inside the of the first byte([01])? [question02]

    The C standard does not provide a way to display the individual bits of a byte. Commonly programmers print the values in hexadecimal, as above, and read the bits from the hexadecimal digits. You can also write your own routine to write binary output from a byte.

    • where does the pointer save the address? does it have to allocate a memory space in the ram to save whe address such as 0x233828ff21 and if so this(0x233828ff21) address requires a lot of bytes? [question03]

    A pointer is a variable like your other int and char variables. It has space of its own in memory where its value is stored. (This model of variables having memory is used to specify the behavior of C programs. When a program is optimized by a compiler, it may change this.)

    In current systems, pointers are commonly 32 or 64 bits (four or eight 8-bit bytes), depending on the target architecture. You can find out which with printf("The size of a 'char *' is %zu bytes.\n", sizeof (char *));. (The C standard allows pointers of different types to be different sizes, but that is rare in modern C implementations.)

    • where does this int pointer stores it's type length (4bytes)? [question05]

    The compiler knows the sizes of pointers. The pointer itself does not store the length of the thing it is pointing to. The compiler simply generates appropriate code when you use the pointer. If you use *ptr to get the value that a pointer points to, the compiler will generate a load-byte instruction if the type of ptr is char *, and it will generate a load-four-byte instruction of the type of ptr is int * (and int is four bytes in your C implementation).

    • what happens if i declare a type with longer byte memory allocation such as long long * ptr = # [01][02][00][00][00][00][00][00] since i am pointing a long long to a 4 byte int, can those 4 last already been allocated by another program and in use? can i read it? [question06]

    When long long is an eight-byte integer, and you have a long long *ptr that is pointing to a four-byte integer, the C standard does not define the behavior when you attempt to use *ptr.

    In general-purpose multi-user operating systems, the memory after the int cannot be allocated by another program (unless this program and the other program have both arranged to share memory). Each process is given its own virtual address space, and their memory is kept separate.

    Using this long long *ptr in your program may access memory beyond that of the int. This can cause various types of bugs in your program, including corrupting data and alignment errors.

    • binary are only 0 and 1 and whether one of those(0 or 1) is called a bite? [question07]

    One binary digit is a “bit”. Multiple binary digits are “bits”.

    The smallest group of bits that a particular computer operates on as a unit is a “byte”. The size of a byte can vary; early computers had bytes of different sizes. Modern computers almost all use eight-bit bytes.

    If your program includes the header <limits.h>, it defines a macro named CHAR_BIT that provides the number of bits in a byte. It is eight in almost all modern C implementations.

    • one byte is 8 bits right? why am i getting 16 bits 0000000000000001 when converting the number 1 in this website (https://www.rapidtables.com/convert/number/decimal-to-binary.html) shouldn't it be 8? [question08]

    The web site is not merely converting to one byte.

    It seems to show at least 16 bits, choosing the least of 16, 32, or 64 bits that the value fits in as a signed integer type.