0
void *memory;
unsigned int b=65535; //1111 1111 1111 1111 in binary
int i=0;

memory= &b;

for(i=0;i<100;i++){
    printf("%d, %d, d\n", (char*)memory+i, *((unsigned int * )((char *) memory + i)));
}

I am trying to understand one thing.

(char*)memory+i - print out adress in range 2686636 - 2686735.

and when i store 65535 with memory= &b this should store this number at adress 2686636 and 2686637 because every adress is just one byte so 8 binary characters so when i print it out

*((unsigned int * )((char *) memory + i)) this should print 2686636, 255 and 2686637, 255

instead of it it prints 2686636, 65535 and 2686637, random number

I am trying to implement memory allocation. It is school project. This should represent memory. One adress should be one byte so header will be 2686636-2586639 (4 bytes for size of block) and 2586640 (1 byte char for free or used memory flag). Can someone explain it to me thanks.

Thanks for answers.

void *memory;
void *abc;

abc=memory;
for(i=0;i<100;i++){
*(int*)abc=0;
abc++;
}

*(int*)memory=16777215;

for(i=0;i<100;i++){
    printf("%p, %c, %d\n", (char*)memory+i, *((char *)memory +i), *((char *)memory +i));
}

output is

0028FF94,  , -1
0028FF95,  , -1
0028FF96,  , -1
0028FF97,  , 0
0028FF98,  , 0
0028FF99,  , 0
0028FF9A,  , 0
0028FF9B,  , 0

i think it works. 255 only one -1, 65535 2 times -1 and 16777215 3 times -1.

  • only remove unsigned int ? output now is 2686636, -1 and 2686637, -1 – Jan Jesenius Sep 28 '14 at 19:07
  • i do this printf("%p, %c, %d\n", (char*)memory+i, *((char *) memory + i), *((char *) memory + i)); and it gives 2686636, ,-1 but i noticed when i make b = 65535 first 2 adresses are blank space, -1 but when i make b = 4294967295 first 4 adresses are blank space, -1 and 4294967295 is 32 bits so i think it works somehow. – Jan Jesenius Sep 28 '14 at 19:24
  • i changed the code and i think it is working now chceck the question i posted it at the end – Jan Jesenius Sep 28 '14 at 20:03
  • I am suprised that the second part works! – γηράσκω δ' αεί πολλά διδασκόμε Sep 28 '14 at 20:48
  • why ? and i tried this *(char*)memory='a'; instead of some number and the output was 0028FF94, a, 97 and all others memory cells were 0. so i think it is ok and it works just like it should. – Jan Jesenius Sep 28 '14 at 20:52
  • *memory* points to *somewhere*. *abc* points to *somewhere*. *abc* points to the same somewhere as *memory*. Write 0 to somewhere. Do you know how many places *abc++* moves? It is a void pointer... – γηράσκω δ' αεί πολλά διδασκόμε Sep 28 '14 at 21:08
  • i know it is chaotic but they want from us to implement our own memory allocation and memory free and i am doing it like this and it works . at this point i am not able to do it better and this is not actual code of my project. On this code i am just trying to undrestand how these adresses works so i would be able to traverse memory chunks for example and so on. – Jan Jesenius Sep 28 '14 at 21:14
  • *... they want from us to implement our own memory allocation*. So do it. The second part is garbage(sorry for the wording). The first at least makes some sense. *memory= &b;* is correct. Work from there. Dont use void pointer. Use char instead and cast appropriately. – γηράσκω δ' αεί πολλά διδασκόμε Sep 28 '14 at 21:19
  • i understand you. But this is not code from my project. Code in this question is just to understand how pointers and casting works. if you want to know more i can send you mail. – Jan Jesenius Sep 28 '14 at 21:33

2 Answers2

1

In your program it seems that address of b is 2686636 and when you will write (char*)memory+i or (char*)&b+i it means this pointer is pointing to char so when you add one to it will jump to only one memory address i.e2686637 and so on till 2686735(i.e.(char*)2686636+99).


now when you are dereferencing i.e.*((unsigned int * )((char *) memory + i))) you are going to get the value at that memory address but you have given value to b only (whose address is 2686636).all other memory address have garbage values which you are printing.


so first you have to store some data at the rest of the addresses(2686637 to 2686735) good luck.. i hope this will help

Ashish Aggarwal
  • 334
  • 2
  • 13
0

I did not mention this in my comments yesterday but it is obvious that your for loop from 0 to 100 overruns the size of an unsigned integer.

I simply ignored some of the obvious issues in the code and tried to give hints on the actual question you asked (difficult to do more than that on a handy :-)). Unfortunately I did not have time to complete this yesterday. So, with one day delay my hints for you.

Try to avoid making assumptions about how big a certain type is (like 2 bytes or 4 bytes). Even if your assumption holds true now, it might change if you switch the compiler or switch to another platform. So use sizeof(type) consequently throughout the code. For a longer discussion on this you might want to take a look at: size of int, long a.s.o. on Stack Overflow. The standard mandates only the ranges a certain type should be able to hold (0-65535 for unsigned int) so a minimal size for types only. This means that the size of int might (and tipically is) bigger than 2 bytes. Beyond primitive types sizeof helps you also with computing the size of structures where due to memory alignment && packing the size of a structure might be different from what you would "expect" by simply looking at its attributes. So the sizeof operator is your friend.

Make sure you use the correct formatting in printf.

Be carefull with pointer arithmetic and casting since the result depends on the type of the pointer (and obviously on the value of the integer you add with).

I.e.

(unsigned int*)memory + 1 != (unsigned char*)memory + 1

(unsigned int*)memory + 1 == (unsigned char*)memory + 1 * sizeof(unsigned int)

Below is how I would write the code:

//check how big is int on our platform for illustrative purposes
printf("Sizeof int: %d bytes\n", sizeof(unsigned int));

//we initialize b with maximum representable value for unsigned int
//include <limits.h> for UINT_MAX
unsigned int b =  UINT_MAX; //0xffffffff (if sizeof(unsigned int) is 4)

//we print out the value and its hexadecimal representation
printf("B=%u 0x%X\n", b, b);

//we take the address of b and store it in a void pointer
void* memory= &b; 

int i = 0;

//we loop the unsigned chars starting at the address of b up to the sizeof(b) 
//(in our case b is unsigned int) using sizeof(b) is better since if we change the type of b
//we do not have to remember to change the sizeof in the for loop. The loop works just the same
for(i=0; i<sizeof(b); ++i)
{
    //here we kept %d for formating the individual bytes to represent their value as numbers
    //we cast to unsigned char since char might be signed (so from -128 to 127) on a particular 
    //platform and we want to illustrate that the expected (all bytes 1 -> printed value 255) occurs.
    printf("%p, %d\n", (unsigned char *)memory + i, *((unsigned char *) memory + i));
}

I hope you will find this helpfull. And good luck with your school assignment, I hope you learned something you can use now and in the future :-).

Community
  • 1
  • 1
ds27680
  • 1,993
  • 10
  • 11